public SocketClient CreateClient(IPEndPoint localep, IPEndPoint remoteep, bool bbeginread, SocketCreator creator) { //Creates the Socket for sending data over TCP. Socket s = null; if (remoteep.Address.AddressFamily != AddressFamily.InterNetworkV6) { s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else { s = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); } // Connects to the host using IPEndPoint. try { LogMessage(MessageImportance.Highest, "EXCEPTION", string.Format("Attempting to create TCP connection from {0} to {1}", localep, remoteep)); s.Bind(localep); s.Connect(remoteep); } catch (SocketException e) { LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Bind or Connect, Winsock Native: {0}, Socket: {1}\n{2}", e.NativeErrorCode, e.SocketErrorCode, e)); return(null); } if (!s.Connected) { LogError(MessageImportance.Highest, "Connection", "Failed to connect to " + remoteep); return(null); } SocketClient client = creator.CreateSocket(s, this); if (bbeginread) { client.DoAsyncRead(); } return(client); }
public SocketClient CreateClient(IPEndPoint ep, bool bbeginread, SocketCreator creator, bool IsIPV6) { //Creates the Socket for sending data over TCP. Socket s = null; if (IsIPV6 == false) { s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else { s = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); } // Connects to the host using IPEndPoint. try { s.Connect(ep); } catch (SocketException e) { LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Connect, Winsock Native: {0}, Socket: {1}\n{2}", e.NativeErrorCode, e.SocketErrorCode, e)); return(null); } if (!s.Connected) { LogError(MessageImportance.Highest, "Connection", "Failed to connect to " + ep.Address.ToString() + " port " + ep.Port); return(null); } SocketClient client = creator.CreateSocket(s, this); if (bbeginread) { client.DoAsyncRead(); } return(client); }
internal void OnAcceptReceived(IAsyncResult ar) { System.Net.Sockets.Socket sman = (System.Net.Sockets.Socket)ar.AsyncState; if (sman != null) { System.Net.Sockets.Socket newsocket = null; try { newsocket = sman.EndAccept(ar); } catch (System.ObjectDisposedException e) { string strError = string.Format("Exception calling EndAccept {0}", e); LogError(MessageImportance.Highest, "EXCEPTION", strError); return; } catch (System.Exception eall) { string strError = string.Format("Exception calling EndAccept - continueing {0}", eall); LogError(MessageImportance.Highest, "EXCEPTION", strError); } /// Start a new accept try { sman.BeginAccept(AcceptCallback, sman); } catch (SocketException e3) /// winso { string strError = string.Format("Exception calling BeginAccept2 {0}", e3); LogError(MessageImportance.Highest, "EXCEPTION", strError); } catch (ObjectDisposedException e4) // socket was closed { string strError = string.Format("Exception calling BeginAccept2 {0}", e4); LogError(MessageImportance.Highest, "EXCEPTION", strError); } catch (System.Exception eall) { string strError = string.Format("Exception calling BeginAccept2 {0}", eall); LogError(MessageImportance.Highest, "EXCEPTION", strError); } if (newsocket == null) { return; } LogMessage(MessageImportance.Medium, "NEWCON", string.Format("Accepted new socket {0}", newsocket.Handle)); /// look up the creator for this socket /// SocketCreator creator = (SocketCreator)this.m_SocketSet[sman]; if (creator != null) { SocketClient newclient = creator.AcceptSocket(newsocket, this.m_ConMgrParent); try { m_ConMgrParent.FireAcceptHandler(newclient); } catch (System.Exception efire) { string strError = string.Format("Exception Fireing Acception Handler -{0}", efire); LogError(MessageImportance.Highest, "EXCEPTION", strError); } newclient.DoAsyncRead(); } } }
void Listener_OnNewConnection(Socket s) { IncomingClient = new SocketClient(s, null); IncomingClient.ReceiveHandlerBytes += new SocketClient.SocketReceiveHandler(IncomingClient_ReceiveHandlerBytes); IncomingClient.DisconnectHandler += new SocketClient.SocketEventHandler(IncomingClient_DisconnectHandler); IncomingClient.DoAsyncRead(); RemoteClient.DoAsyncRead(); }