Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Retreive Port Number of UDP Client connected to my UDP Server

appling
2020-10-28
2020-11-24
  • appling - 2020-10-28

    Hello everyone,

    so I am trying to implement a UDP server that will wait for a message from a client and then send data to that client.
    The problem is, I cannot find a way to get the port number of the UDP client that is sending a message to me.
    I have created the socket like this:

            thesocket :=SysSockCreate(  iAddressFamily  := SOCKET_AF_INET,
                                        diType          := SOCKET_DGRAM,        (* For UDP*)
                                        diProtocol      := SOCKET_IPPROTO_UDP,  //or SOCKET_IPPROTO_IP ?
                                        pResult         := ADR(CreateErrorCode));
    

    bound like this:

    m_bound := SysSockBind(         hSocket         := thesocket,
                                    pSockAddr       := ADR(MySocket),   
                                    diSockAddrSize  := SIZEOF(MySocket));       
    

    Now I imagine I receive a message here:

    m_receivedBytes := SysSockRecvFromUdp(  hSocketUDP  := thesocket, 
                                            pbyData     := m_RecvBufferAddr, 
                                            diDataSize  := m_RecvBufferSize, 
                                            pReply      := ADR(m_clientInfo), 
                                            pResult     := ADR(m_received));
    

    now m_clientInfo is of type UDP_REPLY which contains the IP address of the client. When I want to respond via SysSockSendToUdp I need to specify the destination port (of course).
    In the documentation of sysScoket ( https://help.codesys.com/webapp/idx-SysSocket%20Implementation-lib;product=SysSocket%20Implementation;version=3.5.15.0 ) I could not find a receive method that would work on my UDP socket and give me a socketaddr type.

    I hope someone can help me.

    Best regards

     
  • appling - 2020-11-24

    Hi dFx, thank you very much for your reply.
    Yes, in the end I solved it with the NBS library. Their UDP.Receiver has a structure that contains the remote IP and port - that way I could then establish a two way communication.

     

Log in to post a comment.