0

I have made a class in python 3 and i can't figure why i can't send the information for the server to client. Server and client are using the same class.

class mysocket:
    receive_string_buffer_len = 0
    active_instance = 0

    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.receive_string_buffer = ''
        self.send_string_buffer = ''
        self.host = 'localhost'
        self.port = 30000 + self.active_instance    
        self.active_instance += 1

    def connect(self):
        self.sock.connect((self.host,self.port))

    def mysend(self):
        try:
            sent = self.sock.send(self.send_string_buffer)
        except socket.error:
            print('socket connection broken')

    def myreceive(self):
        try:
            self.receive_string_buffer = self.sock.recv(512)
        except socket.error:
            print('socket connection broken')
        finally: return self.receive_string_buffer

Client code:

Client_socket1 = mysocket()
Client_socket1.connect()
print(Client_socket1.myreceive().decode('ascii'))

Server code:

Server_socket1 = mysocket()
Server_socket1.bind(('', 30000))
Server_socket1.listen(1)
client1, add = Server_socket1.accept()
Server_socket1.send_string_buffer = ' alo '
Server_socket1.mysend().encode('ascii')

The problem is that it's not working. I am new to python programing and new to sockets so if i done stupid mistakes please tell me .

Thanks to anyone that will read this.

2 Answers 2

1

You are sending data on the listening socket instead of the client-server socket returned by accept().

Rgds, Martin

Sign up to request clarification or add additional context in comments.

Comments

1

I dont think "Server_socket1.mysend().encode('ascii')" is valid especially since mysend() doesn't return anything to encode (and you do nothing with return value from encode()). Also you need to encode your data before it can be sent.

I think you will find asynchat module much easier to handle sockets. Just sub class it like:

import threading

class mysocket(asynchat.async_chat):
    terminator = b'\n'
    def __init__(self,sock=None):
        asynchat.async_chat.__init__(self,sock)
        self.create_socket()
        self.connect(('127.0.0.1',6667))

    def handle_connect(self):
        pass
    def handle_close(self):
        pass
    def collect_incoming_data(self, data):
        pass    
    def found_terminator(self):
        pass
    def sockwrite(self,text=None):
        # Avoid conflict with text=''
        if (text == None):
            text = ''
        text += '\n'
        self.sendall(bytes(text,'latin-1'))

chatsock = {}
def main():
    chatsock['a'] = mysocket()
    socketloop = threading.Thread(target=asyncore.loop, daemon=1)
    socketloop.start()
    while True: 
        pass
if __name__ == "__main__":
    main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.