UDP Server dengan Python
UDP Server dengan Python
Berikut ini contoh kode UDP server dengan bahasa Python. Kode ini dapat dijalankan di Windows, Linux dan Raspberry Pi
import socket
# bind all IP
HOST = '0.0.0.0'
# Listen on Port
PORT = 44444
#Size of receive buffer
BUFFER_SIZE = 1024
# Create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the host and port
s.bind((HOST, PORT))
while True:
# Receive BUFFER_SIZE bytes data
# data is a list with 2 elements
# first is data
#second is client address
data = s.recvfrom(BUFFER_SIZE)
if data:
#print received data
print('Client to Server: ' , data)
# Convert to upper case and send back to Client
s.sendto(data[0].upper(), data[1])
# Close connection
s.close()