Sie sind auf Seite 1von 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?

Das könnte Ihnen auch gefallen