1

So i'm writing a TCP based code in python that send 2 variables (username and password) from client and server receives those 2 variables and based on those variables, it does something, but my problem is that my client doesn't receive the second variable!

Client code:

# Variables
ip = "127.0.0.1"
port = 6767
username = "User"
password = "1234"

# Connect To Server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((ip, port))

# Send Information to Server
information_message = client.recv(1024).decode('ascii')
while True:
    if information_message == 'PASSWORD':
        client.send(password.encode('ascii'))
        print(information_message)
    if information_message == 'USERNAME':
        client.send(username.encode('ascii'))
        print(information_message)
    break 

Server code:

import socket

# Connection Data
host = '127.0.0.1'
port = 6767

# Starting Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()

# receive information to client
while True:
    client, address = server.accept()
    client.send('PASSWORD'.encode('ascii'))
    password = client.recv(10240).decode('ascii')
    client.send('USERNAME'.encode())
    username = client.recv(10240).decode('ascii')
    print(username, password)
    break

expect on server.py:

user 1234

real output on server.py:

Traceback (most recent call last):
  File "C:\Users\Administrator\PycharmProjects\test\server.py", line 29, in <module>
    username = client.recv(10240).decode('ascii')
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

i know that only ('PASSWORD') will send as a data to client but i can't understand why it won't send the second data ('USERNAME')

Matt Krause
  • 1,113
  • 14
  • 31
Morteza
  • 25
  • 1
  • 3

1 Answers1

1
information_message = client.recv(1024).decode('ascii')
while True:
    if information_message == 'PASSWORD':
        client.send(password.encode('ascii'))
        print(information_message)
    if information_message == 'USERNAME':
        client.send(username.encode('ascii'))
        print(information_message)
    break 

The last break statement causes your client to exit the loop immediately after the first information_message was processed, i.e. after PASSWORD. At the moment the server writes USERNAME to the socket, this socket is thus already closed by the client. While the send might still succeed (it might also fail with Broken Pipe already) the following recv will fail since the socket is closed.

What you likely want to do is something like this:

while True:
    try:
        information_message = client.recv(1024).decode('ascii')
    except:
        break

    if information_message == 'PASSWORD':
        client.send(password.encode('ascii'))
        print(information_message)
    if information_message == 'USERNAME':
        client.send(username.encode('ascii'))
        print(information_message)
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172