Click here to Skip to main content
15,886,544 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If I have 2 connections being made to 1 TcpClient, is it possible to retrieve 2 different sockets from that one TcpClient? At the moment, as soon as the second connection is being made and getting the Socket from the client, the socket seems to get lost with the first connection, as I cannot send or receive over that socket anymore.

I am using the following code to retrieve the socket from the client.

C#
Socket socket = _tcpClient.Client;
Posted
Comments
Sergey Alexandrovich Kryukov 14-May-13 18:24pm    
Please don't post non-answers as "solution". It can give you abuse reports which eventually may even lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.

Your inappropriate post as "solution" was removed due to abuse reports.

—SA

You cannot establish multiple connections from one client.
TCP is connection orientated, so the multiplicity is always 1:1 (Server-Socket : Client-Socket).

You should use two TcpClients for two connections.
 
Share this answer
 
Comments
Nelek 19-Apr-12 19:10pm    
The OP posted a solution to speak with you, the text is:
okay, so for instance if I have 2 instances of a class that is created, and both of them is required to connect to the same remote tcp client, it is not possible to retrieve a socket for both instances from that client? Is there any way to work around this? I do not constantly want to disconnect the one object to allow the other to connect and retrieve a socket for communication.
Henning Dieterichs 20-Apr-12 5:32am    
I think you have misunderstood the concept of clients/servers (since my explanation is very short, I recommend you reading some network tutorials):
You cannot connect to a TcpClient, as a client is only a client. Use a TcpListener ( = Server), which can be bound to a port and is able to accept multiple clients (AcceptClient or something like this).
Then multiple TcpClients can connect to the same endpoint.
You need to create another instance of the client. The ratio is 1 : 1 as Henning described, so you need as many instances on each side as connections you will have.
 
Share this answer
 
You would need to make two connections (or any other number of connections you may need) using two separate instances of TcpClient, which is, basically, a wrapper around an instance of a Socket. Besides, you can do development either on the level of Socket objects, or TcpListener/TcpClient objects. You can mix both approaches in one project or solution, but in most cases there is no need for it. Those approaches are very close, with TcpListener/TcpClient approach being just a tiny bit of higher level, provided by .NET FCL for some extra convenience.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900