4

I need to use udp and tcp connections in my application,the TcpClient/TcpListener would rarely be active,but the udp one would be the main usage.

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);       
    }

I'm establishing the Tcp connection at first,then i'm trying to connect and send data from the client to the server. From a breakpoint i add, i can see that the Tcp part works properly,the client finishes the program but in the server,it's hangs on the receiving part var data = udpServer.Receive(ref remoteEP); like no data arrived..when i remove the tcp code part(the first 2 lines from the server and the client) it works great,shows the result message.

Does anyone know why im unable to get the data from the client?

Thanks in advance.

1
  • While I don't have a direct answer for your question, I would like to let you know about WCF, which makes working with Tcp & other messaging standards incredibly easy. msdn.microsoft.com/en-us/library/ee354381.aspx Commented Aug 29, 2016 at 14:25

3 Answers 3

1

The main difference between UDP and TCP is that TCP is going to try to resend the message until the server tells the client it has received it. UDP is going to send and forget it even if the packet never reach or the host doesn't exist at all

Here is the flow of your code

Server starts up TCP
Client sends TCP
Server Receives TCP
Client sends UDP (server did not listen yet, packet lost but UDP doesn't care)
Server starts to listen to UDP
Server waiting for UDP to come <--- hang

You would like to do some multithread programming and start both of them at the same time before you tries to send message from the client.

Sign up to request clarification or add additional context in comments.

2 Comments

you mean that the client sends the data before the server started to receive? so if i'll use Thread.Sleep(); before the sending part(in the client code) would i be able to read the message in the server side?
@Slashy You should.
1
+50

The thing is that listener.AcceptTcpClient() blocks your current thread and UdpClient on server side is not established before Tcp connection created. In fact, your server is waiting for Tcp connection and only after that starting listening of Udp connections, while your client creates 2 connections one by one. My suggestions is - your server starting listening Udp port after the moment client actually sent data. The easiest way to check my suggestions - for client code add Thread.Sleep(1000) before sending data via UDP. In order to make that working you probably need to modify your code not blocking main thread but separate Tcp and Udp in the way similar to this:

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();
}

client code can probably stay as it is for this example, but for real project I would recommend to separate that as well, as for sure you would like to get back some data from server via Tcp.

Comments

0

What if you first start the UDP client on server side and then establish the TCP connection between 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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.