Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It assigns the "local" end's port number.</p> <p>For a server socket, this is the ultimate way to go - it is exactly what is needed: have your socket be bound to port 80 for a web server, for example.</p> <p>For a client socket, however, the local address and port is normally not of importance. So you don't <code>bind()</code>. If the server restricts its clients to maybe have a certain port number, or a port number out of a given range, you can use <code>bind()</code> on client side as well.</p> <p>On the other hand, you might as well be able to <code>listen()</code> on a socket where you haven't called <code>bind()</code> (actually I'm not sure about that, but it would make sense). In this scenario, your server port would be random, and the server process would communicate its port via a different means to the client. Imagine a "double-connection" protocol such as FTP, where you have a control connection and a data connection. The port the data connection listens on is completely arbitrary, but must be communicated to the other side. So the "automatically determined port" is used and communicated.</p> <p>One example in Python:</p> <pre><code>import socket s = socket.socket() # create your socket s.listen(10) # call listen without bind s.getsockname() Which random port number did we get? # here results in ('0.0.0.0', 61372) s2 = socket.socket() # create client socket s2.connect(('localhost', 61372)) # connect to the one above s3, x = s.accept() # Python specific use; s3 is our connected socket on the server side s2.getpeername() # gives ('127.0.0.1', 61372) s2.getsockname() # gives ('127.0.0.1', 54663) s3.getsockname() # gives ('127.0.0.1', 61372), the same as s2.getpeername(), for symmetry s3.getpeername() #gives ('127.0.0.1', 54663), the same as s2.getsockname(), for symmetry #test the connection s2.send('hello') print s3.recv(10) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload