TCP Client and Server
TCP Client and Server
py
Here is the code for the client side of the application:
Let’s now take a look at the various lines in the code that differ significantly from the
UDP implementation. The first such line is the creation of the client socket.
This line creates the client’s socket, called clientSocket. The first parameter
again indicates that the underlying network is using IPv4. The second parameter
TCP
Wait for incoming connection setup Create socket, connect
connection request: to serverIP, port=x:
connectionSocket = clientSocket =
serverSocket.accept() socket()
Write reply to
Read reply from
connectionSocket
clientSocket
Close
Close
connectionSocket
clientSocket
indicates that the socket is of type SOCK_STREAM, which means it is a TCP socket
(rather than a UDP socket). Note that we are again not specifying the port number of
the client socket when we create it; we are instead letting the operating system do this
for us. Now the next line of code is very different from what we saw in UDPClient:
clientSocket.connect((serverName,serverPort))
Recall that before the client can send data to the server (or vice versa) using a TCP
socket, a TCP connection must first be established between the client and server. The
As with UDPClient, the above obtains a sentence from the user. The string
sentence continues to gather characters until the user ends the line by typing a
carriage return. The next line of code is also very different from UDPClient:
clientSocket.send(sentence.encode())
The above line sends the sentence through the client’s socket and into the TCP
connection. Note that the program does not explicitly create a packet and attach the
destination address to the packet, as was the case with UDP sockets. Instead the cli-
ent program simply drops the bytes in the string sentence into the TCP connec-
tion. The client then waits to receive bytes from the server.
modifiedSentence = clientSocket.recv(2048)
When characters arrive from the server, they get placed into the string
modifiedSentence. Characters continue to accumulate in modifiedSen-
tence until the line ends with a carriage return character. After printing the capital-
ized sentence, we close the client’s socket:
clientSocket.close()
This last line closes the socket and, hence, closes the TCP connection between the
client and the server. It causes TCP in the client to send a TCP message to TCP in
the server (see Section 3.5).
TCPServer.py
Now let’s take a look at the server program.
Let’s now take a look at the lines that differ significantly from UDPServer and TCP-
Client. As with TCPClient, the server creates a TCP socket with:
serverSocket=socket(AF_INET,SOCK_STREAM)
serverSocket.bind((’’,serverPort))
But with TCP, serverSocket will be our welcoming socket. After establish-
ing this welcoming door, we will wait and listen for some client to knock on the
door:
serverSocket.listen(1)
This line has the server listen for TCP connection requests from the client. The
parameter specifies the maximum number of queued connections (at least 1).
When a client knocks on this door, the program invokes the accept() method for
serverSocket, which creates a new socket in the server, called c
onnectionSocket,
dedicated to this particular client. The client and server then complete the hand-
shaking, creating a TCP connection between the client’s clientSocket and the
server’s connectionSocket. With the TCP connection established, the client
and server can now send bytes to each other over the connection. With TCP, all bytes
sent from one side are only guaranteed to arrive at the other side but also guaranteed
to arrive in order.
connectionSocket.close()
In this program, after sending the modified sentence to the client, we close the con-
nection socket. But since serverSocket remains open, another client can now
knock on the door and send the server a sentence to modify.