private void Connect() { try { IPEndPoint ipPoint = null; try { ipPoint = new IPEndPoint(IPAddress.Parse(textBox1.Text), port); } catch (Exception ex) { MessageBox.Show("Connection error. Wrong IP address\n" + ex.Message); } if (ipPoint != null) { CClientSocket socket = new CClientSocket(); CPool cPool = new CPool(socket); cPool.Init(ipPoint); cPool.Send(textBoxString.Text); richTextBox1.AppendText(cPool.ProcessRead() + "\n"); } } catch (Exception ex) { richTextBox1.AppendText("Communication error" + ex.Message + "\n"); } }
void Connect() { int port = 8005; Log("Getting machine name..."); string host = Dns.GetHostName(); Log("Machine name " + host); Log("Getting ip-address... "); IPAddress IP = Dns.GetHostAddresses(host)[1]; Log("IP addresses: " + IP.ToString()); Log("Getting address for starting socket..."); IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(IP.ToString()), port); Log("Address: " + ipPoint); var listenSocket = new CListeningSocket(); try { CPool cPool = new CPool(listenSocket); listenSocket.Bind(ipPoint); listenSocket.Listen(10); label2.Invoke(new Action(() => { label2.Text = "Server has been started"; })); Log(IP.ToString() + "Server started. Waiting for client connection..."); while (true) { var res = cPool.ProcessAccept(); Log($"Received message: {res.StringResult}"); //Sending to client cPool.Send($"We received message {res.StringResult} from you. You are very COOOOl", res.handler); cPool.ProcessClose(res.Item2); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }