public static void Receive(Socket client, Node node) { try { // Create the state object. StateObject state = new StateObject(); state.workSocket = client; state.node = node; // Begin receiving the data from the remote device. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { node.FailedToSendToServer(null); Console.WriteLine("Receive Exception: " + e.Message + "\nAttempting to reconnect"); } }
public static void StartClient(Int32 port, IPAddress ipAddress, String message, Node node) { // Connect to a remote device. Socket client = null; try { // Establish the remote endpoint for the socket. // The name of the // remote device is "host.contoso.com". //IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com"); //IPAddress ipAddress = ipHostInfo.AddressList[0]; //IPAddress ipAddress = getIPAddressOfTheLocalMachine(); IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); StateObject state = new StateObject(); state.workSocket = client; state.node = node; // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), state); connectDone.WaitOne(); // Send test data to the remote device. Send(client, message, node); //wyjebane <EOF> sendDone.WaitOne(); // Receive the response from the remote device. Receive(client, node); receiveDone.WaitOne(); // Write the response to the console. //Console.WriteLine("Response received : {0}", response); Console.ReadLine(); // Release the socket. client.Shutdown(SocketShutdown.Both); client.Close(); } catch (SocketException e) { Console.WriteLine("StartClient Exception: " + e.ToString() + "\nAttempting to reconnect"); if (client != null) { //Console.WriteLine("Weszłem"); //client.Shutdown(SocketShutdown.Both); //client.Close(); } node.FailedToSendToServer(message); } catch (Exception e) { Console.WriteLine("StartClient General Exception: " + e.ToString()+"\n"); } }