C # Using tcp and udp protocols

I need to use udp and tcp connections in my application, TcpClient/TcpListenerit will rarely be active, but using udp will be the main one.

This is the server code:

    static void Main(string[] args)
    {
        TcpListener  listener = new TcpListener(IPAddress.Any, 25655);
        listener.Start();
        Socket  sck = listener.AcceptTcpClient().Client;
        UdpClient udpServer = new UdpClient(1100);
        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
        var data = udpServer.Receive(ref remoteEP);
        string result = Encoding.UTF8.GetString(data);
        Console.WriteLine(result);
        Console.Read();
    }

And this is the Client:

   static void Main(string[] args)
    {
       TcpClient client = new TcpClient("127.0.0.1", 25655);
       Socket sck = client.Client;
       UdpClient udpclient = new UdpClient();
       IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1100); // endpoint where server is listening
       udpclient.Connect(ep);
       byte[] data = UTF8Encoding.UTF8.GetBytes("Hello");
       udpclient.Send(data,data.Length);       
    }

First, I establish a Tcp connection, then I try to connect and send data from the client to the server. From the breakpoint I add, I see that the Tcp part is working correctly, the client ends the program, but on the server it hangs in the receiving part, var data = udpServer.Receive(ref remoteEP); for example, no data was received. When I delete part of the tcp code (the first 2 lines from the server and client), it works fine, it displays a message about the results.

Does anyone know why they were unable to receive data from the client?

Thanks in advance.

+4
3

UDP TCP , TCP , , . UDP ,

TCP
TCP
TCP
UDP ( , , UDP )
UDP
, UDP < --- hang

, .

+1

, listener.AcceptTcpClient() , UdpClient Tcp-. , Tcp Udp-, 2 . - Udp , . - Thread.Sleep(1000) UDP. , , , , , Tcp Udp :

static void Main(string[] args)
{
    Task.Factory.StartNew(() => 
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 25655);
        listener.Start();
        Socket sck = listener.AcceptTcpClient().Client;

        // ToDo: further actions related to TCP client
    }, TaskCreationOptions.LongRunning);

    UdpClient udpServer = new UdpClient(1100);
    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
    var data = udpServer.Receive(ref remoteEP);
    string result = Encoding.UTF8.GetString(data);
    Console.WriteLine(result);
    Console.Read();
}

, , , , , Tcp.

+1

What if you first start the UDP client on the server side, and then establish a TCP connection between the client and server ?!

Server

        static void Main(string[] args)
    {
        UdpClient udpServer = new UdpClient(1100);
        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

        TcpListener listener = new TcpListener(IPAddress.Any, 25655);
        listener.Start();
        Socket sck = listener.AcceptTcpClient().Client;

        var data = udpServer.Receive(ref remoteEP);
        string result = Encoding.UTF8.GetString(data);
        Console.WriteLine(result);
        Console.Read();
    }

Client

static void Main(string[] args)
{
    TcpClient client = new TcpClient();
    client.Connect("127.0.0.1", 25655);

    UdpClient udpclient = new UdpClient();
    IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1100); // endpoint where server is listening
    udpclient.Connect(ep);
    byte[] data = UTF8Encoding.UTF8.GetBytes("Hello");
    udpclient.Send(data, data.Length);
}
0
source

Source: https://habr.com/ru/post/1652875/


All Articles