Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a domain (ad-box.deslab.vn). I want to PHP socket to communicate with clients. However, After I run program, it has error:

****Warning: socket_bind(): unable to bind address [22]: Invalid argument in /home/vietchip/public_html/deslab.vn/ad-box/index.php on line 27 socket_bind() failed: reason: Invalid argument socket_bind() failed: reason: Invalid argument****

Link: ad-box.deslab.vn/index.php

Code:

<?php

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();

$address = "181.224.157.142";
$port = 10000;

// Create Socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($sock));
exit;
}

// Bind socket to port
socket_bind($sock, $address, $port);
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

// Start listening for connection
socket_listen($sock, 5); // Maximum is 5 connection
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

// Handling connection from client
do {
$msgsock = socket_accept($sock); // msgsock is a client connect to webserver
if ($msgsock === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}

$request ="PHP said : you are sent " . socket_read($msgsock, 2048, PHP_BINARY_READ);
socket_write($msgsock, $request, strlen($request));
socket_close($msgsock); // Close connect of client
} while (true);
socket_close($sock); // Close socket of server
?>

Please help me fix this error. Thanks so much.

What I have tried:

I have tried a lot of method about 1 month, but it doesn't work
Posted
Updated 20-Mar-17 21:58pm

1 solution

You are calling socket_bind twice:
PHP
// Bind socket to port
socket_bind($sock, $address, $port);
if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

The first call succeeds and the second fails because you can't bind to an IP address and port that is already in use. Remove the first call. Same for socket_listen below.
 
Share this answer
 
Comments
Member 13072690 21-Mar-17 6:06am    
Thanks for your response. This error is true, however after i corrected, this is error:
Gateway Timeout
The gateway did not receive a timely response from the upstream server or application.

Code Java of client:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class WebClient {
public static void main(String[] args) throws IOException {

String host ="ad-box.deslab.vn";


Socket s = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
s = new Socket(host, 10000);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname\n");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: hostname\n");
}
// Send message to server
String message = "FETEL HCMUS";
// String message = "GET / HTTP/1.1\r\n\r\n";
// System.out.println(message);
bw.write(message);
bw.flush();
// Get response from server
String response = "";
while ((response = br.readLine()) != null) {
System.out.println(response);
}
bw.close();
br.close();
s.close();
}
}

Please help me more
Jochen Arndt 21-Mar-17 6:22am    
Where does that error occur? On the client?

Then it seems to be a problem with the client code and you should raise a new question because it is not realy related to this question and there will be more people seeing the new question and might help.
Member 13072690 21-Mar-17 22:05pm    
Ok, I will make a new question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900