samedi 13 novembre 2021

python socket.send() not working in if statement

I tried programming a simple console menu wich sends the string "screenshot" to the client if I press 1 and so on. Now when I press 1 it stops working and if I press Ctrl+C it just sends empty bytes (b' ').

Here is my Server:

import socket

s = socket.socket()
print ("Socket successfully created")
port = 2345
host = '0.0.0.0'
print ("socket binded to %s" %(port))

s.listen(5)
print ("socket is listening")

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((host, port))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            choice = input('Choice: ')

            if choice == 1:
                conn.sendall("screenshot")
                s.close()
                break

And here the client:

import socket

s = socket.socket()
port = 2345
host = 'xxx.xx.xx.xxx'

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((host, port))
    data = s.recv(1024)

print('Received', data)

I hope someone can help me with my question because i'am trying to solve that problem for 20hrs or more :/

Aucun commentaire:

Enregistrer un commentaire