Jump to content

Recommended Posts

Posted

Hi all

 

I'm trying to write a program in python which needs to receive a UDP message. I have found this code on the WWW which should work

Apparently it works fine in Python 2 but won't work in Python3 because the storage of the string variables is different - it needs to have the ip address in byte-like object

 

so - error message is

 

sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

TypeError: a bytes-like object is required, not 'str'

 

 

code is

 

import socket

UDP_IP = "127.0.0.1"

UDP_PORT = 5000

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("before sendto")

sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

 

any ideas how I can convert the UDP_IP string variable so that the sock.send works??

 

TIA

 

Mike

Posted

If you do udp_ip.encode() it should change it to a byte last time I did it don’t think it’s changed

 

Shout if not and will try it when on pc

 

Steve

  • Thanks 1
Posted

Thanks - I have now encoded the message as well as the ip address - and this now runs OK

 

however the recv end doesn't seem to get the message - so I have moved forward

 

and yes - it does look like it was the message that needed encoding

 

I will post more when I make some progress

Posted

Just checking my understanding here before I try again

 

Many years ago I was writing and supporting quite a few networked systems all using TCP

 

These all had one design concept in common i.e. there was a server program which opened a socket and waited for a connection from any system on the rest of the network

Then a client system would open a socket which specified the server's ip address and port and requested a connection

Then the 2 could send and receive in whatever way the system required

 

Ok - that was what I used to do and it always worked - but what I am trying to do now HAS to use UDP not TCP

 

but - my question is - is there still the concept in UDP of server that waits - and client that connects??

 

asking because the things I have seen on the WWW both seem to specify IP addresses rather than one just waiting

 

Thanks

 

Mike

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...