Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/104231-sockets-on-free-web-host/
Share on other sites

?
// 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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.