The UDP socket cannot receive replies when connect function is used on Linux












0















I write a udp server and client using Python. The code is simple. When the udp client sends data to the server, the server replies a message to the client.



The server code is as follows, the filename is "serverudp.py".



#!/usr/bin/env python

from socket import *
HOST = ''
PORT = 19998
s = socket(AF_INET,SOCK_DGRAM)
s.bind((HOST,PORT))
print '...waiting for message..'
while True:
data,address = s.recvfrom(1024)
print data,address
s.sendto('this is the UDP server',address)
s.close()


The client code is as follows, the filename is "clientudp.py"



#!/usr/bin/env python
from socket import *
HOST='10.0.0.12'
PORT=19998
addr=(HOST,PORT)
s = socket(AF_INET,SOCK_DGRAM)
s.connect(addr)
while True:
message = raw_input('send message:>>')
s.sendto(message, addr)
data = s.recv(1024)
print data
s.close()


The client can not receive the any reply. However, if I comment the following
connect line in the client code, it works correctly.



#s.connect(addr)


As the same client code works well on another Linux machine with the connect line, so I want to know what's the problem with my Linux machine? Is there any Linux kernel restriction or TCP/UDP socket restriction with it?



I hope your answers, thank you!










share|improve this question



























    0















    I write a udp server and client using Python. The code is simple. When the udp client sends data to the server, the server replies a message to the client.



    The server code is as follows, the filename is "serverudp.py".



    #!/usr/bin/env python

    from socket import *
    HOST = ''
    PORT = 19998
    s = socket(AF_INET,SOCK_DGRAM)
    s.bind((HOST,PORT))
    print '...waiting for message..'
    while True:
    data,address = s.recvfrom(1024)
    print data,address
    s.sendto('this is the UDP server',address)
    s.close()


    The client code is as follows, the filename is "clientudp.py"



    #!/usr/bin/env python
    from socket import *
    HOST='10.0.0.12'
    PORT=19998
    addr=(HOST,PORT)
    s = socket(AF_INET,SOCK_DGRAM)
    s.connect(addr)
    while True:
    message = raw_input('send message:>>')
    s.sendto(message, addr)
    data = s.recv(1024)
    print data
    s.close()


    The client can not receive the any reply. However, if I comment the following
    connect line in the client code, it works correctly.



    #s.connect(addr)


    As the same client code works well on another Linux machine with the connect line, so I want to know what's the problem with my Linux machine? Is there any Linux kernel restriction or TCP/UDP socket restriction with it?



    I hope your answers, thank you!










    share|improve this question

























      0












      0








      0








      I write a udp server and client using Python. The code is simple. When the udp client sends data to the server, the server replies a message to the client.



      The server code is as follows, the filename is "serverudp.py".



      #!/usr/bin/env python

      from socket import *
      HOST = ''
      PORT = 19998
      s = socket(AF_INET,SOCK_DGRAM)
      s.bind((HOST,PORT))
      print '...waiting for message..'
      while True:
      data,address = s.recvfrom(1024)
      print data,address
      s.sendto('this is the UDP server',address)
      s.close()


      The client code is as follows, the filename is "clientudp.py"



      #!/usr/bin/env python
      from socket import *
      HOST='10.0.0.12'
      PORT=19998
      addr=(HOST,PORT)
      s = socket(AF_INET,SOCK_DGRAM)
      s.connect(addr)
      while True:
      message = raw_input('send message:>>')
      s.sendto(message, addr)
      data = s.recv(1024)
      print data
      s.close()


      The client can not receive the any reply. However, if I comment the following
      connect line in the client code, it works correctly.



      #s.connect(addr)


      As the same client code works well on another Linux machine with the connect line, so I want to know what's the problem with my Linux machine? Is there any Linux kernel restriction or TCP/UDP socket restriction with it?



      I hope your answers, thank you!










      share|improve this question














      I write a udp server and client using Python. The code is simple. When the udp client sends data to the server, the server replies a message to the client.



      The server code is as follows, the filename is "serverudp.py".



      #!/usr/bin/env python

      from socket import *
      HOST = ''
      PORT = 19998
      s = socket(AF_INET,SOCK_DGRAM)
      s.bind((HOST,PORT))
      print '...waiting for message..'
      while True:
      data,address = s.recvfrom(1024)
      print data,address
      s.sendto('this is the UDP server',address)
      s.close()


      The client code is as follows, the filename is "clientudp.py"



      #!/usr/bin/env python
      from socket import *
      HOST='10.0.0.12'
      PORT=19998
      addr=(HOST,PORT)
      s = socket(AF_INET,SOCK_DGRAM)
      s.connect(addr)
      while True:
      message = raw_input('send message:>>')
      s.sendto(message, addr)
      data = s.recv(1024)
      print data
      s.close()


      The client can not receive the any reply. However, if I comment the following
      connect line in the client code, it works correctly.



      #s.connect(addr)


      As the same client code works well on another Linux machine with the connect line, so I want to know what's the problem with my Linux machine? Is there any Linux kernel restriction or TCP/UDP socket restriction with it?



      I hope your answers, thank you!







      linux sockets udp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 9:20









      JerryJerry

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You have an assumption in your code that may be incorrect. By passing 10.0.0.12 to connect, you are configuring your client to only accept incoming datagrams with a source IP address of 10.0.0.12. But nothing in your server either ensures that the source IP address is 10.0.0.12 or that the source IP address will match the destination address of the corresponding query.



          Consider:




          1. Client connects to 10.0.0.12:19998

          2. Client sends datagram to 10.0.0.12:19998 with one of its IP addresses as the source address.

          3. Server receives query sent to 10.0.0.12:19998.

          4. Server forms response to the source IP address of that datagram and goes to send it.

          5. Server chooses some source IP address other than 10.0.0.12 because it seems "closer" to the destination. The server's IP stack has no idea this datagram is in any sense a reply to the received datagram and so has no reason to set the source IP address to 10.0.0.12.

          6. Client rejects the reply because it is connected to 10.0.0.12 and the reply is from some other IP address.


          The short version of the solution is not to use connect for UDP unless the server is guaranteed to always send a reply datagram with a source IP address that matches the IP address the client is going to connect to. Nothing in your setup assures this.



          The usual solution is to never bind a UDP socket to a wildcard IP address. Instead, bind the socket to the specific IP address the server is going to use to communicate with its clients.






          share|improve this answer
























          • "...because it seems "closer" to the destination..." - "closer" in terms of routing is likely not the issue. More likely is that the server has multiple IP address in the same subnet on the same interface which have all the same distance from the destination regarding routing. The server will in this case pick the "first" address from the list of possible source addresses, for example the primary address of the interface.

            – Steffen Ullrich
            Jan 19 at 10:34






          • 1





            You are so cool. After I bind the server socket to the specific IP(i.e. 10.0.0.12), the client works.

            – Jerry
            Jan 19 at 12:09











          • I find another related issue, showing below. Do you have any idea about it? Thanks.@David Schwartz

            – Jerry
            Jan 19 at 12:30











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265649%2fthe-udp-socket-cannot-receive-replies-when-connect-function-is-used-on-linux%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have an assumption in your code that may be incorrect. By passing 10.0.0.12 to connect, you are configuring your client to only accept incoming datagrams with a source IP address of 10.0.0.12. But nothing in your server either ensures that the source IP address is 10.0.0.12 or that the source IP address will match the destination address of the corresponding query.



          Consider:




          1. Client connects to 10.0.0.12:19998

          2. Client sends datagram to 10.0.0.12:19998 with one of its IP addresses as the source address.

          3. Server receives query sent to 10.0.0.12:19998.

          4. Server forms response to the source IP address of that datagram and goes to send it.

          5. Server chooses some source IP address other than 10.0.0.12 because it seems "closer" to the destination. The server's IP stack has no idea this datagram is in any sense a reply to the received datagram and so has no reason to set the source IP address to 10.0.0.12.

          6. Client rejects the reply because it is connected to 10.0.0.12 and the reply is from some other IP address.


          The short version of the solution is not to use connect for UDP unless the server is guaranteed to always send a reply datagram with a source IP address that matches the IP address the client is going to connect to. Nothing in your setup assures this.



          The usual solution is to never bind a UDP socket to a wildcard IP address. Instead, bind the socket to the specific IP address the server is going to use to communicate with its clients.






          share|improve this answer
























          • "...because it seems "closer" to the destination..." - "closer" in terms of routing is likely not the issue. More likely is that the server has multiple IP address in the same subnet on the same interface which have all the same distance from the destination regarding routing. The server will in this case pick the "first" address from the list of possible source addresses, for example the primary address of the interface.

            – Steffen Ullrich
            Jan 19 at 10:34






          • 1





            You are so cool. After I bind the server socket to the specific IP(i.e. 10.0.0.12), the client works.

            – Jerry
            Jan 19 at 12:09











          • I find another related issue, showing below. Do you have any idea about it? Thanks.@David Schwartz

            – Jerry
            Jan 19 at 12:30
















          1














          You have an assumption in your code that may be incorrect. By passing 10.0.0.12 to connect, you are configuring your client to only accept incoming datagrams with a source IP address of 10.0.0.12. But nothing in your server either ensures that the source IP address is 10.0.0.12 or that the source IP address will match the destination address of the corresponding query.



          Consider:




          1. Client connects to 10.0.0.12:19998

          2. Client sends datagram to 10.0.0.12:19998 with one of its IP addresses as the source address.

          3. Server receives query sent to 10.0.0.12:19998.

          4. Server forms response to the source IP address of that datagram and goes to send it.

          5. Server chooses some source IP address other than 10.0.0.12 because it seems "closer" to the destination. The server's IP stack has no idea this datagram is in any sense a reply to the received datagram and so has no reason to set the source IP address to 10.0.0.12.

          6. Client rejects the reply because it is connected to 10.0.0.12 and the reply is from some other IP address.


          The short version of the solution is not to use connect for UDP unless the server is guaranteed to always send a reply datagram with a source IP address that matches the IP address the client is going to connect to. Nothing in your setup assures this.



          The usual solution is to never bind a UDP socket to a wildcard IP address. Instead, bind the socket to the specific IP address the server is going to use to communicate with its clients.






          share|improve this answer
























          • "...because it seems "closer" to the destination..." - "closer" in terms of routing is likely not the issue. More likely is that the server has multiple IP address in the same subnet on the same interface which have all the same distance from the destination regarding routing. The server will in this case pick the "first" address from the list of possible source addresses, for example the primary address of the interface.

            – Steffen Ullrich
            Jan 19 at 10:34






          • 1





            You are so cool. After I bind the server socket to the specific IP(i.e. 10.0.0.12), the client works.

            – Jerry
            Jan 19 at 12:09











          • I find another related issue, showing below. Do you have any idea about it? Thanks.@David Schwartz

            – Jerry
            Jan 19 at 12:30














          1












          1








          1







          You have an assumption in your code that may be incorrect. By passing 10.0.0.12 to connect, you are configuring your client to only accept incoming datagrams with a source IP address of 10.0.0.12. But nothing in your server either ensures that the source IP address is 10.0.0.12 or that the source IP address will match the destination address of the corresponding query.



          Consider:




          1. Client connects to 10.0.0.12:19998

          2. Client sends datagram to 10.0.0.12:19998 with one of its IP addresses as the source address.

          3. Server receives query sent to 10.0.0.12:19998.

          4. Server forms response to the source IP address of that datagram and goes to send it.

          5. Server chooses some source IP address other than 10.0.0.12 because it seems "closer" to the destination. The server's IP stack has no idea this datagram is in any sense a reply to the received datagram and so has no reason to set the source IP address to 10.0.0.12.

          6. Client rejects the reply because it is connected to 10.0.0.12 and the reply is from some other IP address.


          The short version of the solution is not to use connect for UDP unless the server is guaranteed to always send a reply datagram with a source IP address that matches the IP address the client is going to connect to. Nothing in your setup assures this.



          The usual solution is to never bind a UDP socket to a wildcard IP address. Instead, bind the socket to the specific IP address the server is going to use to communicate with its clients.






          share|improve this answer













          You have an assumption in your code that may be incorrect. By passing 10.0.0.12 to connect, you are configuring your client to only accept incoming datagrams with a source IP address of 10.0.0.12. But nothing in your server either ensures that the source IP address is 10.0.0.12 or that the source IP address will match the destination address of the corresponding query.



          Consider:




          1. Client connects to 10.0.0.12:19998

          2. Client sends datagram to 10.0.0.12:19998 with one of its IP addresses as the source address.

          3. Server receives query sent to 10.0.0.12:19998.

          4. Server forms response to the source IP address of that datagram and goes to send it.

          5. Server chooses some source IP address other than 10.0.0.12 because it seems "closer" to the destination. The server's IP stack has no idea this datagram is in any sense a reply to the received datagram and so has no reason to set the source IP address to 10.0.0.12.

          6. Client rejects the reply because it is connected to 10.0.0.12 and the reply is from some other IP address.


          The short version of the solution is not to use connect for UDP unless the server is guaranteed to always send a reply datagram with a source IP address that matches the IP address the client is going to connect to. Nothing in your setup assures this.



          The usual solution is to never bind a UDP socket to a wildcard IP address. Instead, bind the socket to the specific IP address the server is going to use to communicate with its clients.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 9:31









          David SchwartzDavid Schwartz

          137k14143224




          137k14143224













          • "...because it seems "closer" to the destination..." - "closer" in terms of routing is likely not the issue. More likely is that the server has multiple IP address in the same subnet on the same interface which have all the same distance from the destination regarding routing. The server will in this case pick the "first" address from the list of possible source addresses, for example the primary address of the interface.

            – Steffen Ullrich
            Jan 19 at 10:34






          • 1





            You are so cool. After I bind the server socket to the specific IP(i.e. 10.0.0.12), the client works.

            – Jerry
            Jan 19 at 12:09











          • I find another related issue, showing below. Do you have any idea about it? Thanks.@David Schwartz

            – Jerry
            Jan 19 at 12:30



















          • "...because it seems "closer" to the destination..." - "closer" in terms of routing is likely not the issue. More likely is that the server has multiple IP address in the same subnet on the same interface which have all the same distance from the destination regarding routing. The server will in this case pick the "first" address from the list of possible source addresses, for example the primary address of the interface.

            – Steffen Ullrich
            Jan 19 at 10:34






          • 1





            You are so cool. After I bind the server socket to the specific IP(i.e. 10.0.0.12), the client works.

            – Jerry
            Jan 19 at 12:09











          • I find another related issue, showing below. Do you have any idea about it? Thanks.@David Schwartz

            – Jerry
            Jan 19 at 12:30

















          "...because it seems "closer" to the destination..." - "closer" in terms of routing is likely not the issue. More likely is that the server has multiple IP address in the same subnet on the same interface which have all the same distance from the destination regarding routing. The server will in this case pick the "first" address from the list of possible source addresses, for example the primary address of the interface.

          – Steffen Ullrich
          Jan 19 at 10:34





          "...because it seems "closer" to the destination..." - "closer" in terms of routing is likely not the issue. More likely is that the server has multiple IP address in the same subnet on the same interface which have all the same distance from the destination regarding routing. The server will in this case pick the "first" address from the list of possible source addresses, for example the primary address of the interface.

          – Steffen Ullrich
          Jan 19 at 10:34




          1




          1





          You are so cool. After I bind the server socket to the specific IP(i.e. 10.0.0.12), the client works.

          – Jerry
          Jan 19 at 12:09





          You are so cool. After I bind the server socket to the specific IP(i.e. 10.0.0.12), the client works.

          – Jerry
          Jan 19 at 12:09













          I find another related issue, showing below. Do you have any idea about it? Thanks.@David Schwartz

          – Jerry
          Jan 19 at 12:30





          I find another related issue, showing below. Do you have any idea about it? Thanks.@David Schwartz

          – Jerry
          Jan 19 at 12:30


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265649%2fthe-udp-socket-cannot-receive-replies-when-connect-function-is-used-on-linux%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage