Cant receive Data using Asyncore Python

343 views Asked by At

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()
1

There are 1 answers

0
Jean-Paul Calderone On BEST ANSWER

Your server accepts connections and then drops them on the floor:

def handle_accept(self):
    pair = self.accept()
    if pair is not None:
        sock, addr = pair
        print ('Incoming connection from %s' % repr(addr))

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

from twisted.internet.task import react
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from twisted.internet.endpoints import clientFromString, connectProtocol

class SendBytes(Protocol):
    def __init__(self, data):
        self.data = data
        self.done = Deferred()

    def connectionMade(self):
        self.transport.write(self.data)
        self.transport.loseConnection()

    def connectionLost(self, reason):
        self.done.callback(None)

def main(reactor, description, data):
    endpoint = clientFromString(reactor, description)
    connecting = connectProtocol(endpoint, SendBytes(data))
    def connected(proto):
        print("Successfully connected to {0}".format(proto.transport.getPeer().host))
        return proto.done
    connecting.addCallback(connected)
    return connecting

if __name__ == '__main__':
    react(main, [b"tcp:host=127.0.0.1:port=4444", b"DDDDDD"])

tx_server.py

from twisted.internet.task import react
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import serverFromString

class ReadBytes(Protocol):
    def connectionMade(self):
        print("Incoming connection from {}".format(self.transport.getPeer()))

    def dataReceived(self, data):
        print("Received {}".format(data))

def main(reactor, description):
    endpoint = serverFromString(reactor, description)
    endpoint.listen(Factory.forProtocol(ReadBytes))
    return Deferred()

if __name__ == '__main__':
    react(main, [b"tcp:interface=127.0.0.1:port=4444"])