none
UDP communication,how server can send message to client before client sends any message RRS feed

  • Question

  • HI ,

    I have a requirement like client server communication using udp protocol.I implemented this here i am able to communicate request response messages between client and server.Here i have one specific requirement like server will send some unsolicitated notifications messgae to client that also i have to read and process.This messages i am able to read only once the client sends a message to server and server sent its response after that i am able to read all the unsolicitated messages(i think by this time server comes to know its remote endpoint).But my requirement is like before client send any message if server any unsolicitataed messages i want to read and process.

    How can we achieve this.Once client and server is established conndection Socket.Connected flag will be true,then in the cliednt side we will come to know server ip address and port through remote endpoint.Is there any similar way to identify the remote endpoint from the server side.

    Or any initialize call from client to server then server can come to know its remote endpoint.

    please suggest an idea.way to resolve this.In my case actuall server is a hardware device,but for testing purpose i am creating a simulator kind of application,here message is not reaching to its clinet.Server is sending to server itself.

    Once client nad server connected in the client side socket.coonected flag is true then i will senda a dummy message

    SendTo(Remote endpoint) is this solve my problem.

    Please help me to find a solution.


    Coding.....................................


    • Edited by La07K Thursday, September 26, 2019 9:06 AM suggestion added
    Thursday, September 26, 2019 6:52 AM

Answers

  • > Once client and server is established connection ...

    Unlike TCP, there is no concept of a "connection" in UDP.   Socket.Connected does NOT mean "connected" in the TCP sense.  It just means you are talking to your local network stack.

    Also, the terms "client" and "server" are a bit confusing in UDP.  Are you using "server" to mean a machine listening for UDP traffic?  Or is your server broadcasting UDP traffic?

    > ... then in the cliednt side we will come to know server ip address and port through remote endpoint.

    No, that's not how it works.  The client has to know the server's IP and port number before it can send a message at all.  No one is going to tell the client what IP and port to use.

    > is there any similar way to identify the remote endpoint from the server side.

    Before the first client message?  Of course not.  How could it possibly know that?  The client could be any machine anywhere on the internet.

    Now, when the server receives a message, you can use the Socket.ReceiveFrom method (instead of Receive).  ReceiveFrom not only gives you the message, it gives you the System.Net.Endpoint of the machine that sent the message.  You can certainly send a message back to that system.

    HOWEVER, remember that the client has to be listening on the socket in order to receive anything.  Many UDP contacts go one direction only.

    ALSO, remember that you can't go back the other direction if there is a router in between.  If your server is outside your network, then you cannot do two-way communication; the client machine cannot be reached by IP address.

    If you really need bi-directional communication, you should probably be using TCP, not UDP.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by La07K Tuesday, October 1, 2019 1:30 PM
    Friday, September 27, 2019 6:00 PM

All replies

  • Hi La07K, 

    Thank you for posting here.

    According to your question, I have questions to confirm with you.

    >> But my requirement is like before client send any message if server any unsolicitataed messages I want to read and process.

    What does ‘unsolicitataed message’ mean? Could you provide more details about what server need to do before client send any message?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 7:16 AM
    Moderator
  • Unsolicitate message means,server will keep on sending hardware updates.Like voltage change,some kind of log information etc.That means without client request they are sending messages to client.

    Thanks..


    Coding.....................................

    Friday, September 27, 2019 8:26 AM
  • > Once client and server is established connection ...

    Unlike TCP, there is no concept of a "connection" in UDP.   Socket.Connected does NOT mean "connected" in the TCP sense.  It just means you are talking to your local network stack.

    Also, the terms "client" and "server" are a bit confusing in UDP.  Are you using "server" to mean a machine listening for UDP traffic?  Or is your server broadcasting UDP traffic?

    > ... then in the cliednt side we will come to know server ip address and port through remote endpoint.

    No, that's not how it works.  The client has to know the server's IP and port number before it can send a message at all.  No one is going to tell the client what IP and port to use.

    > is there any similar way to identify the remote endpoint from the server side.

    Before the first client message?  Of course not.  How could it possibly know that?  The client could be any machine anywhere on the internet.

    Now, when the server receives a message, you can use the Socket.ReceiveFrom method (instead of Receive).  ReceiveFrom not only gives you the message, it gives you the System.Net.Endpoint of the machine that sent the message.  You can certainly send a message back to that system.

    HOWEVER, remember that the client has to be listening on the socket in order to receive anything.  Many UDP contacts go one direction only.

    ALSO, remember that you can't go back the other direction if there is a router in between.  If your server is outside your network, then you cannot do two-way communication; the client machine cannot be reached by IP address.

    If you really need bi-directional communication, you should probably be using TCP, not UDP.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by La07K Tuesday, October 1, 2019 1:30 PM
    Friday, September 27, 2019 6:00 PM
  • As Tim said, there are no permanent connections such as there are for TCP/IP. TCP/IP offers the following guarantees that UDP does not:

    1. Delivery
    2. The specified order of the packets

    As for identify the remote endpoint from the server side I think it depends on the server (the hardware). You need to consult the hardware and software documentation, no one unfamiliar with that can help.



    Sam Hobbs
    SimpleSamples.Info


    Sunday, September 29, 2019 12:01 AM
  • Thanks Tim:)

    Coding.....................................

    Tuesday, October 1, 2019 1:30 PM
  • Also, the terms "client" and "server" are a bit confusing in UDP.  Are you using "server" to mean a machine listening for UDP traffic? 

    yes,here server means the hardware.


    Coding.....................................

    Tuesday, October 1, 2019 1:32 PM
  • Thanks for ur input Sam:)

    Coding.....................................

    Tuesday, October 1, 2019 1:34 PM