TCP Client and Server

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

TCPClient.

py
Here is the code for the client side of the application:

from socket import *


serverName = ’servername’
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = input(’Input lowercase sentence:’)
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024)
print(’From Server: ’, modifiedSentence.decode())
clientSocket.close()

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.

clientSocket = socket(AF_INET, SOCK_STREAM)

This line creates the client’s socket, called clientSocket. The first parameter
again indicates that the underlying network is using IPv4. The second parameter

M02_KURO5469_08_GE_C02.indd 191 03/05/2021 15:50


Server Client
(Running on serverIP)

Create socket, port=x,


for incoming request:
serverSocket =
socket()

TCP
Wait for incoming connection setup Create socket, connect
connection request: to serverIP, port=x:
connectionSocket = clientSocket =
serverSocket.accept() socket()

Send request using


Read request from clientSocket
connectionSocket

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

M02_KURO5469_08_GE_C02.indd 192 03/05/2021 15:50


above line initiates the TCP connection between the client and server. The parameter
of the connect() method is the address of the server side of the connection. After
this line of code is executed, the three-way handshake is performed and a TCP con-
nection is established between the client and server.

sentence = input(’Input lowercase sentence:’)

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.

from socket import *


serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((’’,serverPort))
serverSocket.listen(1)
print(’The server is ready to receive’)

M02_KURO5469_08_GE_C02.indd 193 03/05/2021 15:50


while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.encode())
connectionSocket.close()

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)

Similar to UDPServer, we associate the server port number, serverPort, with


this socket:

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).

connectionSocket, addr = serverSocket.accept()

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.

M02_KURO5469_08_GE_C02.indd 194 03/05/2021 15:50


This completes our discussion of socket programming in TCP. You are encour-
aged to run the two programs in two separate hosts, and also to modify them to
achieve slightly different goals. You should compare the UDP program pair with the
TCP program pair and see how they differ. You should also do many of the socket
programming assignments described at the ends of Chapter 2 and 5. Finally, we hope
someday, after mastering these and more advanced socket programs, you will write
your own popular network application, become very rich and famous, and remember
the authors of this textbook!

M02_KURO5469_08_GE_C02.indd 195 03/05/2021 15:50

You might also like