Jump to content

Socket, IP, 'unable to bind address', and everything else...


Kerroy

Recommended Posts

Hello all masters and freaks,

 

New here, sorry if I repeat the problem but I didn`t found a solution for it.

I`m trying to send a string throught a socket (IP address) in one page/pc and receive the string throught a socket (IP address), I could find many examples but none of them works. I`m working on it for awhile now.

My code for sending is:

 

$fp = fsockopen($host, $port, $errno, $errstr, 30);

if (!$fp) {

echo "$errstr ($errno)<br />\n";

} else {

fwrite($fp, $out);}

and receiving:

$fp = fsockopen($host, $port, $errno, $errstr, 30);

if (!$fp) {

echo "$errstr ($errno)<br />\n";

} else {

fread($fp, $out);}

 

 

Please help, anyone.

Link to comment
Share on other sites

I tried several ports all the same. I`m working with xampp (I don`t know if it will help but it sure important info).

About the listening I tried many deferent approachs and nothing.

 

I`m totaly lost here, what do I do? Do you have  a working code?

Link to comment
Share on other sites

The thing is, that if you are to send data over a socket, you need to know what you are connecting to, and what handshaking the sofware at the other end is expecting.

 

eg, if you connect to port 80 (the http port), you need to know to send http messages, as that is what the software the other end is expecting. If you connect to port 25 (smtp) you need to send appropriate HELO strings etc

 

Whatever you are connecting to, will expect messgaes in a particular format, and reply accordingly.

Link to comment
Share on other sites

It`s a device that expecting this string (string of numbers only), it can be send/received in any port so that`s not the problem.

 

jl5501 thanks for the help but one small question how does it help me to over come this communication problem that I can`t even open the socket to send/receive?

Link to comment
Share on other sites

Hi,

 

There isn`t becuase it can`t bind the socket. It worked once but when I tried to send another string it just stop working, since than I get the same problem: 'unable to bind address...' , 'the connection was fail...'. The weird part is I didn`t change any part of the code before or after it worked and still It`s not working. I tried all the solution I found in the net but nothing.

Link to comment
Share on other sites

What I am still confused about, is what you are trying to connect to. To have a socket connection, you need software ate both ends. One program is listening on a port for a connection, and then you issue your fsockopen() from the other end of the connection and the listener responds

 

If there is no listener, then there will be no response.

 

You have posted your connection code, but what can you provide regarding the listener?

Is it another copy of the same code?

 

One thing that also comes to mind is socket letency, which will explain your failure to connect again after succeeding.

 

It would be interesting to open a command prompt window and type netstat, to see which ports are still held. You would be particularly interested in any ports in a TIME_WAIT status

Link to comment
Share on other sites

Unfornutly I can`t show the receiving code at the moment but I will sure show it here as soon as I can, including the results of netstat.

 

Are the ports still open or time_wait even after a reset? I tried to reset the pc and still got the same err.

How do I solve this problem of socket letency/time_wait?

Link to comment
Share on other sites

Hi jl5501,

 

as I said. This is the code for rec:

$fp = fsockopen("192.168.50.20", "12356", $errno, $errstr, 30);

if (!$fp) {

echo "$errstr ($errno)<br />\n";

} else {

$stng = fread($fp, 10);

}

echo "string: ".$stng; //add to see that it really received what I sent

 

Now I get this err:

Warning: fsockopen() [function.fsockopen]: unable to connect to 192.168.50.20:12356 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\xampp\htdocs\rec_tcp.php on line 5

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)

string:

 

I`m sure it`s something I miss or in the code or in the pc/server it self. Please help me

Link to comment
Share on other sites

New update: I succesfully created the socket and made it listen but I can`t reach it to write into it or read from it. The code I use to creat and write/read is:

$host = "192.168.50.20"; // set the variables

$port = 9878; //just for exmaple, I can use any port available

set_time_limit(0); // no timeout

 

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");// create socket

 

$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // bind socket to port

 

if ($result==true){

$result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); // start listening for connections

 

$output= '1234567890'; //just for exmaple, but the string will be 10 chars length

$spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // spawn another socket to handle

$result = socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");

if ($result == false){

echo "something went wrong. could not write to socket";

}

$input = socket_read($spawn, 1024) or die("Could not read input\n"); // read client input

echo "input: ".$input; //just to make sure it`s reading right

socket_close($spawn);

socket_close($socket);

 

The problems are:

1) it can creat the socket but only as listener.

2) I can`t get to read or write.

3) it doesn`t close the socket.

 

any help, Please?????

Link to comment
Share on other sites

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.