private void btnSend_Click(object sender, EventArgs e) { string message = txtInput.Text; byte[] data = Encoding.ASCII.GetBytes(message); serverSocket.Send(data, data.Length); uMessageLine row = new uMessageLine(); row.SetWidth(this.Width - 65); row.SetMessageContent(txtInput.Text, userNickname); lbMain.Controls.Add(row); txtInput.Text = ""; if (!lbMain.AutoScroll) { if (GetContentHeight() > 170) { lbMain.AutoScroll = true; } } lbMain.ScrollControlIntoView(row); }
void ListenToServer() { IPEndPoint serverEndpoint = new IPEndPoint(IPAddress.Any, 0); while (true) { byte[] data = serverSocket.Receive(ref serverEndpoint); string str = Encoding.ASCII.GetString(data, 0, data.Length); uMessageLine row = new uMessageLine(); row.SetWidth(this.Width - 65); row.SetMessageContent(str, "Admin", false); if (this.InvokeRequired) { this.BeginInvoke((MethodInvoker) delegate() { lbMain.Controls.Add(row); if (!lbMain.AutoScroll) { if (GetContentHeight() > 170) { lbMain.AutoScroll = true; } } lbMain.ScrollControlIntoView(row); }); } else { lbMain.Controls.Add(row); if (!lbMain.AutoScroll) { if (GetContentHeight() > 170) { lbMain.AutoScroll = true; } } lbMain.ScrollControlIntoView(row); } } }
void ConnectToServer() { serverSocket = new UdpClient("127.0.0.1", 3000); IPEndPoint serverEndpoint = new IPEndPoint(IPAddress.Loopback, 3000); string str = userNickname; byte[] data = Encoding.ASCII.GetBytes(str); serverSocket.Send(data, data.Length); data = serverSocket.Receive(ref serverEndpoint); str = Encoding.ASCII.GetString(data, 0, data.Length); uMessageLine row = new uMessageLine(); row.SetWidth(this.Width - 65); row.SetMessageContent(str, "Admin", false); if (this.InvokeRequired) { this.BeginInvoke((MethodInvoker) delegate() { lbMain.Controls.Add(row); }); } else { lbMain.Controls.Add(row); } Thread listenToServer = new Thread(new ThreadStart(ListenToServer)); listenToServer.IsBackground = true; listenToServer.Start(); }