public static void Open() { //Thread.Sleep(3000); // Connect to a remote device. try { // Establish the remote endpoint for the socket. // The name of the // remote device is "host.contoso.com". IPHostEntry ipHostInfo = Dns.Resolve("127.0.0.1"); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); // Send test data to the remote device. //var handshake = "GET / HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: p2z/MFplfpRzjsVywqRQTg==\r\nHost: echo.websocket.org\r\nOrigin: http://echo.websocket.org/\r\n\r\n"; var handshake = "GET / HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: p2z/MFplfpRzjsVywqRQTg==\r\nHost: localhost\r\nOrigin: http://localhost/\r\n\r\n"; Send(client, handshake); sendDone.WaitOne(); statusProvider = StatusSocket.Opened; // Receive the response from the remote device. Receive(client); //receiveDone.WaitOne(); // Write the response to the console. ////Console.WriteLine("Response received : {0}", response); ////// Release the socket. ////client.Shutdown(SocketShutdown.Both); ////client.Close(); //while (true) //{ // Send(Guid.NewGuid().ToString()); // Thread.Sleep(5000); //} } catch (Exception e) { ////Console.WriteLine(e.ToString()); } }
private static void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket)ar.AsyncState; // Complete the connection. client.EndConnect(ar); //Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); // Signal that the connection has been made. connectDone.Set(); statusProvider = StatusSocket.Connected; } catch (Exception e) { //Console.WriteLine(e.ToString()); } }