public static ISocket UdpConnect(int port) { var udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); var localEndpoint = new IPEndPoint(IPAddress.Any, port); udpSocket.Bind(localEndpoint); return(AwesomeSocket.New(udpSocket)); }
public static ISocket TcpListen(int port, int backlog = 10) { var listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var ip = new IPAddress(new byte[] { 0, 0, 0, 0 }); var localEndPoint = new IPEndPoint(ip, port); listenSocket.Bind(localEndPoint); listenSocket.Listen(backlog); return(AwesomeSocket.New(listenSocket)); }
public static ISocket TcpAccept(ISocket listenSocket, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func <ISocket, Exception, Socket> callback = null) { if (type == SocketCommunicationTypes.Blocking) { return(AwesomeSocket.New(listenSocket.GetSocket().Accept())); } if (callback == null) { throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback")); } new Thread(() => TcpAcceptThread(listenSocket, callback)).Start(); return(null); }
private static void TcpAcceptThread(ISocket listenSocket, Func <ISocket, Exception, Socket> callback) { Socket clientSocket = null; try { clientSocket = listenSocket.GetSocket().Accept(); } catch (Exception ex) { callback(null, ex); } callback(AwesomeSocket.New(clientSocket), null); }
public static ISocket TcpConnect(string ipAddress, int port, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func <ISocket, Exception, Socket> callback = null) { var connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var ip = IPAddress.Parse(ipAddress); var remoteEndpoint = new IPEndPoint(ip, port); if (type == SocketCommunicationTypes.Blocking) { connectSocket.Connect(remoteEndpoint); return(AwesomeSocket.New(connectSocket)); } if (callback == null) { throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback")); } new Thread(() => TcpConnectThread(AwesomeSocket.New(connectSocket), remoteEndpoint, callback)).Start(); return(null); }