0

My application has many tcpclients that is uses to update hundreds of servers when instructed. I'm having trouble in the design with a minor but important issue.

My programs takes an outgoing message out of a queue and selects an available client from an array of clients, I'll call this client1. It starts a connection on the tcpclient with a BeginConnect and issues a call back method. The program then moves on to other messages from the queue and the tcpclients that will be sending them.

When the the callback happens for client1, my program gets an AsyncResult from which I can resolve the socket.

Here is my problem. How do I know which socket or TCPClient I have? It's important because I need to know which message to send on this connected client.

I've looked on the socket and didn't find a name property.

How do I identify the socket so I know the correct messaging conversation to have?

Thanks!

Bernie Hunt
  • 306
  • 4
  • 22
  • have you checked to see if the object exposes anything like the Ipaddress, or the name ..etc...? this may lend you some ideas if not try a google search http://stackoverflow.com/questions/2717381/how-do-i-get-client-ip-address-using-tcpclient – MethodMan Apr 06 '15 at 16:01
  • Do each of your clients connect to a unique server (IP address/Port) or do each of your clients use a unique local endpoint (IP address/Port)? – Shar1er80 Apr 06 '15 at 17:56
  • MethodMan, good find. I spent about an hour looking under multiple clients and only got server example, hahaha. – Bernie Hunt Apr 06 '15 at 18:51
  • Sharfer, Each client has a unique IP but they all use the same port. – Bernie Hunt Apr 06 '15 at 18:51

1 Answers1

0

After more study and research I realized the callback would pass any System.Object. So the answer is as simple as wrapping the TCPClient in a class that has properties that can identify it's purpose.

In my case a simple class with a Socket, String for the message, string for the IP, and an int for the Port was enough to handle the situate when it made it to the call back. Just remember you have to cast the IAsyncResult.AsyncState back to the type of your wrapper class.

Bernie Hunt
  • 306
  • 4
  • 22