1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// System.Net.Sockets.TcpClient.cs
//
// Author:
// Phillip Pearson (pp@myelin.co.nz)
// Gonzalo Paniagua Javier (gonzalo@novell.com)
//
// Copyright (C) 2001, Phillip Pearson
// http://www.myelin.co.nz
// Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Net;
namespace System.Net.Sockets
{
public class TcpClient : IDisposable {
enum Properties : uint {
LingerState = 1,
NoDelay = 2,
ReceiveBufferSize = 4,
ReceiveTimeout = 8,
SendBufferSize = 16,
SendTimeout = 32
}
// private data
NetworkStream stream;
bool active;
Socket client;
bool disposed;
Properties values;
int recv_timeout, send_timeout;
int recv_buffer_size, send_buffer_size;
LingerOption linger_state;
bool no_delay;
private void Init (AddressFamily family)
{
active = false;
if(client != null) {
client.Close();
client = null;
}
client = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
}
public TcpClient ()
{
Init(AddressFamily.InterNetwork);
client.Bind(new IPEndPoint(IPAddress.Any, 0));
}
#if NET_1_1
public TcpClient (AddressFamily family)
{
if (family != AddressFamily.InterNetwork &&
family != AddressFamily.InterNetworkV6) {
throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
}
Init (family);
client.Bind (new IPEndPoint (IPAddress.Any, 0));
}
#endif
public TcpClient (IPEndPoint local_end_point)
{
Init(local_end_point.AddressFamily);
client.Bind(local_end_point);
}
public TcpClient (string hostname, int port)
{
Connect(hostname, port);
}
protected bool Active {
get { return active; }
set { active = value; }
}
#if NET_2_0
public Socket Client {
#else
protected Socket Client {
#endif
get { return client; }
set {
client = value;
stream = null;
}
}
#if NET_2_0
public int Available {
get { return client.Available; }
}
public bool Connected {
get { return client.Connected; }
}
public bool ExclusiveAddressUse {
get {
return !((bool) client.GetSocketOption (SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress));
}
set {
client.SetSocketOption (SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, !value);
}
}
#endif
internal void SetTcpClient (Socket s)
{
Client = s;
}
public LingerOption LingerState {
get {
if ((values & Properties.LingerState) != 0)
return linger_state;
return (LingerOption) client.GetSocketOption (SocketOptionLevel.Socket,
SocketOptionName.Linger);
}
set {
if (!client.Connected) {
linger_state = value;
values |= Properties.LingerState;
return;
}
client.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.Linger, value);
}
}
public bool NoDelay {
get {
if ((values & Properties.NoDelay) != 0)
return no_delay;
return (bool)client.GetSocketOption(
SocketOptionLevel.Tcp,
SocketOptionName.NoDelay);
}
set {
if (!client.Connected) {
no_delay = value;
values |= Properties.NoDelay;
return;
}
client.SetSocketOption(
SocketOptionLevel.Tcp,
SocketOptionName.NoDelay, value ? 1 : 0);
}
}
public int ReceiveBufferSize {
get {
if ((values & Properties.ReceiveBufferSize) != 0)
return recv_buffer_size;
return (int)client.GetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer);
}
set {
if (!client.Connected) {
recv_buffer_size = value;
values |= Properties.ReceiveBufferSize;
return;
}
client.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer, value);
}
}
public int ReceiveTimeout {
get {
if ((values & Properties.ReceiveTimeout) != 0)
return recv_timeout;
return (int)client.GetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout);
}
set {
if (!client.Connected) {
recv_timeout = value;
values |= Properties.ReceiveTimeout;
return;
}
client.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, value);
}
}
public int SendBufferSize {
get {
if ((values & Properties.SendBufferSize) != 0)
return send_buffer_size;
return (int)client.GetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.SendBuffer);
}
set {
if (!client.Connected) {
send_buffer_size = value;
values |= Properties.SendBufferSize;
return;
}
client.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.SendBuffer, value);
}
}
public int SendTimeout {
get {
if ((values & Properties.SendTimeout) != 0)
return send_timeout;
return (int)client.GetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.SendTimeout);
}
set {
if (!client.Connected) {
send_timeout = value;
values |= Properties.SendTimeout;
return;
}
client.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, value);
}
}
// methods
public void Close ()
{
((IDisposable) this).Dispose ();
}
public void Connect (IPEndPoint remote_end_point)
{
try {
client.Connect(remote_end_point);
stream = new NetworkStream(client, true);
active = true;
} finally {
CheckDisposed ();
}
}
public void Connect (IPAddress address, int port)
{
Connect(new IPEndPoint(address, port));
}
void SetOptions ()
{
Properties props = values;
values = 0;
if ((props & Properties.LingerState) != 0)
LingerState = linger_state;
if ((props & Properties.NoDelay) != 0)
NoDelay = no_delay;
if ((props & Properties.ReceiveBufferSize) != 0)
ReceiveBufferSize = recv_buffer_size;
if ((props & Properties.ReceiveTimeout) != 0)
ReceiveTimeout = recv_timeout;
if ((props & Properties.SendBufferSize) != 0)
SendBufferSize = send_buffer_size;
if ((props & Properties.SendTimeout) != 0)
SendTimeout = send_timeout;
}
public void Connect (string hostname, int port)
{
CheckDisposed ();
IPHostEntry host = Dns.GetHostByName(hostname);
for(int i=0; i<host.AddressList.Length; i++) {
try {
Init(host.AddressList[i].AddressFamily);
if(host.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
client.Bind(new IPEndPoint(IPAddress.Any, 0));
#if NET_1_1
else if(host.AddressList[i].AddressFamily == AddressFamily.InterNetworkV6)
client.Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
#endif
Connect(new IPEndPoint(host.AddressList[i], port));
if (values != 0)
SetOptions ();
break;
} catch (Exception e) {
if(client != null) {
client.Close();
client = null;
}
/// This is the last known address, re-throw the exception
if(i == host.AddressList.Length-1)
throw e;
}
}
}
void IDisposable.Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (disposed)
return;
disposed = true;
if (disposing){
// release managed resources
NetworkStream s = stream;
stream = null;
if (s != null) {
// This closes the socket as well, as the NetworkStream
// owns the socket.
s.Close();
active = false;
s = null;
} else if (client != null){
client.Close ();
}
client = null;
}
}
~TcpClient ()
{
Dispose (false);
}
public NetworkStream GetStream()
{
try {
if (stream == null)
stream = new NetworkStream (client, true);
return stream;
}
finally { CheckDisposed (); }
}
private void CheckDisposed ()
{
if (disposed)
throw new ObjectDisposedException (GetType().FullName);
}
}
}
|