9

I'm experimenting a little with creating a socket server, in PHP. In doing so I'm trying to abstract away the kinds of sockets I think I'll be needing, that I've tentitively named:

  • ListenSocket — the 'master' socket, that is bound to an address/port and listens for and accepts incoming connections.
  • CommunicationSocket — a 'child' socket, that represents an accepted, incoming connection, which the server will communicate with.

As the name already kind of implies, I will only define read() and write() type of methods on the CommunicationSocket.

However, doing so got me wondering: does it ever make sense to read from or write to the first type of socket (the bound, listening type)?

1
  • Java uses a model like this, and it seems to work OK. Commented Jun 25, 2015 at 9:58

1 Answer 1

3
+50

I interpret your question in a way that you refer to TCP/IP sockets. With UDP/IP sockets it is different.

Two scenarios:

Scenario 1: 1. bind 2. connect 3. listen
Result: You cannot listen at a connected socket.

>>> import socket >>> s = socket.socket() >>> s.bind(('', 0)) >>> s.send(b'a') Traceback (most recent call last): File "<pyshell#130>", line 1, in <module> s.send(b'a') OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied >>> s.connect(('google.de', 80)) >>> s.listen(1) Traceback (most recent call last): File "<pyshell#132>", line 1, in <module> s.listen(1) OSError: [WinError 10056] A connect request was made on an already connected socket 

Scenario 2: 1. bind 2. listen 3. connect
Result: You can not connect or send at a listening socket.

>>> s = socket.socket() >>> s.bind(('', 0)) >>> s.listen(1) >>> s.connect(('google.de', 80)) Traceback (most recent call last): File "<pyshell#136>", line 1, in <module> s.connect(('google.de', 80)) OSError: [WinError 10022] An invalid argument was supplied >>> s.send(b"123") Traceback (most recent call last): File "<pyshell#137>", line 1, in <module> s.send(b"123") OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied 
2
  • Ah right, so, bind() is not only meant for listening for incoming connections (act as a server) but also to connect to a server (act as a client), correct? That actually makes sense, of course. So, concluding, we can't write to listening or unconnected sockets. I'm not sure I completely understand the question you pose in the end, though. Is it pertaining to the fact that when acting as a server you'll get a new socket per client, in stead of reading/writing to the already existing listening socket? And was it meant as a rhetorical question, perhaps? Commented Jul 2, 2015 at 11:30
  • I deleted the question.. I do not remember why i posted it. bind is used to assign a port to a socket. You need a port if you want to listen. You can specify a port for the socket before you connect from a specific port to a specific port, which makes no sense in many cases for TCP but may make sense for UDP sockets. Commented Jul 2, 2015 at 13:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.