public void Connect(IPEndPoint localEP, IPEndPoint remoteEP, int bufferSize) { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { s.Bind(localEP); } catch (Exception ex) { s.Close(); Trace.WriteLine(string.Format("failed to bind to {0}\r\n{1}", localEP, ex)); throw new ApplicationException(string.Format("failed to bind to {0}", localEP), ex); } try { s.Connect(remoteEP); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, bufferSize * 1024); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, bufferSize * 1024); } catch (Exception ex) { s.Close(); Trace.WriteLine(string.Format("failed to connect to {0}\r\n{1}", remoteEP, ex)); throw new ApplicationException(string.Format("failed to connect to {0}", remoteEP), ex); } _direction = SocketConnectionDirection.Outgoing; _socket = s; _remoteEP = remoteEP; }
public AsyncSocketConnection(Socket socket, int bufferSize) { if (socket == null) { throw new ApplicationException("socket is null"); } _recvBuffer = new byte[bufferSize * 1024]; _evenArgs = new DataReceivedEventArgs(_recvBuffer); _direction = SocketConnectionDirection.Incoming; _socket = socket; _remoteEP = socket.RemoteEndPoint as IPEndPoint; }