1 // System.Net.Sockets.TcpListener.cs
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Sridhar Kulkarni (sridharkulkarni@gmail.com)
9 // Copyright (C) 2001, Phillip Pearson
10 // http://www.myelin.co.nz
12 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 // (c) 2004-2006 Novell, Inc.
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 namespace System.Net.Sockets
43 /// A slightly more abstracted way to listen for incoming
44 /// network connections than a Socket.
46 public class TcpListener
57 /// Some code that is shared between the constructors.
59 private void Init (AddressFamily family, EndPoint ep)
62 server = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
67 /// Constructs a new TcpListener to listen on a specified port
69 /// <param name="port">The port to listen on, e.g. 80 if you
70 /// are a web server</param>
72 [Obsolete ("Use TcpListener (IPAddress address, int port) instead")]
74 public TcpListener (int port)
76 if (port < 0 || port > 65535)
77 throw new ArgumentOutOfRangeException ("port");
79 Init (AddressFamily.InterNetwork, new IPEndPoint (IPAddress.Any, port));
83 /// Constructs a new TcpListener with a specified local endpoint
85 /// <param name="local_end_point">The endpoint</param>
86 public TcpListener (IPEndPoint local_end_point)
88 if (local_end_point == null)
89 throw new ArgumentNullException ("local_end_point");
91 Init (local_end_point.AddressFamily, local_end_point);
95 /// Constructs a new TcpListener, listening on a specified port
96 /// and IP (for use on a multi-homed machine)
98 /// <param name="listen_ip">The IP to listen on</param>
99 /// <param name="port">The port to listen on</param>
100 public TcpListener (IPAddress listen_ip, int port)
102 if (listen_ip == null)
103 throw new ArgumentNullException ("listen_ip");
105 if (port < 0 || port > 65535)
106 throw new ArgumentOutOfRangeException ("port");
108 Init (listen_ip.AddressFamily, new IPEndPoint(listen_ip, port));
115 /// A flag that is 'true' if the TcpListener is listening,
116 /// or 'false' if it is not listening
118 protected bool Active
120 get { return active; }
124 /// The local end point
126 public EndPoint LocalEndpoint
130 return server.LocalEndPoint;
137 /// The listening socket
146 get { return server; }
151 /// Specifies whether the TcpListener allows only one
152 /// underlying socket to listen to a specific port
155 [MonoNotSupported ("Not supported as Socket.ExclusiveAddressUse is not supported")]
157 public bool ExclusiveAddressUse
160 if (server == null) {
161 throw new ObjectDisposedException (GetType ().ToString ());
164 throw new InvalidOperationException ("The TcpListener has been started");
167 return(server.ExclusiveAddressUse);
170 if (server == null) {
171 throw new ObjectDisposedException (GetType ().ToString ());
174 throw new InvalidOperationException ("The TcpListener has been started");
177 server.ExclusiveAddressUse = value;
185 /// Accepts a pending connection
187 /// <returns>A Socket object for the new connection</returns>
188 public Socket AcceptSocket ()
191 throw new InvalidOperationException ("Socket is not listening");
193 return server.Accept();
197 /// Accepts a pending connection
199 /// <returns>A TcpClient
200 /// object made from the new socket.</returns>
201 public TcpClient AcceptTcpClient ()
204 throw new InvalidOperationException ("Socket is not listening");
206 Socket clientSocket = server.Accept ();
208 TcpClient client = new TcpClient();
209 // use internal method SetTcpClient to make a
210 // client with the specified socket
211 client.SetTcpClient (clientSocket);
217 /// Destructor - stops the listener listening
226 /// Returns 'true' if there is a connection waiting to be accepted
227 /// with AcceptSocket() or AcceptTcpClient().
229 public bool Pending ()
232 throw new InvalidOperationException ("Socket is not listening");
234 return server.Poll(0, SelectMode.SelectRead);
238 /// Tells the TcpListener to start listening.
242 // MS: sets Listen to Int32.MaxValue
244 // According to the man page some BSD and BSD-derived
245 // systems limit the backlog to 5. This should really be
246 // configurable though
250 /// Tells the TcpListener to start listening for max number
251 /// of pending connections.
259 void Start (int backlog)
264 if (server == null) {
265 throw new InvalidOperationException ("Invalid server socket");
268 server.Bind (savedEP);
269 server.Listen (backlog);
274 public IAsyncResult BeginAcceptSocket (AsyncCallback callback,
277 if (server == null) {
278 throw new ObjectDisposedException (GetType ().ToString ());
281 return(server.BeginAccept (callback, state));
284 public IAsyncResult BeginAcceptTcpClient (AsyncCallback callback, object state)
286 if (server == null) {
287 throw new ObjectDisposedException (GetType ().ToString ());
290 return(server.BeginAccept (callback, state));
293 public Socket EndAcceptSocket (IAsyncResult asyncResult)
295 return(server.EndAccept (asyncResult));
298 public TcpClient EndAcceptTcpClient (IAsyncResult asyncResult)
300 Socket clientSocket = server.EndAccept (asyncResult);
301 TcpClient client = new TcpClient ();
303 client.SetTcpClient (clientSocket);
310 /// Tells the TcpListener to stop listening and dispose
311 /// of all managed resources.
321 Init (AddressFamily.InterNetwork, savedEP);