[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-pnet-commits] CVS: pnetlib/System/Net/Sockets TcpClient.cs,1.4,
From: |
Rhys Weatherley <address@hidden> |
Subject: |
[Dotgnu-pnet-commits] CVS: pnetlib/System/Net/Sockets TcpClient.cs,1.4,1.5 TcpListener.cs,1.1,1.2 |
Date: |
Thu, 03 Apr 2003 02:14:44 -0500 |
Update of /cvsroot/dotgnu-pnet/pnetlib/System/Net/Sockets
In directory subversions:/tmp/cvs-serv16218/System/Net/Sockets
Modified Files:
TcpClient.cs TcpListener.cs
Log Message:
Rewrite the "TcpClient" and "TcpListener" classes.
Index: TcpClient.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System/Net/Sockets/TcpClient.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** TcpClient.cs 28 Sep 2002 01:55:06 -0000 1.4
--- TcpClient.cs 3 Apr 2003 07:14:41 -0000 1.5
***************
*** 1,8 ****
/*
! * TcpClient.cs - Implementation of the "System.Net.Sockets.TcpClient" class.
*
! * Copyright (C) 2002 Francis Rogers.
! *
! * Contributed by Francis Rogers <address@hidden>
*
* This program is free software; you can redistribute it and/or modify
--- 1,7 ----
/*
! * TcpClient.cs - Implementation of the
! * "System.Net.Sockets.TcpClient" class.
*
! * Copyright (C) 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
***************
*** 21,217 ****
*/
- // this is a non-ECMA class
- #if !ECMA_COMPAT
- using System;
- using System.Net;
- using System.Net.Sockets;
-
namespace System.Net.Sockets
{
! public class TcpClient
{
! private static Socket sock;
! private static NetworkStream stream;
! public void Connect(System.Net.IPEndPoint remoteEP)
! {
! sock.Connect(remoteEP);
! }
!
! public void Connect(System.Net.IPAddress address, int port)
! {
! IPEndPoint remoteEP = new IPEndPoint(address, port);
! sock.Connect(remoteEP);
! stream = new NetworkStream(sock);
! }
!
! public void Connect(string hostname, int port)
! {
! IPHostEntry host = Dns.GetHostByName(hostname);
! IPEndPoint remoteEP = new IPEndPoint(host.AddressList[0],
! port);
! sock.Connect(remoteEP);
! stream = new NetworkStream(sock);
! }
public void Close()
! {
! stream.Close();
! sock.Close();
! }
protected virtual void Dispose(bool disposing)
! {
! if (sock != null)
! sock.Close();
! }
!
! public System.Net.Sockets.NetworkStream GetStream()
! {
! return stream;
! }
!
! public TcpClient(string hostname, int port)
! {
! sock = new Socket(AddressFamily.InterNetwork,
! SocketType.Stream,
! ProtocolType.Tcp);
! IPHostEntry host = Dns.GetHostByName(hostname);
! IPEndPoint remoteEP = new IPEndPoint(host.AddressList[0],
!
port);
! sock.Connect(remoteEP);
! stream = new NetworkStream(sock);
! }
! public TcpClient()
! {
! sock = new Socket(AddressFamily.InterNetwork,
! SocketType.Stream,
! ProtocolType.Tcp);
! }
!
! public TcpClient(System.Net.IPEndPoint localEP)
! {
! sock = new Socket(AddressFamily.InterNetwork,
! SocketType.Stream,
! ProtocolType.Tcp);
! sock.Bind(localEP);
! }
!
! // TODO: should this be internal?
! internal TcpClient(System.Net.Sockets.Socket newsock)
! {
! sock = newsock;
! }
!
! protected bool Active
! {
! get
! {
! return sock.Connected;
! }
! }
!
! protected System.Net.Sockets.Socket Client
! {
! get
! {
! return sock;
! }
! }
!
! public bool LingerState
! {
! get
! {
! return
(bool)sock.GetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.Linger);
! }
! set
! {
! sock.SetSocketOption(SocketOptionLevel.Socket,
! SocketOptionName.Linger,
! value);
! }
! }
!
! public System.Net.Sockets.LingerOption NoDelay
! {
! get
! {
! return
(LingerOption)sock.GetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.NoDelay);
! }
! set
! {
! sock.SetSocketOption(SocketOptionLevel.Socket,
! SocketOptionName.NoDelay,
! value);
! }
! }
!
! public int ReceiveBufferSize
! {
! get
! {
! return
(int)sock.GetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.ReceiveBuffer);
! }
! set
! {
! sock.SetSocketOption(SocketOptionLevel.Socket,
! SocketOptionName.ReceiveBuffer,
! value);
! }
! }
!
! public int ReceiveTimeout
! {
! get
! {
! return
(int)sock.GetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.ReceiveTimeout);
! }
! set
! {
! sock.SetSocketOption(SocketOptionLevel.Socket,
! SocketOptionName.ReceiveTimeout,
! value);
! }
! }
!
! public int SendBufferSize
! {
! get
! {
! return
(int)sock.GetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.SendBuffer);
! }
! set
! {
! sock.SetSocketOption(SocketOptionLevel.Socket,
! SocketOptionName.SendBuffer,
! value);
! }
! }
!
! public int SendTimeout
! {
! get
! {
! return
(int)sock.GetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.SendTimeout);
! }
! set
! {
! sock.SetSocketOption(SocketOptionLevel.Socket,
! SocketOptionName.SendTimeout,
! value);
! }
! }
!
! }
- }
#endif // !ECMA_COMPAT
--- 20,323 ----
*/
namespace System.Net.Sockets
{
! #if !ECMA_COMPAT
!
! using System;
! using System.IO;
!
! public class TcpClient : IDisposable
{
! // Internal state.
! private Socket client;
! private NetworkStream stream;
! private bool active;
! // Constructors.
! public TcpClient()
! {
! Initialize(null, null);
! }
! public TcpClient(IPEndPoint localEP)
! {
! if(localEP == null)
! {
! throw new
ArgumentNullException("localEP");
! }
! Initialize(localEP, null);
! }
! public TcpClient(String hostname, int port)
! {
! Initialize(null, Lookup(hostname, port));
! }
! internal TcpClient(Socket client)
! {
! this.client = client;
! this.stream = null;
! this.active = true;
! }
!
! // Destructor.
! ~TcpClient()
! {
! Dispose(false);
! }
!
! // Initialize this object with a new TCP socket, optionally bind
! // to a local end-point, and optionally connect to a remote
! // end-point. If anything fails, the object will be left in a
! // clean state, with the socket handle closed.
! private void Initialize(IPEndPoint localEP, IPEndPoint remoteEP)
! {
! client = new Socket(AddressFamily.InterNetwork,
!
SocketType.Stream, ProtocolType.Tcp);
! stream = null;
! active = false;
! try
! {
! if(localEP != null)
! {
! client.Bind(localEP);
! }
! if(remoteEP != null)
! {
! client.Connect(remoteEP);
! active = true;
! }
! }
! catch(SocketException)
! {
! // We weren't able to bind or connect,
so clean up the
! // socket on our way back up the stack.
! client.Close();
! client = null;
! throw;
! }
! }
!
! // Implement the IDisposable interface.
! void IDisposable.Dispose()
! {
! Dispose(true);
! GC.SuppressFinalize(this);
! }
+ // Close this TCP client object.
public void Close()
! {
! Dispose(true);
! GC.SuppressFinalize(this);
! }
!
! // Connect to a remote end-point.
! public void Connect(IPEndPoint remoteEP)
! {
! if(client == null)
! {
! throw new ObjectDisposedException
! (S._("Exception_Disposed"));
! }
! if(remoteEP == null)
! {
! throw new
ArgumentNullException("remoteEP");
! }
! client.Connect(remoteEP);
! active = true;
! }
! public void Connect(String hostname, int port)
! {
! Connect(Lookup(hostname, port));
! }
! public void Connect(IPAddress address, int port)
! {
! Connect(new IPEndPoint(address, port));
! }
+ // Dispose of this object.
protected virtual void Dispose(bool disposing)
! {
! // Close the client socket if we haven't yet
attached a
! // "NetworkStream" object to it.
! if(stream == null && client != null)
! {
! client.Close();
! }
!
! // Clear the internal state.
! client = null;
! stream = null;
! active = false;
! }
!
! // Get a network stream for accessing the client socket.
! public NetworkStream GetStream()
! {
! if(client == null)
! {
! throw new ObjectDisposedException
! (S._("Exception_Disposed"));
! }
! if(!(client.Connected))
! {
! throw new InvalidOperationException
! (S._("IO_SocketNotConnected"));
! }
! if(stream == null)
! {
! stream = new NetworkStream
! (client, FileAccess.ReadWrite,
false);
! }
! return stream;
! }
!
! // Get or set a value that indicates if there is an active connection.
! protected bool Active
! {
! get
! {
! return active;
! }
! set
! {
! active = value;
! }
! }
!
! // Get or set the socket used by this client object.
! protected Socket Client
! {
! get
! {
! return client;
! }
! set
! {
! client = value;
! }
! }
!
! // Get or set the linger state.
! public LingerOption LingerState
! {
! get
! {
! return
(LingerOption)(client.GetSocketOption
! (SocketOptionLevel.Socket,
! SocketOptionName.Linger));
! }
! set
! {
!
client.SetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.Linger,
!
value);
! }
! }
!
! // Get or set the no-delay flag.
! public bool NoDelay
! {
! get
! {
! return (((int)(client.GetSocketOption
! (SocketOptionLevel.Tcp,
! SocketOptionName.NoDelay))) !=
0);
! }
! set
! {
!
client.SetSocketOption(SocketOptionLevel.Tcp,
!
SocketOptionName.NoDelay,
!
value ? 1 : 0);
! }
! }
!
! // Get or set the receive buffer size.
! public int ReceiveBufferSize
! {
! get
! {
! return (int)(client.GetSocketOption
! (SocketOptionLevel.Socket,
!
SocketOptionName.ReceiveBuffer));
! }
! set
! {
!
client.SetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.ReceiveBuffer,
!
value);
! }
! }
!
! // Get or set the receive timeout value.
! public int ReceiveTimeout
! {
! get
! {
! return (int)(client.GetSocketOption
! (SocketOptionLevel.Socket,
!
SocketOptionName.ReceiveTimeout));
! }
! set
! {
!
client.SetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.ReceiveTimeout,
!
value);
! }
! }
!
! // Get or set the send buffer size.
! public int SendBufferSize
! {
! get
! {
! return (int)(client.GetSocketOption
! (SocketOptionLevel.Socket,
! SocketOptionName.SendBuffer));
! }
! set
! {
!
client.SetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.SendBuffer,
!
value);
! }
! }
!
! // Get or set the send timeout value.
! public int SendTimeout
! {
! get
! {
! return (int)(client.GetSocketOption
! (SocketOptionLevel.Socket,
! SocketOptionName.SendTimeout));
! }
! set
! {
!
client.SetSocketOption(SocketOptionLevel.Socket,
!
SocketOptionName.SendTimeout,
!
value);
! }
! }
!
! // Perform a lookup on a hostname and port number to
! // get a remote end-point.
! private static IPEndPoint Lookup(String hostname, int port)
! {
! if(hostname == null)
! {
! throw new
ArgumentNullException("hostname");
! }
! if(port < IPEndPoint.MinPort || port >
IPEndPoint.MaxPort)
! {
! throw new ArgumentOutOfRangeException
! ("port", S._("ArgRange_Port"));
! }
! IPAddress address =
Dns.Resolve(hostname).AddressList[0];
! return new IPEndPoint(address, port);
! }
! }; // class TcpClient
#endif // !ECMA_COMPAT
+
+ }; // namespace System.Net.Sockets
Index: TcpListener.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System/Net/Sockets/TcpListener.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** TcpListener.cs 28 Sep 2002 01:55:06 -0000 1.1
--- TcpListener.cs 3 Apr 2003 07:14:41 -0000 1.2
***************
*** 1,9 ****
/*
* TcpListener.cs - Implementation of the
! * "System.Net.Sockets.TcpListener" class.
*
! * Copyright (C) 2002 Francis Rogers.
! *
! * Contributed by Francis Rogers <address@hidden>
*
* This program is free software; you can redistribute it and/or modify
--- 1,7 ----
/*
* TcpListener.cs - Implementation of the
! * "System.Net.Sockets.TcpListener" class.
*
! * Copyright (C) 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
***************
*** 22,120 ****
*/
- // this is a non-ECMA class
- #if !ECMA_COMPAT
- using System;
- using System.Net;
- using System.Net.Sockets;
-
namespace System.Net.Sockets
{
public class TcpListener
{
! private static Socket sock;
! private IPEndPoint endpoint;
! private bool listening;
!
! public System.Net.Sockets.Socket AcceptSocket()
! {
! return sock.Accept();
! }
!
! public System.Net.Sockets.TcpClient AcceptTcpClient()
! {
! Socket newsock = sock.Accept();
! TcpClient tcpc = new TcpClient(newsock);
! return tcpc;
! }
public bool Pending()
! {
! return sock.Poll(0, SelectMode.SelectRead);
! }
!
! public TcpListener(int port)
! {
! sock = new Socket(AddressFamily.InterNetwork,
! SocketType.Stream, ProtocolType.Tcp);
! endpoint = new
IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0], port);
! sock.Bind(endpoint);
! listening = false;
! }
!
! public TcpListener(System.Net.IPAddress localaddr, int port)
! {
! sock = new Socket(AddressFamily.InterNetwork,
! SocketType.Stream,
! ProtocolType.Tcp);
! endpoint = new IPEndPoint(localaddr, port);
! sock.Bind(endpoint);
! listening = false;
! }
!
! public TcpListener(System.Net.IPEndPoint localEP)
! {
! sock = new Socket(AddressFamily.InterNetwork,
! SocketType.Stream,
! ProtocolType.Tcp);
! sock.Bind(localEP);
! listening = false;
! }
public void Start()
! {
! sock.Listen((int)SocketOptionName.MaxConnections);
! }
public void Stop()
! {
! sock.Close();
! }
protected bool Active
! {
! get
! {
! return listening;
! }
! }
!
! public System.Net.EndPoint LocalEndpoint
! {
! get
! {
! return endpoint;
! }
! }
!
! protected System.Net.Sockets.Socket Server
! {
! get
! {
! return sock;
! }
! }
! }
- }
#endif // !ECMA_COMPAT
--- 20,191 ----
*/
namespace System.Net.Sockets
{
+ #if !ECMA_COMPAT
+
+ using System;
+
public class TcpListener
{
! // Internal state.
! private Socket server;
! private IPEndPoint serverEP;
!
! // Constructors.
! public TcpListener(IPEndPoint localEP)
! {
! if(localEP == null)
! {
! throw new
ArgumentNullException("localEP");
! }
! server = null;
! serverEP = localEP;
! }
! public TcpListener(IPAddress localaddr, int port)
! : this(new IPEndPoint(localaddr, port))
! {
! // Nothing to do here.
! }
! public TcpListener(int port)
! : this(new IPEndPoint(IPAddress.Any, port))
! {
! // Nothing to do here.
! }
!
! // Destructor.
! ~TcpListener()
! {
! Stop();
! }
!
! // Accept the next incoming connection on a listener and
! // return it as a raw socket with no "TcpClient" wrapper.
! public Socket AcceptSocket()
! {
! if(server == null)
! {
! throw new InvalidOperationException
!
(S._("Invalid_ServerNotCreated"));
! }
! else
! {
! return server.Accept();
! }
! }
!
! // Accept the next incoming connection on a listener and
! // wrap it in a "TcpClient" object.
! public TcpClient AcceptTcpClient()
! {
! if(server == null)
! {
! throw new InvalidOperationException
!
(S._("Invalid_ServerNotCreated"));
! }
! else
! {
! return new TcpClient(server.Accept());
! }
! }
+ // Determine if there is a pending connection on a listener.
public bool Pending()
! {
! if(server == null)
! {
! throw new InvalidOperationException
!
(S._("Invalid_ServerNotCreated"));
! }
! else
! {
! return server.Poll(0,
SelectMode.SelectRead);
! }
! }
+ // Start the listener.
public void Start()
! {
! if(server != null)
! {
! server = new
Socket(AddressFamily.InterNetwork,
!
SocketType.Stream,
!
ProtocolType.Tcp);
! try
! {
! // Attempt to reuse an existing
address, to prevent
! // delays in re-binding to the
same port after a
! // server crash.
! server.SetSocketOption
!
(SocketOptionLevel.Socket,
!
SocketOptionName.ReuseAddress, 1);
! }
! catch(SocketException)
! {
! // Don't get too concerned if
we cannot reuse.
! // It isn't serious - just
annoying.
! }
! try
! {
! server.Bind(serverEP);
! server.Listen(Int32.MaxValue);
! }
! catch(SocketException)
! {
! // Clean up the socket if the
bind or listen failed.
! server.Close();
! server = null;
! throw;
! }
! }
! }
+ // Stop the listener.
public void Stop()
! {
! if(server != null)
! {
! server.Close();
! server = null;
! }
! }
+ // Determine if the server socket is active.
protected bool Active
! {
! get
! {
! return (server != null);
! }
! }
!
! // Get the local end-point for the server.
! public EndPoint LocalEndpoint
! {
! get
! {
! if(server != null)
! {
! return server.LocalEndPoint;
! }
! else
! {
! return serverEP;
! }
! }
! }
!
! // Get the socket that is acting as the server.
! protected Socket Server
! {
! get
! {
! return server;
! }
! }
!
! }; // class TcpListener
#endif // !ECMA_COMPAT
+
+ }; // namespace System.Net.Sockets
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-pnet-commits] CVS: pnetlib/System/Net/Sockets TcpClient.cs,1.4,1.5 TcpListener.cs,1.1,1.2,
Rhys Weatherley <address@hidden> <=