问题
I have established a client-server communication.
The problem is that I can't send more than one message and I tried to fix it and I don't know what's wrong.
this is my code:
**The server code and client code are run in two different python windows.
server:
import socket
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s = socket.socket()
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
conn, addr = s.accept()
print('Got connection from ', addr[0], '(', addr[1], ')')
while True:
data = conn.recv(1024)
print(data.decode("utf-8"))
if not data:
break
conn.sendall(data)
conn.close()
print('Thank you for connecting')
client:
import socket # Import socket module
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
conn = socket.socket() # Create a socket object
conn.connect((host, port))
conn.sendall(b'Connected. Wait for data...')
intosend = input("message to send:")
conn.sendall(bytes(intosend, 'utf-8'))
data = conn.recv(1024)
intosend= "no"
while intosend != "quit":
intosend = input("message to send:")
conn.sendall(bytes(intosend, 'utf-8'))
conn.close() # Close the socket when done
print(data.decode("utf-8"))
can someone help?
回答1:
Server side with a little protection added. Perhaps this is what you need? The server will continue to listen for connections after the client process finishes. Try it by starting the server, starting the client, sending a message, starting the client again and sending another message...
import socket
import threading
import sys
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s = socket.socket()
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
def processMessages(conn, addr):
while True:
try:
data = conn.recv(1024)
if not data:
conn.close()
print(data.decode("utf-8"))
conn.sendall(bytes('Thank you for connecting', 'utf-8'))
except:
conn.close()
print("Connection closed by", addr)
# Quit the thread.
sys.exit()
while True:
# Wait for connections
conn, addr = s.accept()
print('Got connection from ', addr[0], '(', addr[1], ')')
# Listen for messages on this connection
listener = threading.Thread(target=processMessages, args=(conn, addr))
listener.start()
回答2:
You need to loop the code on the client side that accepts the input and sends it.
This code worked for me on the client side.
import socket # Import socket module
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
conn = socket.socket() # Create a socket object
conn.connect((host, port))
conn.sendall(b'Connected. Wait for data...')
while 1:
intosend = input("message to send:")
conn.sendall(intosend.encode('utf-8'))
#data received back from sever
data = conn.recv(1024)
print("Data: ", data.decode('utf-8'))
conn.close() # Close the socket when done
print(data.decode("utf-8"))
来源:https://stackoverflow.com/questions/44662178/client-server-communication-in-python