I have been trying to implement a small SERVER – CLIENT app but ran in to a errrr:
socket.error: [Errno 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
Server:
class Server:
gate = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 0
file = ''
def __init__(self, port):
self.port = port
self.gate.bind((self.host, self.port))
self.listen()
def listen(self):
self.gate.listen(10)
while True:
add = self.gate.accept()
self.reciveFileName()
self.reciveFile()
def reciveFileName(self):
while True:
data = self.gate.recv(1024)
self.file = data
def reciveFile(self):
createFile = open("new_"+self.file, "wb")
while True:
data = self.gate.recv(1024)
createFile.write(data)
createFile.close()
a = Server(1111)
Client:
class Client:
gateway = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#host = socket.gethostname()
host = ''
port = 0
file = ''
def __init__(self, host, port, file):
self.port = port
self.host = host
self.file = file
self.connect()
def connect(self):
self.gateway.connect((self.host, self.port))
self.sendFileName()
self.sendFile()
def sendFileName(self):
self.gateway.send("name:" + self.file)
def sendFile(self):
readByte = open(self.file, "rb")
data = readByte.read()
readByte.close()
self.gateway.send(data)
self.gateway.close()
a = Client('93.103.56.251', 1111, 'data.txt')
A server has one server socket that is used to
acceptincoming connections, and another socket for each of these connections. From your code:Instead of operating on self.gate in
reciveFilenameandreciveFile(by the way, the proper spelling isreceive), you should make these methods operate on the accepted connection, like this:Also,
reciveFilenamecurrently listens forever on the socket. Instead, you must design a proper protocol that specifies when the filename is over and the file content begins. For example, you can let the file name end withb'\0'and let the server search for that byte.