Server

#!/usr/bin/python3

import socket
from time import ctime

HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

udpSerSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpSerSock.bind(ADDR)

while(True):
    print('waiting for message...')
    data, addr = udpSerSock.recvfrom(BUFSIZ)
    udpSerSock.sendto(data, addr)
    print('...received from and returned to:', addr)

udpSerSock.close()
#END#

Client

#!/usr/bin/python3

import socket

HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

udpCliSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while(True):
    data = input('> ')

    if not data:
        break
    udpCliSock.sendto(data, ADDR)
    data, ADDR = udpCliSock.recvfrom(BUFSIZ)

    if not data:
        break
    print(data)

udpCliSock.close()
#END#

What is the relevance of BUFSIZE? When and why do I need to wory aobut BUFSIZ? Also, when I run this program this is the message I get on the client screen after typing anything and pressing enter.

udpCliSock.sendto(data, ADDR)
TypeError: 'str' does not support the buffer interface

One last thing, how could these to programs me modified so that the server and client simply sent and received a simple 'hello world'? Thanks.

BUFSIZE is exactly what its name implies. It is the buffer size. The maximum amount of data to send/receive in bytes.

Regarding the error message:
This error is exactly the same error I encountered and fixed in your other thread. It is to do with the strings used in python3's implementation of sockets.

Sockets use byte-encoded strings, whereas standard strings are in Unicode. So in order to send the string held in the variable called 'data' from your client to the server, you first need to convert the string to a byte string using the encode() function.

So line 17 of client.py becomes:
udpCliSock.sendto(data.encode(), ADDR)

Optionally, at line 22:
print(data.decode())
This converts the string received back from the server into a Unicode string. So for example, if the user typed hello; Without the call to decode, the bounced back string is printed as b'hello'. Which indicates that the string is still in byte-string format!

Whereas with the call to decode in place, the string is printed as standard. i.e.:
hello

WRT changing the program to send and receive a simple "hello world", is this what you want?
client.py:

#!/usr/bin/python3

import socket

HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

# Connect to server via a socket
udpCliSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# send some data to the server
data = "Hello world!"
udpCliSock.sendto(data.encode(), ADDR)

# wait for the server to send something back
data, ADDR = udpCliSock.recvfrom(BUFSIZ)

# print what we get back from the server
print(data.decode())

udpCliSock.close()
#END#

The above is a simple one-shot deal. Connect to the server (the source for the server is the same), send "Hello world!" to the server, the server sends "Hello world!" back to the client. The client then prints the string received from the server. We then close the connection and the client program ends.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.