I have wrote a client server program using C#, the programm runs on the same machine, the client sends a message and the server displays te message, how can i edit my program so that both server and client run on different machines, and also that 10 clients can connect to the server
Here is my Client Code:
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)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the local host
clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, PORT));
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
EnableSendButton();
}
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: http://stackoverflow.com/questions/1047662/what-is-asynccallback
void ReceiveCallback(IAsyncResult AR)
{
try
{
int bytesReceived = clientSocket.EndReceive(AR); // Number of bytes received
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); }
}
}
and the server code:
public partial class ServerForm : Form
{
const int BUFFER_SIZE = 1024;
byte[] rcvBuffer = new byte[BUFFER_SIZE]; // To recieve data from the client
Socket serverSocket;
Socket clientSocket; // We will only accept one client socket for now
const int PORT = 3333;
public ServerForm()
{
InitializeComponent();
StartServer();
}
void StartServer()
{
try
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT));
serverSocket.Listen(10); // Listen(1) means a maximum of one client pending
serverSocket.BeginAccept(AcceptCallback, null);
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
}
// About Asynchronous Callbacks: http://stackoverflow.com/questions/1047662/what-is-asynccallback
void AcceptCallback(IAsyncResult AR)
{
try
{
clientSocket = serverSocket.EndAccept(AR); // Client connected successfully, waiting for requests
AppendToTextBox("Client connected successfully...");
clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
}
void ReceiveCallback(IAsyncResult AR)
{
try
{
int bytesReceived = clientSocket.EndReceive(AR); // Number of bytes received
byte[] rcvBufferTrim = new byte[bytesReceived];
Array.Copy(rcvBuffer, rcvBufferTrim, bytesReceived); // Removes trailing nulls
string text = Encoding.ASCII.GetString(rcvBufferTrim); // Convert bytes into string
AppendToTextBox("Received Text: " + text);
if (text == "getdate")
text = "\t"+ System.DateTime.Now.ToString("dd.MM.yyyy");
else if (text == "gettime")
text = "\t"+ System.DateTime.Now.ToString("h:mm:ss ");
///////////////////////////////////////////////////////////////////////////////////
// Reply with echo
byte[] echoBuffer = Encoding.ASCII.GetBytes("Echo... " + text);
clientSocket.Send(echoBuffer);
// Starts receiving data again
clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);
}
catch (Exception ex) { AppendToTextBox("Exception: " + ex.Message + "..."); }
}
// Provides a thread-safe way to append text to the textbox
void AppendToTextBox(string text)
{
MethodInvoker invoker = new MethodInvoker(delegate
{
textBox.Text += text + "\r\n\r\n";
textBox.SelectionStart = textBox.TextLength;
textBox.ScrollToCaret();
}); // "\r\n" are for new lines
this.Invoke(invoker);
}
}