I am just beginning to program UDP servers and clients in Python. I have followed a tutorial and added some of my own knowledge to create a command-line chat server. Everything has worked as planned except for one thing: when my client sends a message to the server, the server prints it with a b' in front of the actual message. My client gets his message sent back to him with the b' in front. I have tried decoding the message in the client, but then it never sends. I have tried decoding the message when the server is going to send it back, but it never sends back to the client. My reason for wanting this removed is because it ruins the look I wanted for the program. Thank you for any help! Below is my code for first the server and then the client. After my code I have pasted some of the output I have gotten so you can see exactly what I am talking about.
#Server
from socket import *
import time
address = input("IP Address: ")
port = input("Port: ")
clients = []
serversock = socket(AF_INET, SOCK_DGRAM)
serversock.bind((address, int(port)))
serversock.setblocking(0)
quitting = False
print("Server is up and running so far.")
while not quitting:
try:
data, addr = serversock.recvfrom(1024)
if "Quit" in str(data):
quitting = True
if addr not in clients:
clients.append(addr)
print(time.ctime(time.time()) + str(addr) + ": :" + str(data))
for client in clients:
serversock.sendto(data, client)
except:
pass
serversock.close()
#Client
from socket import *
import threading
import time
tLock = threading.Lock()
shutdown = False
def receiving(name, sock):
while not shutdown:
try:
tLock.acquire()
while True:
data, addr = sock.recvfrom(1024)
print(str(data))
except:
pass
finally:
tLock.release()
address = input("IP Address: ")
port = 0
server = address, 6090
clientsock = socket(AF_INET, SOCK_DGRAM)
clientsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
clientsock.bind((address, int(port)))
clientsock.setblocking(0)
rT = threading.Thread(target=receiving, args=("RecvThread", clientsock))
rT.start()
nick = input("How about we get you a nickname: ")
message = input(nick + "> ").encode()
while message != "q":
if message != "":
clientsock.sendto(nick.encode() + "> ".encode() + message, server)
tLock.acquire()
message = input(nick + "> ").encode()
tLock.release()
time.sleep(0.2)
shutdown = True
rT.join()
clientsock.close()
How the server receives the message: Sun Oct 30 18:23:22 2016('192.168.1.66', 61249): :b'jake> jake'