![](https://csdnimg.cn/release/download_crawler_static/7008861/bg7.jpg)
2 BASIC SOCKET USAGE 7
an integer representing the maximum queued connection. This argument should be at least
1 and maximum, system-dependent, 5. Until now we have a socket with a proper bounded
address. When a connection request arrives, the server decide whether it will be accepted or
not. Accepting a connection is made through the accept() method. It takes no parameter
but it returns a tuple (clientsocket, address) where clientsocket is a new socket server
uses to communicate with the client and address is the client’s address. accept() normally
blocks until a connection is realized. This behavior can be overridden running the method in
a separate thread, collecting the new created socket descriptors in a list and process them in
order. Meantime, the server can do something else. The above mentioned methods are used
as follows:
sock.listen(5)
clisock, address = sock.accept()
The code instructs the socket on listening with a queue of five connections and accept all
incoming “calls”. As you can see, accept() returns a new socket that will be used in further
data exchanging. Using the chain bind-listen-accept we create TCP servers. Remember, a
TCP socket is connection-oriented; when a client wants to speak to a particular server it must
connect itself, wait until the server accepts the connection, exchange data then close. This
is modeling a phone call: the client dial the number, wait till the other side establish the
connection, speak then quit.
2.5 UDP sockets
We chose to deal with connectionless sockets separately because these are less common in
day to day client/server design. A datagram socket is characterized by a connectionless and
symmetric message exchange. Server and client exchange data packets not data streams,
packets flowing between client and server separately. The UDP connection resemble the
postal system: each message is encapsulated in an envelope and received as a separate entity.
A large message may be split into multiple parts, each one delivered separately (not in the
same order, duplicated and so on). Is the receiver’s duty to assemble the message.
The server
8
have a bind() method used to append a proper name and port. There are
no listen() and accept() method, because the server is not listening and is not accepts
connection. Basically, we create a P.O.Box where is possible to receive messages from client
processes. Clients only send packets, data and address being included on each packet.
Data packets are send and received with the sendto(data, address ) and recvfrom(buf-
fer [, flags ]) methods. First method takes as parameters a string and the server address
as explained above in connect() and bind(). Because is specified the remote end of the
socket there is no need to connect it. The second method is similar to recv().
2.6 Closing the socket
After the socket is no longer used, it must be closed with close() method. When a user is no
more interested on any pending data a shutdown may be performed before closing the socket.
The method is shutdown(how ), where how is: 0 if no more incoming data will be accepted, 1
will disallow data sending and a value of 2 prevent both send and receive of data. Remember:
always close a socket after using it.
8
Which process is the server and which is the client is hard to predict at socket level, because of the
symmetrical connection.