I want to Run a simple Client-Server socket PHP script on my web host.
But i don't know how to exactly specify IP address and port number to work.
I test then on my wammp with success, but wen i upload them on my host control panel and run, they are not works.
Here is my codes:
Client
<?php
$host = "105.24.123.188";
$port = 8080;
$message = "hello";
echo "Message To server: ".$message. "<br>";
// 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");
// send string to server
echo "After connect: ".$result. "<br>";
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");
echo "Reply From Server: ".$result. "<br>";
// close socket
socket_close($socket);
?>
Server
<?php
$host = "105.24.123.188";
$port = 8080;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
//echo "reached \n";
printf("this1\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");
///echo "reached \n";
printf("this2\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);
echo "Client Message: ".$input. "<br>";
// reverse client input and send back
$output = strtoupper($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
[Error]
Client
Message To server: hello
After connect: 1
Reply From Server: HTTP/1.1 400 Bad Request
Server
can't bind
I cant find out how to run my simple example on web server.
IP address and port checked they are valid and open.
I test this on local host under wammp server in windows successfully but how i can put them in online real web?
[?]
Is anyone face this issue when Run client, server scripts in web host?
Can you guys show me some working example similar.
How to obtain valid IP, Port to test them?
I get these ip from run ping www.myhost.com on windows, is it true?
Related
I got this two files chat.php and chat2.php, it works fine but i want to run it as a background service in linux server or something like that
chat.php
`<?php
$host = $_SERVER['SERVER_ADDR'];// set some variables
$port = 25003;
// don't timeout!
set_time_limit(0);
// 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);
echo "Client Message : ".$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);
?> `
*
i run this code and i could transfer from my input form as long as the page is reloading the chat.php file get the POST string so i want to run it as a server service or something like that. Thanks.*
`<?php
// set some variables
$host = $_SERVER['SERVER_ADDR'];
$port = 25003;
$message = $_POST['message'];
echo "Message To server :".$message;
// 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");
// 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");
echo '';
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
?>`
I am initiator of socket programming. I am trying to implement the socket connection.
But I don't know which IP and port should I use for server.
The code I took from a link :
server.php:
<?php
// set some variables
$host = "<?>";
$port = 5555;
// don't timeout!
set_time_limit(0);
// create socket
//echo 'hello';
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
// 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");
echo 'hello';
// 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);
echo "Client Message : ".$input;
// reverse client input and send back
//$output = $_REQUEST['server_input'];
$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);
?>
- client.php
<?php
//include_once('server.php');
$host = "<?>";
$port = 5555;
$message = "hi from client";
//echo "Message To server :".$message;
// 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");
// 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");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
?>
Description :
This function accepts incoming connection request on the created socket. After accepting the connection from client socket, this function returns another socket resource that is actually responsible for communication with the corresponding client socket. Here “$spawn” is that socket resource which is responsible for communication with client socket.
But I don't know which IP and port should I use for server.
The IP for use is same static IP set on your server's network, If you try to local test, Can use 127.0.0.1 for IP.
And also, for port, you can use one desired number between 1024 to 65535.
Note that, the IP and Port used on the server and client must be same.
I am working on PHP socket programming project. In this project we are going to create a service in php socket. This socket will listen on one particular port. And client from outside network will able to communicate on that port.
Till now I am able to create server and client in php for socket programming. Now my pc is connected to LAN so I have to use port forward for connecting my pc with outside client. I forward port 2000 and all communication on that port is transfer to my pc IP address. I have netgear router n150 wireless adsl .I add all configuration on that router. I test port forwarding online at this site http://www.yougetsignal.com/tools/open-ports/ it says port is open.
I test my code on locally (intranet), it is working fine. But when I trying to run server on my pc and client from web server which is my ipage hosting server. It throws me error "Server Could not connect to server".
Server.php
<?php
// set some variables
// My LAN Ip
$host = "192.168.0.5";
$port = 2000;
// don't timeout!
set_time_limit(0);
// 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");
$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);
echo "Client Message : " . $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);
?>
Client.php
<?php
//my public ip
$host = "117.223.90.191";
// port on which I port forword
$port = 2000;
$message = "Hello Server";
echo "Message To server :" . $message;
// 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");
// 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");
echo "Reply From Server :" . $result;
// close socket
socket_close($socket);
?>
Any suggestion for problem. I think many will have same problem like me.
Even if i think the problem is in the lan forwarding, try testing it with :
telnet 117.223.90.191 2000
another thing to try is to make the server listen on all interfaces
$host = "0.0.0.0";
and take a look at http://reactphp.org/
I have a client and server program i do not have an idea of how to start the server automatically on my CENTOS server and the server should keep on running irrespective of my browser is closed, and it keeps on listening to client request. Please Help
Server.php
<?php
// set some variables
$host = "XX.XX.XXX.XX";
$port = 25763;
// don't timeout!
set_time_limit(0);
// 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);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
echo $spawn;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Client.php
<?php
// where is the socket server?
$host = "XX.XX.XXX.XX";
$port = 25763;
$message = "Hello Server This is the first message to the server";
echo "Message To server :".$message;
// 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");
// 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");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
// print result to browser
?>
Generally, you should start .php file with exec(), for example:
exec('/usr/bin/php /var/www/directory/server/event_server.php');
Next thing, put your socket_accept() (althoug, I used different scheme, with socket_select()) in infinite loop, and dont forget about delay - this is very important, as you don't want to hang your CPU:
$broadcast_start = microtime(true);
while(TRUE) {
socket_accept($socket);
//Do stuff...
if( round(microtime(true) - $broadcast_start, 1) < 0.5 ) {
usleep(500000);
}
}
But actually, your subbjects is really complex. You should first search the web for solutions, which are plenty.
I have created a client and server programs using socket it is working perfectly on the localhost but when i upload it to the server through cpanel it does not work properly and it gives following error. The main thing is some times both of the program works on the server.
I am new to socket programming and seen some of the related posts on stackoverflow but was unable to found exact solution.
Client: Warning: socket_connect(): unable to connect [111]: Connection refused in .../client.php on line 10
Could not connect to server
Server:Warning: socket_bind(): unable to bind address [98]: Address already in use in ..server.php on line 10
Could not bind to socket
Client.php
<?php
// where is the socket server?
$host = "XX.XX.XXX.XX";
$port = 25763;
$message = "Hello Server This is the first message to the server";
echo "Message To server :".$message;
// 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");
// 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");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
// print result to browser
?>
Server.php
<?php
// set some variables
$host = "XX.XX.XXX.XX";
$port = 25763;
// don't timeout!
set_time_limit(0);
// 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);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
echo $spawn;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
For the server you might like to set option SO_REUSEADDR on the freshly created socket using socket_set_opt().
For code how to do this please see the example section on manual page linked above.