Hi im new to python programming. May i know why my server ain't receiving msgs sent from my client? Im trying to send data from the client to the server and make the server display it in the console...
Client.py
import asyncore
import socket
class Client(asyncore.dispatcher):
def __init__(self,IPAddress_Server,intServerPort):
asyncore.dispatcher.__init__(self)
self.IPHostEntry_Server = (IPAddress_Server,intServerPort)
self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
self.set_reuse_addr()
self.connect(self.IPHostEntry_Server)
def handle_connect(self):
print("Successfully connected to {0}".format(self.IPHostEntry_Server))
def handle_write(self):
bytesTx = b"DDDDDDD"
self.send(bytesTx)
self.close()
client = Client("127.0.0.1", 4444)
asyncore.loop()
Server.py
import asyncore
import socket
class Server(asyncore.dispatcher):
def __init__(self,IPAddress_Server,intServerPort):
asyncore.dispatcher.__init__(self)
self.IPHostEntry_Server = (IPAddress_Server,intServerPort)
self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(self.IPHostEntry_Server)
self.listen(5)
def handle_accept(self):
pair = self.accept()
if pair is not None:
sock, addr = pair
print ('Incoming connection from %s' % repr(addr))
def handle_read(self):
bytesRx = self.recv(4)
print("%s" %bytesRx)
server = Server("127.0.0.1", 4444)
asyncore.loop()
Your server accepts connections and then drops them on the floor:
The server socket is separate from each of the connections accepted by the server socket.
If you want to read bytes from the connection then you have to do some more work to handle read events on it.
I suggest taking a look at Twisted instead. It is more actively maintained, better documented, and more featureful than asyncore. Here's a minimal Twisted-based example that does what I think you want your app to do:
tx_client.py
tx_server.py