Reputation: 13
So this is code for my TCP based chat server written in c#. It works no problem for connecting two computers via ipv4 for chat, but I am wanting the server program to listen for and accept, then send a "sucessfully joined" message to the computers that joined.
I am wondering is there a way to change this code to do this?
Server code:
IPAddress ipAd = IPAddress.Parse(IPV4.Text); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
MessageBox.Show("The server is running at port 8001...");
MessageBox.Show("The local End point is :" + myList.LocalEndpoint);
MessageBox.Show("Looking for other computer");
Socket s = myList.AcceptSocket();
Console.WriteLine("Found buddy " + s.RemoteEndPoint);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(satt.Text));
MessageBox.Show("The message " + satt.Text + " was sent to the computer with IP address " + IPV4.Text);
byte[] b = new byte[100];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
/* clean up */
s.Close();
myList.Stop();
Client code:
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(RecieveIPAdd.Text, 8001); // use the ipaddress as in the server program
MessageBox.Show("Connected");
Stream stm = tcpclnt.GetStream();
MessageBox.Show("Listening for attack information......");
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
string atk = Encoding.UTF8.GetString(bb.AsSpan(0, k));
Thank you.
// Code after help
private void Connectnattk_DoWork(object sender, DoWorkEventArgs e)
{
{
{
try
{
IPAddress ipAd = IPAddress.Parse(IPV4.Text); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
MessageBox.Show("The server is running at port 8001...");
MessageBox.Show("The local End point is :" + myList.LocalEndpoint);
MessageBox.Show("Looking for other computer");
Socket s = myList.AcceptSocket();
Console.WriteLine("Found buddy " + s.RemoteEndPoint);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(satt.Text));
MessageBox.Show("The command " + satt.Text + " was sent to the computer with IP address " + IPV4.Text);
byte[] b = new byte[100];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
void ServerStart()
{
TcpListener listener = new TcpListener(IPAddress.Any, 8001);
listener.Start();
Console.WriteLine("Listening on port: 8001");
while (true)
{
TcpClient client = listener.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(state => HandleConnection(client));
}
}
void HandleConnection(TcpClient client)
{
Socket s = myList.AcceptSocket();
Console.WriteLine("Found buddy " + s.RemoteEndPoint);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(satt.Text));
Console.WriteLine("Number of connected buddies: " + connectedbuddies++);
client.Close();
}
/* clean up */
s.Close();
myList.Stop();
}
Upvotes: 1
Views: 987
Reputation: 149
Accepting a client socket in your code is synchronous, so you can only accept one client, process it then loop around to accept and process another.
You can use threads to add parallelism to your code. You create a method for handling client sockets and create a new thread for the client socket. This will allow you to continue looping to accept new incoming client socket connections whilst processing existing client socket connections.
static void ServerStart()
{
TcpListener listener = new TcpListener(IPAddress.Any, 4480);
listener.Start();
Console.WriteLine("Listening on port: 4480");
while (true)
{
// Accept the client connection before creating the thread.
TcpClient client = listener.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(state => HandleConnection(client));
}
}
static void HandleConnection(TcpClient client)
{
// Insert your code here. (Do not accept socket again here)
client.Close();
}
I create a new thread to run the HandleConnection
method for each TcpClient
using ThreadPool
. You can create threads without ThreadPool however if threads are short-lived for performance reasons, you will want to use ThreadPool to create them.
Upvotes: 1