move FrameworkName from corlib to System
[mcs.git] / class / System / System.Net.Sockets / TcpClient.cs
blob21c8ad58e8bde623354003fd0932899d254b79cf
1 // System.Net.Sockets.TcpClient.cs
2 //
3 // Author:
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Sridhar Kulkarni (sridharkulkarni@gmail.com)
7 //
8 // Copyright (C) 2001, Phillip Pearson
9 // http://www.myelin.co.nz
10 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System;
35 using System.Net;
37 namespace System.Net.Sockets
39 public class TcpClient : IDisposable {
40 enum Properties : uint {
41 LingerState = 1,
42 NoDelay = 2,
43 ReceiveBufferSize = 4,
44 ReceiveTimeout = 8,
45 SendBufferSize = 16,
46 SendTimeout = 32
49 // private data
50 NetworkStream stream;
51 bool active;
52 Socket client;
53 bool disposed;
54 Properties values;
55 int recv_timeout, send_timeout;
56 int recv_buffer_size, send_buffer_size;
57 LingerOption linger_state;
58 bool no_delay;
60 private void Init (AddressFamily family)
62 active = false;
64 if(client != null) {
65 client.Close();
66 client = null;
69 client = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
72 public TcpClient ()
74 Init(AddressFamily.InterNetwork);
75 client.Bind(new IPEndPoint(IPAddress.Any, 0));
78 #if NET_1_1
79 public TcpClient (AddressFamily family)
81 if (family != AddressFamily.InterNetwork &&
82 family != AddressFamily.InterNetworkV6) {
83 throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
86 Init (family);
87 IPAddress any = IPAddress.Any;
88 if (family == AddressFamily.InterNetworkV6)
89 any = IPAddress.IPv6Any;
90 client.Bind (new IPEndPoint (any, 0));
92 #endif
94 public TcpClient (IPEndPoint local_end_point)
96 Init(local_end_point.AddressFamily);
97 client.Bind(local_end_point);
100 public TcpClient (string hostname, int port)
102 Connect(hostname, port);
105 protected bool Active {
106 get { return active; }
107 set { active = value; }
110 #if NET_2_0
111 public Socket Client {
112 #else
113 protected Socket Client {
114 #endif
115 get { return client; }
116 set {
117 client = value;
118 stream = null;
122 #if NET_2_0
123 public int Available {
124 get { return client.Available; }
127 public bool Connected {
128 get { return client.Connected; }
131 #if TARGET_JVM
132 [MonoNotSupported ("Not supported as Socket.ExclusiveAddressUse is not supported")]
133 #endif
134 public bool ExclusiveAddressUse {
135 get {
136 return(client.ExclusiveAddressUse);
138 set {
139 client.ExclusiveAddressUse = value;
142 #endif
143 internal void SetTcpClient (Socket s)
145 Client = s;
148 public LingerOption LingerState {
149 get {
150 if ((values & Properties.LingerState) != 0)
151 return linger_state;
153 return (LingerOption) client.GetSocketOption (SocketOptionLevel.Socket,
154 SocketOptionName.Linger);
156 set {
157 if (!client.Connected) {
158 linger_state = value;
159 values |= Properties.LingerState;
160 return;
162 client.SetSocketOption(
163 SocketOptionLevel.Socket,
164 SocketOptionName.Linger, value);
168 public bool NoDelay {
169 get {
170 if ((values & Properties.NoDelay) != 0)
171 return no_delay;
173 return (bool)client.GetSocketOption(
174 SocketOptionLevel.Tcp,
175 SocketOptionName.NoDelay);
177 set {
178 if (!client.Connected) {
179 no_delay = value;
180 values |= Properties.NoDelay;
181 return;
183 client.SetSocketOption(
184 SocketOptionLevel.Tcp,
185 SocketOptionName.NoDelay, value ? 1 : 0);
189 public int ReceiveBufferSize {
190 get {
191 if ((values & Properties.ReceiveBufferSize) != 0)
192 return recv_buffer_size;
194 return (int)client.GetSocketOption(
195 SocketOptionLevel.Socket,
196 SocketOptionName.ReceiveBuffer);
198 set {
199 if (!client.Connected) {
200 recv_buffer_size = value;
201 values |= Properties.ReceiveBufferSize;
202 return;
204 client.SetSocketOption(
205 SocketOptionLevel.Socket,
206 SocketOptionName.ReceiveBuffer, value);
210 public int ReceiveTimeout {
211 get {
212 if ((values & Properties.ReceiveTimeout) != 0)
213 return recv_timeout;
215 return (int)client.GetSocketOption(
216 SocketOptionLevel.Socket,
217 SocketOptionName.ReceiveTimeout);
219 set {
220 if (!client.Connected) {
221 recv_timeout = value;
222 values |= Properties.ReceiveTimeout;
223 return;
225 client.SetSocketOption(
226 SocketOptionLevel.Socket,
227 SocketOptionName.ReceiveTimeout, value);
231 public int SendBufferSize {
232 get {
233 if ((values & Properties.SendBufferSize) != 0)
234 return send_buffer_size;
236 return (int)client.GetSocketOption(
237 SocketOptionLevel.Socket,
238 SocketOptionName.SendBuffer);
240 set {
241 if (!client.Connected) {
242 send_buffer_size = value;
243 values |= Properties.SendBufferSize;
244 return;
246 client.SetSocketOption(
247 SocketOptionLevel.Socket,
248 SocketOptionName.SendBuffer, value);
252 public int SendTimeout {
253 get {
254 if ((values & Properties.SendTimeout) != 0)
255 return send_timeout;
257 return (int)client.GetSocketOption(
258 SocketOptionLevel.Socket,
259 SocketOptionName.SendTimeout);
261 set {
262 if (!client.Connected) {
263 send_timeout = value;
264 values |= Properties.SendTimeout;
265 return;
267 client.SetSocketOption(
268 SocketOptionLevel.Socket,
269 SocketOptionName.SendTimeout, value);
274 // methods
276 public void Close ()
278 ((IDisposable) this).Dispose ();
281 public void Connect (IPEndPoint remote_end_point)
283 try {
284 client.Connect(remote_end_point);
285 active = true;
286 } finally {
287 CheckDisposed ();
291 public void Connect (IPAddress address, int port)
293 Connect(new IPEndPoint(address, port));
296 void SetOptions ()
298 Properties props = values;
299 values = 0;
301 if ((props & Properties.LingerState) != 0)
302 LingerState = linger_state;
303 if ((props & Properties.NoDelay) != 0)
304 NoDelay = no_delay;
305 if ((props & Properties.ReceiveBufferSize) != 0)
306 ReceiveBufferSize = recv_buffer_size;
307 if ((props & Properties.ReceiveTimeout) != 0)
308 ReceiveTimeout = recv_timeout;
309 if ((props & Properties.SendBufferSize) != 0)
310 SendBufferSize = send_buffer_size;
311 if ((props & Properties.SendTimeout) != 0)
312 SendTimeout = send_timeout;
315 public void Connect (string hostname, int port)
317 IPAddress [] addresses = Dns.GetHostAddresses (hostname);
318 Connect (addresses, port);
321 #if NET_2_0
322 public
323 #else
324 private
325 #endif
326 void Connect (IPAddress[] ipAddresses, int port)
328 CheckDisposed ();
330 if (ipAddresses == null) {
331 throw new ArgumentNullException ("ipAddresses");
334 for(int i = 0; i < ipAddresses.Length; i++) {
335 try {
336 IPAddress address = ipAddresses[i];
338 if (address.Equals (IPAddress.Any) ||
339 address.Equals (IPAddress.IPv6Any)) {
340 throw new SocketException ((int)SocketError.AddressNotAvailable);
343 Init (address.AddressFamily);
345 if (address.AddressFamily == AddressFamily.InterNetwork) {
346 client.Bind (new IPEndPoint (IPAddress.Any, 0));
347 #if NET_1_1
348 } else if (address.AddressFamily == AddressFamily.InterNetworkV6) {
349 client.Bind (new IPEndPoint (IPAddress.IPv6Any, 0));
350 #endif
351 } else {
352 throw new NotSupportedException ("This method is only valid for sockets in the InterNetwork and InterNetworkV6 families");
355 Connect (new IPEndPoint (address, port));
357 if (values != 0) {
358 SetOptions ();
361 break;
362 } catch (Exception e) {
363 /* Reinitialise the socket so
364 * other properties still work
365 * (see no-arg constructor)
367 Init (AddressFamily.InterNetwork);
369 /* This is the last known
370 * address, so re-throw the
371 * exception
373 if (i == ipAddresses.Length - 1) {
374 throw e;
380 #if NET_2_0
381 public void EndConnect (IAsyncResult asyncResult)
383 client.EndConnect (asyncResult);
386 #if TARGET_JVM
387 [MonoNotSupported ("Not supported as Socket.BeginConnect is not supported")]
388 #endif
389 public IAsyncResult BeginConnect (IPAddress address, int port,
390 AsyncCallback callback,
391 object state)
393 return(client.BeginConnect (address, port, callback,
394 state));
397 #if TARGET_JVM
398 [MonoNotSupported ("Not supported as Socket.BeginConnect is not supported")]
399 #endif
400 public IAsyncResult BeginConnect (IPAddress[] addresses,
401 int port,
402 AsyncCallback callback,
403 object state)
405 return(client.BeginConnect (addresses, port, callback,
406 state));
409 #if TARGET_JVM
410 [MonoNotSupported ("Not supported as Socket.BeginConnect is not supported")]
411 #endif
412 public IAsyncResult BeginConnect (string host, int port,
413 AsyncCallback callback,
414 object state)
416 return(client.BeginConnect (host, port, callback,
417 state));
419 #endif
421 void IDisposable.Dispose ()
423 Dispose (true);
424 GC.SuppressFinalize (this);
427 protected virtual void Dispose (bool disposing)
429 if (disposed)
430 return;
431 disposed = true;
433 if (disposing) {
434 // release managed resources
435 NetworkStream s = stream;
436 stream = null;
437 if (s != null) {
438 // This closes the socket as well, as the NetworkStream
439 // owns the socket.
440 s.Close();
441 active = false;
442 s = null;
443 } else if (client != null){
444 client.Close ();
445 client = null;
450 ~TcpClient ()
452 Dispose (false);
455 public NetworkStream GetStream()
457 try {
458 if (stream == null)
459 stream = new NetworkStream (client, true);
460 return stream;
462 finally { CheckDisposed (); }
465 private void CheckDisposed ()
467 if (disposed)
468 throw new ObjectDisposedException (GetType().FullName);