Jump to content

Socket testing


KingOfHeart

Recommended Posts

There are several in the comments at http://www.php.net/manual/en/function.socket-connect.php

 

I'm assuming you want to create a socket to connect to somewhere.  There are also sockets for receiving connections.  And remember that UDP sockets will ALWAYS connect, even when they are unable to send anything, because UDP is connectionless.  Only TCP sockets will fail to connect.

Link to comment
https://forums.phpfreaks.com/topic/213447-socket-testing/#findComment-1111247
Share on other sites

Did you give the port number to that script?  The code has a bug and calls socket_last_error() even if it hasn't tried to connect.  It's also specialized to connect back to whatever ip address connected to it, which might not be suitable.l

 

If you're comfortable with calling functions, I would try this script: http://www.php.net/manual/en/function.socket-connect.php#84465

 

Otherwise try this one: http://www.php.net/manual/en/function.socket-connect.php#36223

 

You can try connecting to www.google.com port 80, that will never fail :)  Do keep in mind though that you won't get any output unless you send an HTTP request first.

Link to comment
https://forums.phpfreaks.com/topic/213447-socket-testing/#findComment-1111873
Share on other sites

  • 3 weeks later...

I tried the one with the function but got an error.

 

<?php
function msConnectSocket($remote, $port, $timeout = 30) {
        # this works whether $remote is a hostname or IP
        $ip = "";
        if( !preg_match('/^\d+\.\d+\.\d+\.\d+$/', $remote) ) {
            $ip = gethostbyname($remote);
            if ($ip == $remote) {
                $this->errstr = "Error Connecting Socket: Unknown host";
                return NULL;
            }
        } else $ip = $remote;

        if (!($this->_SOCK = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
            $this->errstr = "Error Creating Socket: ".socket_strerror(socket_last_error());
            return NULL;
        }

        socket_set_nonblock($this->_SOCK);

        $error = NULL;
        $attempts = 0;
        $timeout *= 1000;  // adjust because we sleeping in 1 millisecond increments
        $connected;
        while (!($connected = @socket_connect($this->_SOCK, $remote, $port+0)) && $attempts++ < $timeout) {
            $error = socket_last_error();
            if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
                $this->errstr = "Error Connecting Socket: ".socket_strerror($error);
                socket_close($this->_SOCK);
                return NULL;
            }
            usleep(1000);
        }

        if (!$connected) {
            $this->errstr = "Error Connecting Socket: Connect Timed Out After $timeout seconds. ".socket_strerror(socket_last_error());
            socket_close($this->_SOCK);
            return NULL;
        }
       
        socket_set_block($this->_SOCK);

        return 1;     
}
msConnectSocket("www.google.com",80,30);
?>

Fatal error: Using $this when not in object context in /home/thegamin/public_html/openzelda/test/socket.php on line 13

 

 

Link to comment
https://forums.phpfreaks.com/topic/213447-socket-testing/#findComment-1118357
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.