[英]Connecting client to server, python
I am having trouble with my server and client.我的服务器和客户端有问题。 I need the client to send information to the server, the server sends the information back, and the process repeats once more.我需要客户端向服务器发送信息,服务器将信息发回,这个过程再次重复。 I was able to get the client to connect to the server and have infomation sent the first round but I cant seem to figure out how to get the server to listen to client again after I shutdown the sending side using the function conn.shutdown(1).我能够让客户端连接到服务器并在第一轮发送信息,但在我使用函数 conn.shutdown(1) 关闭发送端后,我似乎无法弄清楚如何让服务器再次侦听客户端)。 I tried connecting to the port again but I get an error that says "Transport endpoint is already connected".我尝试再次连接到该端口,但收到一条错误消息,指出“传输端点已连接”。 Any suggestions?有什么建议?
Try to use threads(Multithreading)-尝试使用线程(多线程)-
Server App服务器应用程序
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
def clientthread(conn):
conn.send('connected')
try:
while True:
data = conn.recv(1024)
processData(conn,data)
if not data:
break
except Exception as ex:
print ex
conn.close()
while True:
conn, addr = s.accept()
print 'Got connection from', addr
thread.start_new_thread(clientthread ,(conn,))
s.close()
Client App客户端应用程序
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.