Reputation: 3078
I am traing to make simple application on python that works with sockets and use UDP protocol. I have a client.py and server.py. I whant to send messages from client to server in infinity loop and catch resive message from server. For making this I have following code:
server.py
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 8000
print(host)
print(port)
serversocket.bind((host, port))
serversocket.listen(5)
print('server started and listening')
while 1:
(clientsocket, address) = serversocket.accept()
print("connection found!")
data = clientsocket.recv(1024).decode()
print(data)
r = 'I can hear you by UDP!!!!'
clientsocket.send(r.encode())
client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 8000
addr = (host, port)
s.connect(addr)
def send(user_input):
s.sendto(user_input.encode(), addr)
data = s.recv(1024).decode()
print('Server tell that: ', data)
while 1:
if input('Exit? (y/n)') == 'y':
break
i = input('>> ')
send(i)
s.close()
It's work correctly, but when i try to send message second time, my program make stop on line
s.sendto(user_input.encode(), addr)
I don't understand what's happening in this moment.
Upvotes: 0
Views: 2428
Reputation: 10729
If you'd like to build server/client for UDP, you need to read the documents carefully to prevent from using the APIs which are for connection-oriented protocols.
Below is one working sample for UDP: (I added some comments in the codes, then you will see why adjusted them).
Server:
For one UDP server, you need to initialize with one socket socket.SOCK_DGRAM
instead of SOCK_STREAM
, then bind the port you'd like.
import socket
#The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols.
serversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = "127.0.0.1"
port = 8000
print(host)
print(port)
serversocket.bind((host, port))
#serversocket.listen(5) #--This method sets up and start TCP listener.
print('server started and listening')
while 1:
#(clientsocket, address) = serversocket.accept() #---This passively accept TCP client connection, waiting until connection arrives (blocking)
#print("connection found!")
#data = clientsocket.recv(1024).decode() #This method receives TCP message
data, addr = serversocket.recvfrom(2048)
print(data)
r = 'I can hear you by UDP!!!!'
serversocket.sendto(r.encode(), addr)
serversocket.close()
Client:
For one UDP client, you need to initialize one socket with socket.SOCK_DGRAM
instead of SOCK_STREAM
, then uses sendto
to send the data to the server.
import socket
#The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = "127.0.0.1"
port = 8000
addr = (host, port)
#s.connect(addr) #---This method actively initiates TCP server connection.
def send(user_input):
s.sendto(user_input.encode(), addr)
data = s.recvfrom(1024).decode()
print('Server tell that: ', data)
while 1:
if input('Exit? (y/n)') == 'y':
break
i = input('>> ')
send(i)
s.close()
For more details:
Check Python Socket
Upvotes: 1