KingOfHeart Posted September 15, 2010 Share Posted September 15, 2010 Can someone give me a simple socket script that I can copy and paste. It can return anything you like. I just want to make sure I have a working socket connection, and if not then you can maybe help me fix it. Link to comment https://forums.phpfreaks.com/topic/213447-socket-testing/ Share on other sites More sharing options...
btherl Posted September 15, 2010 Share Posted September 15, 2010 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 More sharing options...
KingOfHeart Posted September 16, 2010 Author Share Posted September 16, 2010 I tried copying and pasting that first script. I get a message saying unable to connect, then the next line said Sucess. I thought it was supposed to return some numbers or something. Link to comment https://forums.phpfreaks.com/topic/213447-socket-testing/#findComment-1111516 Share on other sites More sharing options...
btherl Posted September 16, 2010 Share Posted September 16, 2010 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 More sharing options...
KingOfHeart Posted October 2, 2010 Author Share Posted October 2, 2010 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 More sharing options...
btherl Posted October 3, 2010 Share Posted October 3, 2010 Oops, that one is a method, not a function. If you replace "$this->_SOCK" with "$sock" and "$this->errstr" with "$errstr" then that should fix the errors. You would also need to make it return $sock instead of 1. Link to comment https://forums.phpfreaks.com/topic/213447-socket-testing/#findComment-1118504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.