1 // System.Net.Sockets.TcpListener.cs
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Copyright (C) 2001, Phillip Pearson
9 // http://www.myelin.co.nz
11 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
12 // (c) 2004 Novell, Inc.
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 namespace System.Net.Sockets
42 /// A slightly more abstracted way to listen for incoming
43 /// network connections than a Socket.
45 public class TcpListener
56 /// Some code that is shared between the constructors.
58 private void Init (AddressFamily family, EndPoint ep)
61 server = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
66 /// Constructs a new TcpListener to listen on a specified port
68 /// <param name="port">The port to listen on, e.g. 80 if you
69 /// are a web server</param>
71 [Obsolete ("Use TcpListener (IPAddress address, int port) instead")]
73 public TcpListener (int port)
75 if (port < 0 || port > 65535)
76 throw new ArgumentOutOfRangeException ("port");
78 Init (AddressFamily.InterNetwork, new IPEndPoint (IPAddress.Any, port));
82 /// Constructs a new TcpListener with a specified local endpoint
84 /// <param name="local_end_point">The endpoint</param>
85 public TcpListener (IPEndPoint local_end_point)
87 if (local_end_point == null)
88 throw new ArgumentNullException ("local_end_point");
90 Init (local_end_point.AddressFamily, local_end_point);
94 /// Constructs a new TcpListener, listening on a specified port
95 /// and IP (for use on a multi-homed machine)
97 /// <param name="listen_ip">The IP to listen on</param>
98 /// <param name="port">The port to listen on</param>
99 public TcpListener (IPAddress listen_ip, int port)
101 if (listen_ip == null)
102 throw new ArgumentNullException ("listen_ip");
104 if (port < 0 || port > 65535)
105 throw new ArgumentOutOfRangeException ("port");
107 Init (listen_ip.AddressFamily, new IPEndPoint(listen_ip, port));
114 /// A flag that is 'true' if the TcpListener is listening,
115 /// or 'false' if it is not listening
117 protected bool Active
119 get { return active; }
123 /// The local end point
125 public EndPoint LocalEndpoint
129 return server.LocalEndPoint;
136 /// The listening socket
138 protected Socket Server
140 get { return server; }
147 /// Accepts a pending connection
149 /// <returns>A Socket object for the new connection</returns>
150 public Socket AcceptSocket ()
153 throw new InvalidOperationException ("Socket is not listening");
155 return server.Accept();
159 /// Accepts a pending connection
161 /// <returns>A TcpClient
162 /// object made from the new socket.</returns>
163 public TcpClient AcceptTcpClient ()
166 throw new InvalidOperationException ("Socket is not listening");
168 Socket clientSocket = server.Accept ();
170 TcpClient client = new TcpClient();
171 // use internal method SetTcpClient to make a
172 // client with the specified socket
173 client.SetTcpClient (clientSocket);
179 /// Destructor - stops the listener listening
188 /// Returns 'true' if there is a connection waiting to be accepted
189 /// with AcceptSocket() or AcceptTcpClient().
191 public bool Pending ()
194 throw new InvalidOperationException ("Socket is not listening");
196 return server.Poll(0, SelectMode.SelectRead);
200 /// Tells the TcpListener to start listening.
208 throw new InvalidOperationException("Invalid server socket");
210 server.Bind (savedEP);
212 // MS: sets Listen to Int32.MaxValue
214 // According to the man page some BSD and BSD-derived
215 // systems limit the backlog to 5. This should really be
216 // configurable though
222 /// Tells the TcpListener to stop listening and dispose
223 /// of all managed resources.
233 Init (AddressFamily.InterNetwork, savedEP);