Sending information to clients through two ports on one server-pls guide
Hello Everybody:
I have developed a multithreaded client server application. The server has two listener with two ports (8000 and 8001).. When the server loads the two ports are listening…The client connects through port 8000 and sends message to the server through port 8000. The thread through which the client connects is identified by using Thread.GetHashCode(). Now I want to sent the same message to another client through port 8001. That thread is also identified by using the Thread.GetHashCode(). When the client at port 8001 receives the message , it has to sent the same message to the server through the same thread. After the server receives the message it has to sent back to the same client at port 8000 through the same thread identified by using the Thread.GetHashCode()…
My problem is how do I carry out the entire process sending the information through two ports, through the server.to the same two clients two and fro?
Should I send the data Asynchronously or synchronously….
I will really appreciate it for any help and guidance….
I have attached the code for your reference…
I will really appreciate it for any help and guidance…. And comments.
Thanks,,,
Rahil
public MultiThreadXml()
{
try
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
InitializeListView();
tcpLsn1= new TcpListener(System.Net.IPAddress.Parse(ipAddress),8000);// start the server
tcpLsn1.Start();
statusBar2.Text="Listen at:" + tcpLsn1.LocalEndpoint.ToString();
for( int NumOfThds=1; NumOfThds <=100; NumOfThds++)// For loop for 100 threads
{
tcpThd1=new Thread(new ThreadStart(ClientProcess));
tcpThd1.Start();
}
tcpLsn=new TcpListener(System.Net.IPAddress.Parse(ipAddress),8001);// start the server
tcpLsn.Start();
statusBar1.Text = "Listen at: " + tcpLsn.LocalEndpoint.ToString();
for( int NoOfThds=1; NoOfThds <=10; NoOfThds++)// For loop for 10 threads
{
tcpThd=new Thread(new ThreadStart(ServerProcess));
tcpThd.Start();
}
}
catch (SocketException socketEx)
{
MessageBox.Show(socketEx.Message);
}
}
public void ServerProcess()
{
try
{
{
Byte[] bytes = new Byte[5000]; //Buffer for reading data
//Buffer for reading data
String data = null;
// Enter the listening loop.
while (true)
{
TcpClient client = tcpLsn.AcceptTcpClient();
MessageBox.Show("Connected to the client");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
Int32 i;
// Loop to receive all the ata sent by the client
while ((i=stream.Read(bytes, 0,bytes.Length))!=0)
{
//Translate data bytes to a ASCII string.
data=System.Text.Encoding.ASCII.GetString(bytes,0,i);
txtDataRx.Text= data + txtDataRx.Text;
}
}
}
}
catch (SocketException e)
{
MessageBox.Show("SocketException");
}
if ( tcpThd.IsAlive )
{
tcpThd.Abort();
}
}
public void ClientProcess()//
{
try
{
Byte[] bytes = new Byte[5000]; //Buffer for reading data
String data = null;
while (true)
{
TcpClient client1 = tcpLsn1.AcceptTcpClient();
MessageBox.Show("Connected to the client");
data = null;
NetworkStream stream = client1.GetStream();
Int32 i;
while ((i=stream.Read(bytes, 0,bytes.Length))!=0)
{
data=System.Text.Encoding.ASCII.GetString(bytes,0,i);
txtDataRx.Text= data + txtDataRx.Text;
}
}
}
catch (SocketException e)
{
MessageBox.Show("SocketException");
}
if(tcpThd.IsAlive)
{
tcpThd.Abort();
}
}