2.7 – Socket Programming: Creating Network Applications
2.7.1 – Socket Programming with UDP
- A host may be running many network application processes, each with one or more sockets, it is also necessary to identify the particular socket in the destination host.
- When a socket is created, an identifier, called a port number is assigned to it.
- The packet’s destination address on a packet contains the IP address and the socket’s port number. The source IP address and source port number is also attached.
- Adding the source address is often not done by UDP but the underlying OS.
- The following simple client-server application to demonstrate socket programming:
- The client reads a line of characters (data) from its keyboard and sends the data to the server
- The server receives the data and converts the characters to uppercase
- The server sends the modified data to the client
- The client receives the modified data and displays the line on its screen
- UDP Client program:
- Code:
- from socket import *
- serverName = ‘hostname’
- serverPort = 12000
- clientSocket = socket(AF_INET, SOCK_DGRAM)
- message = raw_input(‘Input lowercase sentence:’)
- clientSocket.sendto(message.encode(), (serverName, serverPort)
- modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
- Print(modifiedMessage.decode())
- clientSocket.close()
- Line 1 imports all the modules needed for the program.
- Line 2 and 3 sets the hostname of the server and the port number it uses.
- Line 4 creates the socket. The first parameter specifies the address family (IPV4 in this case). The second parameter specifies the socket type (UDP socket in this case).
- Line 5 is the message we want to send. Raw_input prompts the user to write in a lower case message.
- Line 6 firsts encode the string type to byte type then attaches the address (hostname and port) to the message and sends it to the process’s socket.
- Line 7 receives packets from the internet. The message is put into modified message and the address and port number is put into serverAddress. The recvfrom(2048) sets the buffer size to 2048.
- Line 8 Prints out the message after converting it from byte to string type.
- Line 9 closes the socket and the process is terminated.
- UDP Server program:
- Code:
- from socket import *
- serverPort = 12000
- serverSocket = socket(AF_INET, SOCK_DGRAM)
- serverSocket.bind = ((‘’, serverPort))
- print(“The server is ready to receive)
- While true
- message, clientAddress = serverSocket.recvfrom(2048)
- modifiedMessage = message.decode().upper()
- serverSocket.sendto(modifiedMessage.encode(), clientAddress)
- Line 1, 2 and 3 is the same as in the client.
- Line 4 binds the port to the server socket, thus the code is explicitly assigning a port to the socket. Which means when anyone sends a packet to the port it will be directed to the socket.
- Line 5 Just prints a message to the terminal that its operational
- Line 6 is loops the main part of the server such that its always ready to receive packets.
- Line 7 saves the message from the received packet to the message variable, and the source IP address and source port number to the clientAddress variable which will be used later as a return address.
- Line 8 converts the byte type to a string type and converts the lowercase text to uppercase text.
- Line 9 attaches the clients address and port to the message and sends it to the server’s socket.
2.7.2 – Socket Programming with TCP
- TCP is a connection-oriented protocol, which means that before the client and server can start to send data to each other, they first need to handshake and establish a TCP connection (we attach the IP address and port number when establishing it).
- One end of the TCP connection is attached to the client socket and the other end is attached to the server socket.
- With the TCP connection established, when one side wants to send data to the other side, it just drops the data into the TCP connection via its socket.
- With the server process running, the client process can initiate a TCP connection to the server. This is done in the client program by creating a TCP socket. When the client creates its TCP socket, it specifies the address of the welcoming socket in the server, namely, the IP address of the server host and the port number of the socket.
- After creating its socket, the client initiates a three-way handshake and establishes a TCP connection with the server. The three-way handshake is invisible to the client and server program.
- From the applications perspective, the client’s socket and the server’s connection socket are directly connection by a pipe.
- The client process can send arbitrary bytes into its socket, and TCP guarantees that the server process will receive each byte in the order sent.
- TCP client program:
- Code:
- from socket import *
- serverName = ‘serverName’
- serverPort = 12000
- clientSocket = socket(AF_INET, SOCK_STREAM)
- clientSocket.connect((serverName,serverPort))
- sentence = raw_input(‘Input lowercase sentence:’)
- clientSocket.send(sentence.encode())
- modifiedSentence = clientSocket.recv(1024)
- print(‘From Server: ‘, modifiedSentence.decode())
- clientSocket.close()
- Line 1, 2, 3 is the same as in the UDP programs
- Line 4 creates the socket. The first parameter specifies the address family (IPV4 in this case) and the second parameter indicates that the socket is of the type SOCK_STREAM, which means it is a TCP socket.
- Line 5 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 connection is established.
- Line 6 Obtains the message from the user.
- Line 7 converts the message to bytes and drops it into the TCP connection.
- Line 8 When characters arrive from the server, they get placed into the string. Recv(2048) sets the buffer size to 2048.
- Line 9 Prints the message.
- Line 10 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.
- TCP server program:
- Code:
- From socket import *
- serverPort = 12000
- serverSocket = socket(AF_NET, SOCK_STREAM)
- socerSocket.bind((‘’, serverPort))
- serverSocket.listen(1)
- print(‘The server is ready to receive’)
- while true:
- connectionSocket, addr = serverSocket.accept()
- sentence = connectionSocket.recv(1024).decode()
- capitalizedSentence = sentence.upper()
- connectionSocket.send(capitalizedSentence.encode())
- ConnectionSocket.close()
- Line 1, 2, 3 Is the same as in the client.
- Line 4 we associate the server port number, with the socket.
- Line 5 serverSocket is our welcoming socket. After establishing this welcome door, we will wait and listen for clients to knock on the door.
- Line 6 prints a message to the console that the server is ready
- Line 7 a while loop that loop and loop to listen for packets.
- Line 8 When a client knocks on this door, the program invokes the accept() function which creates a new socket in the server called connectionSocket dedicated to the client. Thus creating a TCP connection between the clientSocket and the connectionSocket.
- Line 9 Receive bytes from the client and converts them to a string
- Line 10 makes the string uppercase
- Line 11 convert the message string to bytes and sends it back through the TCP connection
- Line 12 Closes the connectionSocket, but the serverSocket remains open for another client in the future.