- I am writing a server client code for communication with socket programming. i have written code for server and client. when i run server(server.php) from command line by "php -q server.php" and then client connects to server using telnet, it works.
- Thereafter i made a html page for client and tries to connect to same server,but it is not able to connect with server. server which is running from terminal is also not showing any connection request recieved from client. my code for client is ...
<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="192.168.1.2";
$port = 1234;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_read ($socket, 1024) or die("Could not read server response\n");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
// end session
socket_write($socket, "END", 3) or die("Could not end session\n");
// close socket
socket_close($socket);
// clean up result
$result = trim($result);
$result = substr($result, 0, strlen($result)-1);
// print result to browser
?>
Server said: <b><? echo $result; ?></b>
<?
}
?>
- please troubleshoot this code.
</body>
</html>