TCPClient to multiple systems
-
Hello Everybody, I am writing a sender and receiver application, sender in C#,WPF and receiver in C# WinForms. In sender, will be having a list of task to do, which will be sent to multiple receivers running in multiple systems. Here is the code in Sender
for (int j = 0; j < tmpfilled.Count; j++)
{
// some statements related to send to receiver
////////////
Thread thFromReceiver = new Thread(new ThreadStart(thFromReceiverFunction));
thFromReceiver.Start();
}//Body of the thFromReceiverFunction()
public void thFromReceiverFunction()
{
// here again two thread, for read and write
try
{
TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip_address), portNo);
client.Connect(serverEndPoint);Thread readThreadRec = new Thread(new ParameterizedThreadStart(readFromReceiver)); Thread writeThreadRec = new Thread(new ParameterizedThreadStart(writeToReceiver)); readThreadRec.Start(client); writeThreadRec.Start(client); } catch(Exception ex) { }
In Receiver,
// inside a thread
TcpListener tcpListener = new TcpListener(IPAddress.Any, portNo); // same port number
tcpListener.Start();while (true) { //blocks until a client has connected to the server TcpClient client = tcpListener.AcceptTcpClient(); Thread readThread = new Thread(new ParameterizedThreadStart(read\_thread)); readThread.IsBackground = true; Thread writeThread = new Thread(new ParameterizedThreadStart(write\_thread)); writeThread.IsBackground = true; readThread.Start(client); writeThread.Start(client); readThread.Join(); writeThread.Join(); client.Close(); }
My problem is, if I am sending to single receiver, everything is working fine. If multiple receivers are running in multiple systems, then, ipaddress which I am sending the request, shows right, but whichever the client receives the first request, all other requests are going to the same client, even though I am sending to different ipaddress (through thFromReceiverFunction() funtion). I think I explained the issue properly, if not please let me know. Thanks in advance. Regards, Gopinath.
-
Hello Everybody, I am writing a sender and receiver application, sender in C#,WPF and receiver in C# WinForms. In sender, will be having a list of task to do, which will be sent to multiple receivers running in multiple systems. Here is the code in Sender
for (int j = 0; j < tmpfilled.Count; j++)
{
// some statements related to send to receiver
////////////
Thread thFromReceiver = new Thread(new ThreadStart(thFromReceiverFunction));
thFromReceiver.Start();
}//Body of the thFromReceiverFunction()
public void thFromReceiverFunction()
{
// here again two thread, for read and write
try
{
TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip_address), portNo);
client.Connect(serverEndPoint);Thread readThreadRec = new Thread(new ParameterizedThreadStart(readFromReceiver)); Thread writeThreadRec = new Thread(new ParameterizedThreadStart(writeToReceiver)); readThreadRec.Start(client); writeThreadRec.Start(client); } catch(Exception ex) { }
In Receiver,
// inside a thread
TcpListener tcpListener = new TcpListener(IPAddress.Any, portNo); // same port number
tcpListener.Start();while (true) { //blocks until a client has connected to the server TcpClient client = tcpListener.AcceptTcpClient(); Thread readThread = new Thread(new ParameterizedThreadStart(read\_thread)); readThread.IsBackground = true; Thread writeThread = new Thread(new ParameterizedThreadStart(write\_thread)); writeThread.IsBackground = true; readThread.Start(client); writeThread.Start(client); readThread.Join(); writeThread.Join(); client.Close(); }
My problem is, if I am sending to single receiver, everything is working fine. If multiple receivers are running in multiple systems, then, ipaddress which I am sending the request, shows right, but whichever the client receives the first request, all other requests are going to the same client, even though I am sending to different ipaddress (through thFromReceiverFunction() funtion). I think I explained the issue properly, if not please let me know. Thanks in advance. Regards, Gopinath.
-
You have shown all the code related to receivers, but it is the sender that is causing the problem. Please edit your question and show the problem code.
Hello Richard, Thanks for your reply. First two session in earlier post is the code from sender. Here are the code for the functions mentioned in the thread.
public void readFromReceiver(object clients)
{
ASCIIEncoding encoder = new ASCIIEncoding();
TcpClient client = (TcpClient)clients;
NetworkStream clientStream = (client).GetStream();
byte[] msg_type = new byte[1]; // to read header
bool flag = false;
while(!flag)
{
try
{
// need to get back msg from receiver about status
int bytesRead = 0;
bytesRead = clientStream.Read(msg_type, 0, 1);
int msg = (int)(msg_type[0]);
string retVal = "";
if (msg == 3) // return success
{
retVal = receiveString(clientStream); // return message
}
}
catch(Exception ex)
{} }
}
public void writeToReceiver(object clients)
{
ASCIIEncoding encoder = new ASCIIEncoding();
TcpClient client = (TcpClient)clients;
NetworkStream clientStream = (client).GetStream();bool flag = false; while(!flag) { while (nQReceiveProjName.Count > 0) // list of request { try { string cmdToPass = fnGetXMLString(nQReceiveProjName\[0\]); nQReceiveProjName.RemoveAt(0); sendCommand(clientStream, 5); sendString(clientStream, cmdToPass); } catch(Exception ex) { } } }
}
In both the cases, client remote end point is holding the ip address of the client in which the first request is received. Regards, Gopinath.
-
Hello Richard, Thanks for your reply. First two session in earlier post is the code from sender. Here are the code for the functions mentioned in the thread.
public void readFromReceiver(object clients)
{
ASCIIEncoding encoder = new ASCIIEncoding();
TcpClient client = (TcpClient)clients;
NetworkStream clientStream = (client).GetStream();
byte[] msg_type = new byte[1]; // to read header
bool flag = false;
while(!flag)
{
try
{
// need to get back msg from receiver about status
int bytesRead = 0;
bytesRead = clientStream.Read(msg_type, 0, 1);
int msg = (int)(msg_type[0]);
string retVal = "";
if (msg == 3) // return success
{
retVal = receiveString(clientStream); // return message
}
}
catch(Exception ex)
{} }
}
public void writeToReceiver(object clients)
{
ASCIIEncoding encoder = new ASCIIEncoding();
TcpClient client = (TcpClient)clients;
NetworkStream clientStream = (client).GetStream();bool flag = false; while(!flag) { while (nQReceiveProjName.Count > 0) // list of request { try { string cmdToPass = fnGetXMLString(nQReceiveProjName\[0\]); nQReceiveProjName.RemoveAt(0); sendCommand(clientStream, 5); sendString(clientStream, cmdToPass); } catch(Exception ex) { } } }
}
In both the cases, client remote end point is holding the ip address of the client in which the first request is received. Regards, Gopinath.
I don't see anything anywhere where you are storing or using an IP address. Once a socket connection is established between server and client, that socket remains open until closed by either end. Each message using the correct socket endpoint will be sent to the correct client. Google for "client server C#" to see other examples.
-
I don't see anything anywhere where you are storing or using an IP address. Once a socket connection is established between server and client, that socket remains open until closed by either end. Each message using the correct socket endpoint will be sent to the correct client. Google for "client server C#" to see other examples.
-
Here is where I am using the ip address to send the request.
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip_address), portNo);
client.Connect(serverEndPoint);Let me check in google also as you mentioned. Thanks again. Regards, Gopinath.
-
You only do that once, when the client first connects. From then on you must use just the socket that is returned on the connection.