Need help with sockets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mfbrowne
Forum Newbie
Posts: 16
Joined: Tue May 02, 2006 11:45 am

Need help with sockets

Post by mfbrowne »

Hello, I am having a problem with sockets and would like some advice on what I may be doing wrong.

I have the following small test function, which I found in one of the examples and I have modified it to perform my testing. The problem I am facing is that it appears the socket is being opened and connected and the data is being sent but the script hangs on the socket_read() and never returns.

The test code is;

Code: Select all

function SocketConnectTest()
{
         error_reporting(E_ALL);

         echo "TCP/IP Connection<br>";

         /* Define Port to use */
         $service_port           = "5000" ;

         /* Define IP to use */
         $address = 'IP HIDDEN FOR SECURITY';  

         echo "Host ip '$address'<br>";

         /* Create a TCP/IP socket. */
         $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
         if ($socket < 0) {
             echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
          } else {
            echo "OK Socket Created.<br>";
          }

          echo "Attempting to connect to '$address' on port '$service_port'...<br>";

          $result = socket_connect($socket, $address, $service_port);
          if ($result < 0) {
              echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "<br>";
          } else {
            echo "OK Socket is connected.<br>";
          }

          socket_getpeername($socket, &$connectip);
          echo "Peer Name $connectip<br>";


          $in = "MESSAGE I SEND" ;
          $out = '';

          echo "Sending message to server<br>";
          $Status = socket_write($socket, $in, strlen($in));
          if ( $Status === FALSE ) {
                echo "socket_write() failed.\nReason: ($Status) " . socket_strerror($Status) . "\n";
          } else {
                echo "Ok written to socket '$Status'<br>";
          }

          echo "Reading response:<br>";

          while ($out = socket_read($socket,100)) {
                    echo $out;
          }
          echo "Closing socket...<br>";
          socket_close($socket);
          echo "OK socket closed<br>";

}

So when I run this the results I get are :

TCP/IP Connection
Host ip 'IP HIDDEN FOR SECURITY'
OK Socket Created.
Attempting to connect to 'IP HIDDEN FOR SECURITY' on port '5000'...
OK Socket is connected.
Peer Name 'IP HIDDEN FOR SECURITY
Sending message to server
Ok written to socket '37'
Reading response:



But the script never returns from the read. Now I know the server is there and I can connect and send and receive messages using a C program. So I don't understand why the read is never returning.

Could someone help in respect to this. I sure would appreciate it.

Thanks very much.
mfbrowne
Forum Newbie
Posts: 16
Joined: Tue May 02, 2006 11:45 am

Post by mfbrowne »

Just wondering if anyone can assist me in this problem.

Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Which protocol is the server talking?

Change your reading loop to while ($in = socket_read(..., ...) && $in != "") // since socket_read returns "" if there is nothing to read anymore...
mfbrowne
Forum Newbie
Posts: 16
Joined: Tue May 02, 2006 11:45 am

Post by mfbrowne »

Ok, I will try this suggestion.

The server is using TCP/IP protocol.

Am I right in that if I issue the socket read(), if there is nothing to be read then it would return with "" in the return variable. Is there any way to determine how many bytes are in the socket buffer before issuing a read on the socket.

Thanks for the help, I appreciate it.
mfbrowne
Forum Newbie
Posts: 16
Joined: Tue May 02, 2006 11:45 am

Post by mfbrowne »

Well it seems I can't get any response from the server I am trying to make a socket connection with. I have tried the above suggestion and it still does not work.

Can anyone help me in this.

Thanks

Mike
Post Reply