private void bConnect_Click(object sender, RoutedEventArgs e) { if (String.IsNullOrEmpty(ClientName.Text.Trim()) || String.IsNullOrEmpty(ClientConnectIp.Text.Trim()) || String.IsNullOrEmpty(ClientConnectPort.Text.Trim())) { MessageBox.Show("Какое-то из полей пустое!"); } else { try { Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ClientConnectIp.Text.Trim()), Convert.ToInt32(ClientConnectPort.Text.Trim())); socket.Connect(ip); string name = ClientName.Text.Trim(); SocketFunctions.SendString(socket, name); MainWindow window = new MainWindow(socket); window.Show(); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void bSendMessage_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(ClientMessage.Text.Trim())) { SocketFunctions.SendInt(socket, 0); SocketFunctions.SendString(socket, ClientMessage.Text.Trim()); ClientMessage.Text = ""; } }
async Task GetMessageAsync() { while (true) { await Task.Delay(500); string x = await Task.Run(() => { while (true) { if (SocketFunctions.RecieveInt(socket) == 0) { break; } } return(SocketFunctions.RecieveString(socket)); }); string[] msg = x.Split(new[] { "%&%" }, StringSplitOptions.None); AllMessages.AppendText(msg[0] + " : " + msg[1] + " " + Environment.NewLine); } }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { SocketFunctions.SendInt(socket, 1); }