Reputation: 1
I'm trying to make an online Python game, and am currently working on the server file and the network class (responsible for connecting server to client). It's been going okay, but I've been trying to send something from the network file back to the server, but it isn't working.
I tried putting it in a try/except loop and had it print the error. Now, the console prints out this.
None
[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
A fine day to you my friend!
None
Process finished with exit code 0
The client file:
import socket
IPV4 = socket.AF_INET
TCP = socket.SOCK_STREAM
SERVER = "192.168.1.77" # Replace with the ip address of the server
PORT = 5555
BITS = 2048
class Network:
def __init__(self):
self.client = socket.socket(IPV4, TCP)
self.server = SERVER
self.port = PORT
self.address = (SERVER, PORT)
self.id = self.connect()
print(self.id)
# So the idea is that when we decode the message on line 30 (rewrite this later), it will give us the string
# "Connected" to self.id, as it calls the function self.connect, which returns the message.
# self.id # This would be so that we could give an id to each player, and send specific things to each player
def connect(self):
try:
self.client.connect(self.address) # Connects our client to the server on the specified port
return self.client.recv(BITS).decode()
# Ideally when we connect we should send some form of validation token
except:
pass
def send(self, data):
try: # I think the problem is here!!!!
self.client.send(str.encode(data))
return self.client.recv(BITS).decode()
except socket.error as e:
print(e)
print("A fine day to you my friend!")
n = Network()
print(n.send("hello"))
# print(n.send("working"))
The problem comes with the send function if I'm not mistaken. The error I receive is a result of me trying to encode and send the data (self.client.send(str.encode(data)). It then gives me the error message above.
The server code is:
import socket
from _thread import *
import sys
SERVER = "192.168.1.77" # (For now) the private ipv4 address of my computer (localhost)
PORT = 5555
MAX_PLAYERS = 2
BITS = 2048
IPV4 = socket.AF_INET
TCP = socket.SOCK_STREAM
# Setting up the socket
s = socket.socket(IPV4, TCP) # The arguments are the address family and socket type.
# AF_INET is the address family for Ipv4, and SOCK_STREAM is the socket type for TCP connections
try: # There is a chance that the port may be being used for something, or some other error may occur. If so, we want to find out what
s.bind((SERVER, PORT))
except socket.error as e: # This will
str(e)
s.listen(MAX_PLAYERS) # Opens up the port for connections
print("Waiting for a connection, Server Started")
def threaded_client(connection):
connection.send(str.encode("Connected")) # Sends an encrypted message to the client
reply = ""
while True:
try:
data = connection.recv(BITS)
reply = data.decode("utf-8") # Decodes the encrypted data
if not data: # If we try to get some info from the client and we don't, we're going to disconnect
print("Disconnected")
break # and break out of the try loop
else:
print("Received: {}".format(reply))
print("Sending: {}".format(reply))
connection.sendall(str.encode(reply)) # Sends our encrypted reply
except:
break
# Add possible errors when they occur
print("Lost connection")
connection.close()
while True:
connection, address = s.accept() # Accepts incoming connections and stores the connection and address
# Note: the connection is an object and the address is an ip address
print("Connected to {}".format(address))
start_new_thread(threaded_client, connection)
Ideally, the results (assuming I started up the server file and it is running without any errors) are as follows:
Connected
hello
To explain a little further... The reason I get "Connected" is because in the connect method I recieve an encrypted message from the server, which I decode and return to self.id. self.id is then printed, and so shows that it is connected to the server.
Upvotes: 0
Views: 1081
Reputation: 177685
The server gets an error as well and is the cause of the problem:
Waiting for a connection, Server Started
Connected to ('127.0.0.1', 1930)
Traceback (most recent call last):
File "C:\server.py", line 54, in <module>
start_new_thread(threaded_client, connection)
TypeError: 2nd arg must be a tuple
Use the following instead:
start_new_thread(threaded_client, (connection,))
Note that TCP is a streaming protocol and has no concept of message boundaries, so you will eventually have problems with sending multiple messages at a time if you don't design a protocol into the stream to determine where messages begin and end.
Upvotes: 1