[go: up one dir, main page]

0% found this document useful (0 votes)
18 views4 pages

Udp Client

The document provides code snippets for creating UDP and TCP servers and clients in Python. The UDP server listens for incoming messages and responds with a greeting, while the TCP server accepts connections, receives data, and sends a response. Both client types connect to the respective servers and handle communication, including error handling and socket closure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Udp Client

The document provides code snippets for creating UDP and TCP servers and clients in Python. The UDP server listens for incoming messages and responds with a greeting, while the TCP server accepts connections, receives data, and sends a response. Both client types connect to the respective servers and handle communication, including error handling and socket closure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

UDP CLIENT:

import socket # Create a UDP socket server_socket =

socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to a specific address and port server_address

= ('localhost', 12345) server_socket.bind(server_address)

# Print a message indicating that the UDP server is listening

print('UDP server is listening on {}:{}'.format(*server_address)) while

True:

# Receive data and client address data, client_address =

server_socket.recvfrom(1024) print('Received data from {}:

{}'.format(client_address, data.decode()))

# Prepare a response message

response = 'Hello, client!'

# Send the response back to the client

server_socket.sendto(response.encode(), client_address)

UDP Server: import


socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)


server_address = ('localhost', 12345)
server_socket.bind(server_address)
print('UDP server is listening on {}:{}'.format(*server_address))

while True:
data, client_address = server_socket.recvfrom(1024)
print('Received data from {}: {}'.format(client_address, data.decode()))
response = 'Hello, client!'
server_socket.sendto(response.encode(), client_address)
SERVER SIDE TCP: import
socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 12345) try:
server_socket.bind(server_address)
server_socket.listen(1)
print('TCP server is listening on {}:{}'.format(*server_address))
print('Waiting for a connection...')
client_socket, client_address = server_socket.accept()
print('Connected to', client_address) data =
client_socket.recv(1024)
print('Received data from client:', data.decode())

response = 'Hello, client!'


client_socket.sendall(response.encode())

except Exception as e:
print('Error:', e) finally:
# Close the client socket and the server socket in the finally block
if 'client_socket' in locals():
client_socket.close()
server_socket.close() Clint side TCP: import
socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 12345) try:
client_socket.connect(server_address)
print('Connected to', server_address)
message = 'Hello, server!'
client_socket.sendall(message.encode())
data = client_socket.recv(1024)
print('Received response from server:', data.decode())
except Exception as e: print('Error:', e) finally:
# Close the socket in the finally block
client_socket.close()

You might also like