0% found this document useful (0 votes)
4 views2 pages

UDP and TCP Socket Programming

The document contains Python code for both UDP and TCP client-server implementations. The UDP client sends a lowercase message to the server, which responds with the message in uppercase, while the TCP client connects to the server, sends a message, and receives a response. Both server implementations listen on port 12000 and handle incoming messages accordingly.

Uploaded by

gabrielmloster45
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

UDP and TCP Socket Programming

The document contains Python code for both UDP and TCP client-server implementations. The UDP client sends a lowercase message to the server, which responds with the message in uppercase, while the TCP client connects to the server, sends a message, and receives a response. Both server implementations listen on port 12000 and handle incoming messages accordingly.

Uploaded by

gabrielmloster45
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UDP CLIENT

from socket import *

serverName = '<hostname>'
serverPort = 12000

clientSocket = socket(AF_INET, SOCK_DGRAM)

message = input('Input lowercase sentence: ')

[Link]([Link](), (serverName, serverPort))

modifiedMessage, serverAddress = [Link](2048)

print(modifiedMessage)

[Link]()

UDP SERVER

from socket import *

serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)

[Link](('', serverPort))

print('The server is ready to receive...')

while 1:
message, clientAddress = [Link](2048)
modifiedMessage = [Link]()
[Link](modifiedMessage, clientAddress)
TCP CLIENT

import socket

HOST = '[Link]' # The server's hostname or IP address


PORT = 12000 # The port used by the server

with [Link](socket.AF_INET, socket.SOCK_STREAM) as s:


[Link]((HOST, PORT))
[Link](b'Hello, world')
data = [Link](1024)

print('Received', repr(data))

TCP SERVER

import socket

HOST = '[Link]' # Standard loopback interface address (localhost)


PORT = 12000 # Port to listen on (non-privileged ports are > 1023)

with [Link](socket.AF_INET, socket.SOCK_STREAM) as s:


[Link]((HOST, PORT))
[Link]()
conn, addr = [Link]()
with conn:
print('Connected by', addr)
while True:
data = [Link](1024)
if not data:
break
[Link](data)

You might also like