How to create socket connection with php?

Why can't socket send or receive data in the second iteration of a while loop in this code?

  • my server program : import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 10086)) sock.listen(5) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) while True: clientsocket, addr = sock.accept() data = clientsocket.recv(1024) print data clientsocket.send("OK\n") sock.close() my client program : import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', 10086)) times = 0 while True: print times times += 1 sock.send("status\n") data = sock.recv(1024) print data If I put the sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', 10086)) in the while loop, everything is OK, otherwise, socket error. And I don't want to create a connection each time, and just want to create a connection and then keep send and receive message.

  • Answer:

    The problem you are encountering is that the while loop in your server code creates a new socket object on every iteration. 1clientsocket, addr = sock.accept() This line will create a new clientsocket object. which will kill the existing connection, thus disconnecting the connected client. You need to either have the client disconnect and reconnect on each iteration, or have the server stay open. In the client you can move this: 12sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', 10086)) into the while loop in the client, which will reconnect each iteration, though this is inefficient, unless there is a specific need to do this. Or you can have the server retain the connection by moving: 1clientsocket, addr = sock.accept() out of the while loop in the server. This will make the program only good for one client though, you'll need to add more logic to handle multiple connections. offers this bit of code: 12345clientsocket, addr = sock.accept() while True:     data = clientsocket.recv(1024)     if not data: break # this handles the end of the stream      print data...

John Clover at Quora Visit the source

Was this solution helpful to you?

Other answers

The problem is in your server side code. You put accept() call in the while loop, so the second iteration will BLOCK FOREVER. Because your client only connect once. That also explains why everything works fine when you put connect() call in the while loop. In that way, you create a connection each time, and the server will respond accordingly.

Chen Ge

Because the socket doesn't exist in the second iteration. What you need to do is # start code Clientsock, addr=... While true:    #do your stuff Good luck

Ryan Ehrlich

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.