Search code examples
c#socketstcpclientudpclient

How to create Client working on both UDP and TCP in the same port C#?


When I use the next code at client side:

IPEndPoint EndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
UdpClient UDPServer = new UdpClient(EndPoint);
TcpClient TCPClient = new TcpClient(EndPoint);

I got an exception, that it is allowed to use only single socket address. But at the server side I use:

IPEndPoint EndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
TcpListener TcpServer = new TcpListener(EndPoint);
UdpClient UDPServer = new UdpClient(EndPoint);

And there is no exception. Why?

I need to write a client that can be work on both TCP and UDP protocols in the same port. TCP - for general commands, UDP - for voice stream. How to do this?


Solution

  • The problem was that I triyed to bind UDPClient object to the same port number on Client side and as well as at Server side. That's why a I had an exception.