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')