0% found this document useful (0 votes)
216 views3 pages

Socket Programming - Client and Server PDF

The main differences are: 1. TCP uses SOCK_STREAM while UDP uses SOCK_DGRAM. SOCK_STREAM provides reliable, ordered and error-checked delivery of streams of bytes between the client and server. 2. TCP requires an accept call to accept an incoming connection before communication can take place, while UDP does not have the concept of connections. 3. TCP uses connection sockets while UDP uses datagram sockets. Connection sockets provide connection-oriented services (like TCP) and datagram sockets provide connectionless services (like UDP). 4. TCP provides flow control (preventing too fast sender), error control (using sequence numbers and acknowledgements) and congestion control while UDP does not provide these features.

Uploaded by

一鸿
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
216 views3 pages

Socket Programming - Client and Server PDF

The main differences are: 1. TCP uses SOCK_STREAM while UDP uses SOCK_DGRAM. SOCK_STREAM provides reliable, ordered and error-checked delivery of streams of bytes between the client and server. 2. TCP requires an accept call to accept an incoming connection before communication can take place, while UDP does not have the concept of connections. 3. TCP uses connection sockets while UDP uses datagram sockets. Connection sockets provide connection-oriented services (like TCP) and datagram sockets provide connectionless services (like UDP). 4. TCP provides flow control (preventing too fast sender), error control (using sequence numbers and acknowledgements) and congestion control while UDP does not provide these features.

Uploaded by

一鸿
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Client & Server Socket Programming

In this lab, we will write a simple client-server programs that use UDP and TCP.

Socket Programming with UDP


We will use the following simple client-server application to demonstrate socket
programming for both UDP and TCP:

Basically, at the end of this lab, you are supposed to achieve the following:

1. The client reads a line of characters (data) from its keyboard and sends the data to the
server.
2. The server receives the data and converts the characters to UPPERCASE.
3. The server sends the modified data to the client.
4. The client receives the modified data and displays the UPPERCASE characters on its
screen

Steps:
1. To start, create a new folder in your Desktop
2. Type the following codes using your text editor and save it as UDPClient.py and
UDPServer.py respectively in the created folder.
3. In order for a client and server to start communicating, you have to set one host as
a client and another host as a server. To do so, these two scripts have to run on two
different machines.
4. Put the IP address in the ‘hostname’ inside the UDPclient.py
5. Make sure the server is ready (on) before the client can contact and initiate the
connection.
a. To execute the server: type ‘python UDPServer.py’ in your terminal
6. Execute the client to connect to the server you have just created.
a. To execute the client: type ‘python UDPClient.py’ in your terminal
7. Follow the instruction that appears on the client’s screen.
8. What do you notice?
9. CTRL+C to exit.
UDPClient.py Type “ifconfig” to find out IP address in command prompt
from socket import *
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = raw_input(’Input lowercase sentence:’)
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()

UDPServer.py
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print “The server is ready to receive”
while 1:
message, clientAddress=serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage,clientAddress)
Repeat the same steps for TCP client and server.

Socket Programming with TCP


TCPClient.py
Type “ifconfig” to find out IP address in command prompt
from socket import *
serverName = 'hostname'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input('Input lowercase sentence:')
clientSocket.send(sentence)
modifiedSentence = clientSocket.recvfrom(1024)
print 'From Server:', modifiedSentence
clientSocket.close()

TCPServer.py

from socket import *


serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print 'The server is ready to receive'
while 1:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()

** What is the main difference in the coding of TCP compared to UDP?

You might also like