this is the client code ! You can try it .
multi client to server in C#
Collapse
X
-
Code:namespace Client { public partial class ClientForm : Form { const int BUFFER_SIZE = 1024; byte[] sendBuffer = new byte[BUFFER_SIZE]; byte[] rcvBuffer = new byte[BUFFER_SIZE]; Socket clientSocket; const int PORT = 3333; public ClientForm() { InitializeComponent(); } void btnConnect_Click(object sender, EventArgs e) { string serverIP = "192.168.1.200"; try { this.clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the local host clientSocket.Connect(new IPEndPoint(IPAddress.Parse(serverIP), PORT)); // Prepares to receive something, i.e. the echo clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null); EnableSendButton(); } catch (Exception ex) { AppendToTextBox(ex.Message); } } void btnSend_Click(object sender, EventArgs e) { try { // Serialize the textBox text before sending sendBuffer = Encoding.ASCII.GetBytes(textBoxInput.Text); // Sends contents of textbox to the server clientSocket.Send(sendBuffer); // Prepares to receive something, i.e. the echo clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);//.Receive(rcvBuffer); } catch (Exception ex) { AppendToTextBox(ex.Message); } } // About Asynchronous Callbacks: [url]http://stackoverflow.com/questions/1047662/what-is-asynccallback[/url] void ReceiveCallback(IAsyncResult AR) { try { int bytesReceived = clientSocket.EndReceive(AR); // Number of bytes received if (bytesReceived == 0) return; byte[] rcvBufferTrim = new byte[bytesReceived]; Array.Copy(rcvBuffer, rcvBufferTrim, bytesReceived); // Removes trailing nulls string text = Encoding.ASCII.GetString(rcvBufferTrim); // Convert bytes into string AppendToTextBox(DateTime.Now.ToString("HH:mm:ss") + ": " + text); // Displays buffer contents as text // Starts receiving data again clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null); } catch (Exception ex) { AppendToTextBox(ex.Message); } } // Provides a thread-safe way to enable the send button/disable the connect button void EnableSendButton() { MethodInvoker invoker = new MethodInvoker(delegate { btnSend.Enabled = true; btnConnect.Enabled = false; btnDisconnect.Enabled = true; textBoxInput.ReadOnly = false; btnDisconnect.Enabled = true; }); this.Invoke(invoker); } private void textBoxInput_KeyDown(object sender, KeyEventArgs e) { if (btnSend.Enabled == true && Control.ModifierKeys != Keys.Shift && e.KeyCode == Keys.Return) { MethodInvoker invoker = new MethodInvoker(delegate { btnSend.PerformClick(); }); this.Invoke(invoker); e.SuppressKeyPress = true; } } // Provides a thread-safe way to append text to the textbox void AppendToTextBox(string text) { MethodInvoker invoker = new MethodInvoker(delegate { textBoxDisplay.Text += text + "\r\n"; textBoxDisplay.SelectionStart = textBoxDisplay.TextLength; textBoxDisplay.ScrollToCaret(); }); // "\r\n" are for new lines try { this.Invoke(invoker); } catch { }; } private void btnDisconnect_Click(object sender, EventArgs e) { sendBuffer = Encoding.ASCII.GetBytes("disconnect"); clientSocket.Send(sendBuffer); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); this.Close(); // Close the window } private void textBoxDisplay_TextChanged(object sender, EventArgs e) { } } }Last edited by Rabbit; May 25 '14, 09:22 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.Comment
-
Don't forget this for client :
Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets;
Last edited by Rabbit; May 25 '14, 09:23 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.Comment
Comment