Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UDP Socket Issues
#1
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)
Reply
#2
Hm!
print unpack('>Q', data)
 ?

I am looking at struct module now. For the first time  Big Grin
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Here's a couple of documents that will help.
1st how to capture a UDP packet: http://stackoverflow.com/questions/32255...ith-python
Next what to do with it: http://www.networksorcery.com/enp/protocol/udp.htm
Reply
#4
(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/32255...ith-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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020