How to reverse roles of TcpListener/TcpClient?

241 Views Asked by At

I have an unusual question that I hope to get input on. I'm new to network programming and am learning socket programming via the .NET "TcpListener" and "TcpClient" classes in the System.Net.Sockets namespace. I need to use them in an unusual manner though: I need to be able to establish a connection between computer A and computer B. Computer A will be running an instance of the "TcpClient" class and will attempt to open a connection to computer B which is running an instance of "TcpListener". This part seems straight forward. However, once the connection has been established I need to reverse the roles so that computer A becomes the machine that waits for and responds to messages sent from computer B. In other words computer A then becomes the server and computer B becomes the client. This role reversal must use the same connection that was opened by computer A when it was initially acting as a client. It's not clear to me how to do this as all the examples and the (excellent) book "TCP/IP Sockets in C#" I'm reading always use these classes in their predefined roles. Is there actually a way to do this? Do I have to drop down to the "Socket" class rather than use the more abstracted "TcpListener/TcpClient" classes?

0

There are 0 best solutions below