ofek ofek
ofek ofek

Reputation: 11

Sending a float/integer over a socket

I wrote a srever and a client. The client asks the user to choose one of 4 commands:

NAME: Returns the server's name.
TIME: Returns the current time and date.
RAND: Returns a random number between 1 to 10.
EXIT: As it sounds...

By trying NAME/EXIT everything wokrs properly.
By using TIME it sends the error: 'a bytes-like object is required, not 'float''.
By using RAND it sends the error: 'a bytes-like object is required, not 'int''.

I've already tried to convert the float (when using TIME) to bytes and got the error: 'cannot convert 'float' object to bytes

In addition, I've tried also converting the RAND's integer to bytes and got no errors, but I don't know how to convert the bytes back to an integer (so I can't print it).

This is the server code:

    import socket
    import time
    import random
    
    def main():
    
        server_socket = socket.socket()
        server_socket.bind(('0.0.0.0', 8820))
    
        server_socket.listen(1)
    
        (client_socket, client_adress) = server_socket.accept()
    
        client_input = client_socket.recv(1024).decode()
    
        if client_input == 'TIME':
            client_socket.send(time.time())
        elif client_input == 'NAME':
            server_name = 'server2.6'
            client_socket.send(server_name.encode())
        elif client_input == 'RAND':
            client_socket.send(random.randint(0, 10))
        elif client_input == 'EXIT':
            client_socket.close()
            server_socket.close()
            pass
        else:
            error_message = 'Not one of the options'
            client_socket.send(error_message.encode())
    
        client_socket.close()
        server_socket.close()
    
    if __name__ == '__main__':
        main()

This is the client code:

    import socket
    
    def main():
    
        my_socket = socket.socket()
    
        my_socket.connect(('127.0.0.1', 8820))
    
        client_input = input('Enter your command (NAME\TIME\RAND\EXIT): ')
    
        my_socket.send(client_input.encode())
    
        data = my_socket.recv(1024).decode()
    
        print (data)
    
        my_socket.close()
    
    
    if __name__ == '__main__':
        main()

Upvotes: 1

Views: 2898

Answers (2)

Nikolaos Chatzis
Nikolaos Chatzis

Reputation: 1979

The most straightforward way to fix your code is to turn the float returned by time.time() and the int returned by random.randint() to str and then encode.

Try:

client_socket.send(str(time.time()).encode())
client_socket.send(str(random.randint(0, 10)).encode())

You may also want to add the following line before bind-ing in the server code to avoid the Address already in use errors, if you start your server often:

server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Upvotes: 1

bbworld1
bbworld1

Reputation: 21

To encode to bytes you can convert to string and specify encoding:

bytes(str(var), encoding="UTF-8")

This will allow you to send those floats and ints over the socket. For a more advanced/flexible solution you can use Python's struct library to pack data into an encoded object.

Converting the bytes back to int is a trivial matter:

int(bytes_var)

Upvotes: 0

Related Questions