fineias Posted May 5, 2008 Share Posted May 5, 2008 I am fairly new to PHP and am trying to create a socket I can connect to on a free web host (000webhost.com). They have socket support and so I thought I would be able to use them. However I believe I am doing something wrong when I try to create a socket, and am not sure if I'm able to even do this. Anyone know if it's possible? If so, where do I put the PHP file to create the socket? Sorry if this is a dumb question, but if anyone could help, that would be greatly appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/104231-sockets-on-free-web-host/ Share on other sites More sharing options...
Daniel0 Posted May 5, 2008 Share Posted May 5, 2008 Try to post the code you have and we'll take it from there. Quote Link to comment https://forums.phpfreaks.com/topic/104231-sockets-on-free-web-host/#findComment-533602 Share on other sites More sharing options...
rarebit Posted May 5, 2008 Share Posted May 5, 2008 Are you wanting to create a server, if so I think you'll have trouble since a page is executed not persistent. A web page in it's self is basically a socket, arguments are sent as GET statements... Quote Link to comment https://forums.phpfreaks.com/topic/104231-sockets-on-free-web-host/#findComment-533603 Share on other sites More sharing options...
fineias Posted May 6, 2008 Author Share Posted May 6, 2008 ? // set some variables $host = "66.197.177.69"; $port = 10000; // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // bind socket to port $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // start listening for connections $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // read client input $input = socket_read($spawn, 1024) or die("Could not read input\n"); // clean up input string $input = trim($input); // reverse client input and send back $output = strrev($input) . "\n"; socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); // close sockets socket_close($spawn); socket_close($socket); ?> <html> <head> </head> <body> <? // form not yet submitted if (!$submit) { ?> <form action="<? echo $PHP_SELF; ?>" method="post"> Enter some text:<br> <input type="Text" name="message" size="15"><input type="submit" name="submit" value="Send"> </form> <? } else { // form submitted // where is the socket server? $host="66.197.177.69"; $port = 10000; // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); if (!$fp) { $result = "Error: could not open socket connection"; } else { // get the welcome message fgets ($fp, 1024); // write the user string to the socket fputs ($fp, $message); // get the result $result .= fgets ($fp, 1024); // close the connection fputs ($fp, "END"); fclose ($fp); // trim the result and remove the starting ? $result = trim($result); $result = substr($result, 2); // now print it to the browser } ?> Server said: <b><? echo $result; ?></b> <? } ?> </body> </html> This code I place in my default.php. I basically just gathered up some code from some tutorials, so I don't exactly know what I'm doing. When I run it, it comes with a warning that it could not bind to the socket because the address is already being used. My guess is that since the HTTP socket is being used while it's trying to bind another socket on the same server, it won't bind. Also, this isn't my own server, but a free webhost, which may be part of my problem. They do support sockets though. But if this host supports sockets, then there should be a way to open up a socket and use it right? It's not really a huge risk to the webhost since you can't change the set_limit() which is set to 10. Quote Link to comment https://forums.phpfreaks.com/topic/104231-sockets-on-free-web-host/#findComment-534345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.