Search code examples
pythonsocketsprocesssendrecv

Calling .recv(1024) and .send() twice, nothing happening (Python)


I am trying to learn Socket coding right now, and I wrote a little piece of Process-to-Process communication. This is the Servercode:

import socket
s = socket.socket()
host = socket.gethostname()
port = 17752
s.bind((host, port))

s.listen(5)
while True:
    (client, address) = s.accept()
    print(address, 'just connected!')
    message = input("Would you like to close the connection? (y/n)")
    if message == 'y':
        message = "False"
        client.send(message.encode(encoding="utf_8"))
        client.close()
        break
    elif message == 'n':
        print("sending message...")
        testing = "Do you want to close the connection?"
        client.send(testing.encode(encoding='utf_8'))
        print("sent!")

And the Clientcode:

import socket

client = socket.socket()
host = socket.gethostname()
port = 17752

client.connect((host, port))

while True:
    print("awaiting closing message...")
    closing = client.recv(1024)
    closing = closing.decode(encoding='utf_8')
    print("Closing message recieved and decoded")
    if closing == 'False':
        print("message is false, breaking loop")
        break
    else:
        print("Awaiting message...")
        recieved = client.recv(1024)
        recieved = recieved.decode(encoding='utf_8')
        print("Message recieved and decoded")
        print(recieved)
        sd = input('(y/n) >')
        if sd == 'y':
            print("Closing connection")
            client.close()
            break

print("Sorry, the server closed the connection!")

What it is meant to do?

It is basically to learn and practice socket coding. It should be a program that sends data from the Server to the Client with both being able to terminate the connection by answering y or n to the questions. If both sides keep answering n the program just keeps running. As soon as someone answers y it terminates either the Server or the client.

Now, I don't know what to heck is wrong there. If I type 'y' for the Servers question "Would you like to close this connection?" it all works as it should.

If I type 'n' the Server does what it should, but the client does not recieve anything. Most of the 'print' statements are for debugging. Thats how I know the Server works fine.

What is wrong there? I tried to find it, I couldn't.

I am kinda new to python and new to socket coding. So keep it easy please. Thanks.

(I run it with Batch scripts under Win10 cmd) (Since it is Process-to-Process it is probably not called a "Server"?)


Solution

  • In you code each connect should have a matching accept on server side.
    Your client connects once per session, but the server accepts after each message, so at the point where the second recv is invoked the server is already trying to accept another client. Apparently your server is supposed to handle only one client, so you can just move the call to accept out of the loop:

    s.listen(5)
    (client, address) = s.accept()
    print(address, 'just connected!')
    
    while True:
        message = raw_input("Would you like to close the connection? (y/n)")