I'm trying to write a python function that sends an UDP message to a remote host and receives a reply, but I have a really hard time understaning how to do this.
I've been looking at thread: Simple Python UDP Server: trouble receiving packets from clients other than localhost
I understand how to send something, but how to send AND receive in the correct sequence?
Thanks in advance.
Please ignore, I got it..
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 3001
MESSAGE = "asd"
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
Related
I copied the echo server example from the python documentation and it's working fine. But when I edit the code, so it wont send the data back to the client, the socket.recv() method doesn't return when it's called the second time.
import socket
HOST = ''
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(b'ok')
conn.close()
In the original version from the python documentation the while loop is slightly different:
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
Client's code:
import socket
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))
TCP sockets are streams of data. There is no one-to-one correlation between send calls on one side and receive calls on the other. There is a higher level correlation based on the protocol you implement. In the original code, the rule was that the server would send exactly what it received until the client closed the incoming side of the connection. Then the server closed the socket.
With your change, the rules changed. Now the server keeps receiving and discarding data until the client closes the incoming side of the connection. Then the server sends "ok" and closes the socket.
A client using the first rule hangs because its expecting data before it closes the socket. If it wants to work with this new server rule, it has to close its outgoing side of the socket to tell the server its done, and then it can get the return data.
I've updated the client and server to shutdown parts of the connection and also have the client do multiple recv's in case the incoming data is fragmented. Less complete implementations seem to work for small payloads because you are unlikely to get fragmentation, but break horribly in real production code.
server
import socket
HOST = ''
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(b'ok')
conn.shutdown(socket.SHUT_WR)
conn.close()
client
import socket
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
s.shutdown(socket.SHUT_WR)
data = b''
while True:
buf = s.recv(1024)
if not buf:
break
data += buf
s.close()
print('Received', repr(data))
The number of receive and send operations have to match because they are blocking. This is the flow diagram for your code:
Server listen
Client connect
Server receive (this waits until a message arrives at the server) [1]
Client send 'Hello world' (received by [1])
Server receive (because there was data received) [2]
Client receive [3]
Because the server and the client are blocked now, no program can continue any further.
The fix would be to remove the client's receive call because you removed the server's send call.
I am trying to create a small program that sends a Hello World to my laptop from my Raspberry Pi, I am on the same network however, the code below seems not to work. I feel like it might be a firewall issue.
Raspberry Pi's code
import socket
ip = "127.0.0.1"
port = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
message = b"Hello World"
sock.sendto(message, (ip, port))
Laptop's code
import socket
ip = "127.0.0.1"
port = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
while True:
data, addr = sock.recvfrom(1024)
print("received message: %s" % data)
There are no error messages, just no data is displayed.
Should be noted that when both of these files are ran on the same machine they work however, when on two different machines they do not.
I try to communicate 2 raspberry pi's and I use UDP server. While creating udp server I used the codes below, however I took an error as it requires an integer at the line 10. Can you help me please ?
This is for Raspberry pi 3 and I try udp server.
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = "5005"
MESSAGE ="1"
print "UDP target IP:", UDP_IP
print " UDP target port:", UDP_PORT
print"message:", MESSAGE
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
I expect to create the sending server.
welcome to StackOverflow! I solved this by changing UDP_PORT to 5005 instead of "5005".
#/usr/bin/python
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE ="1"
print "UDP target IP:", UDP_IP
print " UDP target port:", UDP_PORT
print"message:", MESSAGE
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
It's asking for an integer and interpreting your argument "5005" as a string.
Im trying to create a script that will send a specific amount of packets with tcp/udp
i used the next script as a start to check how the socket module works and its working. but the massage is not at the size of 1 packet.
can anyone help?
Client:
import socket
UDP_IP = "0.0.0.0" # = 0.0.0.0 u IPv4
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
print "Socket is connected!", sock
for x in range(0,300):
print "send"
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
I'm using the Python socket library to send a message to another server and measure the roundtrip latency. I'm new networks so it's very basic, just using the send-receive examples given in the socket docs (http://wiki.python.org/moin/UdpCommunication). For the moment I'm using time.time() to timestamp when the message goes out and when it comes back, though I want to be able to get nanosecond accuracy in the end. Right now, I get about 300microseconds through my python script but pinging from the shell yields about 100micros. What can I do to get faster communication and/or more accurate measurement?
Script that sends one message:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
print "SENT {0}".format(int(time.time()*1000*1000)
This is the script that listens for the ping to come back
import socket
UDP_IP = "originating side localhost ip"
UDP_PORT = 9112
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
print "received message: {0} {1}".format(data, int(time.time()*1000*1000)
This is the script that listens on the server being pinged. When it receives a message on the port, it sends a message right back
import socket
UDP_IP = "pinged server's localhost ip"
UDP_PORT = 9111
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
UDP_IP2 = "ip of server that sent ping"
UDP_PORT2 = 9112
MESSAGE = "HEY"
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print "Listening on PORT 9111"
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
sock.sendto(MESSAGE, (UDP_IP2, UDP_PORT2))
print "received message: ", data