Tuesday, 15 June 2010

python - Socket connection is sometimes deadlocked -


I write a server-client communication through sockets in python3 I am here. When I run the server for the first time and after that I run the client, everything goes well, but when I run it the next time (never) it does nothing, it seems that the server is waiting for the client and opposite of this.

Server side:

  Import socket server socket = socket socket (socket .fenet, socket.SOCKRRM) server socket socketcopp (socket.SOLOCKET, socket SUUUSAport, 1) Serverocket .bind (('localhost', 8098)) serverocket.listen (50) #server socket, become up to 5 connections, while true: connection, address = serverocket.accept () buf = connection.recv (1024) frc = bytes ( "I've got", "UTF-8") connection.send (frc) if len (buf)> 0: Print (buff) brake connection. Shut down (socket SHDRDWR) connection.close ()  

client side:

  import socket clientscount = socket socket (socket.AF_INET, socket.SOCK_STREAM) Clientsocket.setsockopt (socket .SOL_SOCKET, socket.SO_REUSEPORT, 1) clientsocket.connect (('localhost', 8098)) frc = bytes ("hello i am customer", "utf-8") clientsocket.send (frc) a = Clientsocket.recv (1024) Print (A) clientsocket.shutdown (socket.SHUT_RDWR) clientsocket.close ()  

Where should one mistake? Thank you.

Every time you read something that goes away from you (the break sentence). Is this the right behavior? After this you will not be able to do this if you try to connect to "server" (again). Another thing is this: If you want to add more than one client at the same time, then you have to do something like this:

  while correct: conn, addr = s.accept () print # Connect with '+ Addr [0] +': '+ str (ADR [1]) # Initial Thread: # 2 works to run the first argument argument as the # function name  < / Pre> 

I hope this can help.

PS: You have another way of creating a TCP server. I suggest you read this:


No comments:

Post a Comment