I think there might be some problem with my using socket_set_nonblock(). Without socket_set_nonblock(), socket_select() works, but with socket_set_nonblock(), socket_select() fails. I wrote codes as below.
case 1. fail
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
echo socket_strerror(socket_last_error()); /* 'success' */
socket_set_nonblock($sock);
echo socket_strerror(socket_last_error()); /* 'success' */
socket_connect($sock, ADDRESS, PORT);
echo socket_strerror(socket_last_error()); /* 'unable to connect [115]: Operation now in progress' */
$read = array($sock); $write = array($sock);
socket_select($read, $write, $e=NULL, TIMEOUT);
echo socket_strerror(socket_last_error()); /* 'unable to connect [115]: Operation now in progress' */
case 2. succeed
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
echo socket_strerror(socket_last_error()); /* 'success' */
socket_connect($sock, ADDRESS, PORT);
echo socket_strerror(socket_last_error()); /* 'success' */
$read = array($sock); $write = array($sock);
socket_select($read, $write, $e=NULL, TIMEOUT);
echo socket_strerror(socket_last_error()); /* 'success' */
The comments after 'echo's are output of the 'echo's. I can't guess what the problem is. Furthermore, it happens to the only one server I am running. The others are working fine. The environments of the servers are exactly the same. (same os / apache / php version and modules).
Related
I have one PHP application which have socket connection. I am thinking for integrate proxy in it so I can make connection using proxy. My current code for connection is like below
public function connect()
{
if ($this->isConnected()) {
return true;
}
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket !== false) {
$result = socket_connect($socket, 'e'.rand(1, 16).'.myserver.net', Constants::PORT);
if ($result === false) {
$socket = false;
}
}
if ($socket !== false) {
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => Constants::TIMEOUT_SEC, 'usec' => Constants::TIMEOUT_USEC]);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => Constants::TIMEOUT_SEC, 'usec' => Constants::TIMEOUT_USEC]);
$this->socket = $socket;
$this->eventManager()->fire('onConnect',
[
$this->phoneNumber,
$this->socket,
]
);
$this->logFile('info', 'Connected to server');
return true;
} else {
$this->logFile('error', 'Failed to connect server');
$this->eventManager()->fire('onConnectError',
[
$this->phoneNumber,
$this->socket,
]
);
return false;
}
}
Let me know if someone have reliable idea for do it. Thanks
When the WebSocket handshake is done I receive a PHPSESSID different from the one in the browser, why is this?
Client code for connecting:
websocket = new WebSocket("ws://192.168.0.109:9000/php_servers/socketserver1/socketserver.php");
Server code for reading header:
<?php
include "../serverfunctions.php";
include "eventfunctions.php";
$host = '192.168.0.109';
$port = '9000';
$null = NULL;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, $port);
socket_listen($socket);
$clients = array($socket);
while (true)
{
$changed = $clients;
socket_select($changed, $null, $null, 0, 10);
if (in_array($socket, $changed))
{
$socket_new = socket_accept($socket);
$clients[] = $socket_new;
$header = socket_read($socket_new, 1024);
I am writing PHP socket server, displayed here is error causing portion. I find out socket_recv() is causing problem, it only lets one computer connect. However, if I comment out socket_recv then its working fine. But I have to receive data also in socket server. Help me find out solution. Please also point out any wrong with code. JQuery part is working fine, hence didn't print it here.
<?php
set_time_limit(0);
$host = '172.28.4.5';
$port = 10000;
$null = NULL;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $host, $port);
socket_listen($socket);
$clients = array($socket);
while(true) {
$new_socket = socket_accept($socket);//Accepting new connection/socket/client if any
$clients[] = $new_socket;//Adding the new client/socket/connection to client array
$header = socket_read($new_socket, 1024);
perform_handshaking($header, $new_socket, $host, $port);
/* If I want to notify if new connection is established**/
socket_getpeername($new_socket, $ip);
$message = "Welcome to WebSocket $ip";
$array = array(
'message' => $message
);
$message = mask(json_encode($array));
write_to_socket($message);
$found = array_search($socket, $clients);
unset($clients["$found"]);
//Going through each client
foreach($clients as $client) {
//Getting messages with loop how many packages for each client has
while(socket_recv($client, $buf, 1024, 0) >= 1) {
$array = json_decode(unmask($buf));
print_r($array);
}
}
}
}
?>
it seems that
the code is wrong
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $host, $port);
socket_listen($socket);
//$clients = array($socket); <---- here
while(true) {
it seems that you add the listened socket to array , you should not receive and data from the listened socket.
How can i run socket server for each www client?
I have simple socket_server:
<?php
// set ip and port
include('socket.php');
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "-1";
exit;
}
// bind socket to port
$result = socket_bind($socket, $host, $port) or die(socket_strerror(socket_last_error($socket)));
// start listening for connections
$result = socket_listen($socket, 3);
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Server: Ready.";
while(1) {
// 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 = strrev($input) ."<br />";
$output = "output message";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
}
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Now when i try run server in this way:
<?php
//include the server.php script to start the server
include_once('server.php');
include('socket.php');
$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);
?>
I get the message:
Only one usage of each socket address (protocol/network address/port) is normally permitted.
I do not have any other communications on this port. I checked the netstat.
I have a problem with android client receiving data from php server. Android can successfully writes data to php server and server accepts that data and then send back response to that client but android is not accepting. it does not move forward from Socket s = ss.accept()
Here is my android code to recieve data
public void run() {
Boolean end = false;
ServerSocket ss = serverSocket;
/*try {
ss = new ServerSocket(54546);
} catch (IOException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}*/
while(!end){
//Server is waiting for client here, if needed
try {
Log.i("before accept", "yes");
Socket s = ss.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
//PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
String st = input.readLine();
Log.d("Tcp Example", "From client: "+st);
//output.println("Good bye and thanks for all the fish :)");
}catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my php code
$host = "127.0.0.1";
$port = 54546;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
//$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
/*if (!socket_connect($socket, $host, $port)) {
die('failed'.socket_strerror(socket_last_error($socket)));
}*/
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
$result = socket_bind($socket, $host, $port) or die("Could not create socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "\nbefore socket accept while loop\n";
$aaa = fopen("tesst.txt", "w");
while(true)
{
echo "\nbefore socket accept\n";
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "\nThe server is ready\n";
$input = socket_read($spawn, 1024) or die("Could not read input\n");
echo "Input recieved from $spawn : ".$input;
fwrite($aaa, $input);
$output = $input."\n";
$sent = socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
echo "Output sent ".$sent;
socket_close($spawn);
}
fclose($aaa);
socket_close($socket);
echo "\nTerminating\n";
ss.accept() is not accepting connection from server.
We don't have to do ss.accept() for the client as well. The client should establish the connection using the connect() and the server should do accept(). Once the connection is established, the server should use the file descriptor returned from the accept() to send and receive data to/from the client. On the other side, the client does not need to do any accept, it should simply call recv() to retrieve received data.
Thus, if the Android code is the client, then it should do a connect() call to the PHP server -- the connect() call would take the IP address and the port number (54546). With a successful connect() call, the accept() on the PHP would return.