Python Forum
UDP Socket Issues - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: UDP Socket Issues (/thread-2731.html)



UDP Socket Issues - hellboy632789 - Apr-05-2017

Hello, I am trying to figure out how to properly print the data from a UDP packet. For homework our professor has a server running that when it receives the string 'time' formatted as ascii (which from my understanding I don't need to actually do anything. It will automatically be formatted to that) that the server will then send a datagram containing the time since epoch in milliseconds as an unsigned int long long. He wanted us to use structs to unpack the datagram. The datagram will be 8 bytes, but if the server receives anything but 'time' it will send a one byte datagram containing \x00. So my problem here is, I can not figure out how to properly unpack and print the datagram. When I unpack it as a unsigned long long, it is just a bunch of numbers and I know that is not correct. I do believe I am connecting correctly, because I checked the size of the datagrams and they were either 8 bytes or 1 byte. So I am receiving them, but can't figure out how to properly unpack them to read them. Also the sad part is that this only works if you are on the same network. So none of you can actually test the code :/ 

import socket
import sys
from struct import *
address = socket.gethostbyname('hawk.champlain.edu')
port = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

message = 'time'
sock.sendto(message, (address, port))

data, server = sock.recvfrom(4096)
length = len(data)
if length == 8 :
    print unpack('Q',data)



RE: UDP Socket Issues - wavic - Apr-05-2017

Hm!
print unpack('>Q', data)
 ?

I am looking at struct module now. For the first time  Big Grin


RE: UDP Socket Issues - Larz60+ - Apr-05-2017

Here's a couple of documents that will help.
1st how to capture a UDP packet: http://stackoverflow.com/questions/32255661/how-to-capture-udp-packets-with-python
Next what to do with it: http://www.networksorcery.com/enp/protocol/udp.htm


RE: UDP Socket Issues - hellboy632789 - Apr-05-2017

(Apr-05-2017, 06:13 PM)Larz60+ Wrote: Here's a couple of documents that will help.
1st how to capture a UDP packet: http://stackoverflow.com/questions/32255661/how-to-capture-udp-packets-with-python
Next what to do with it: http://www.networksorcery.com/enp/protocol/udp.htm

Sadly I didn't find much help in the second document. I guess I forgot to do bind, so now that I have that hopefully I am actually connected. The second document is confusing to me (in fact all this networking stuff is crazy confusing)

oh my god I am a dunce. I have been stuck for 3 hours, when all I had to do was put a ! next to the Q. I totally forgot that my professor said in class to not forget to do that or the format won't work. This is the truth of a programmer, one tiny thing that makes you feel dumb when you figure out the answer