Kerroy Posted January 25, 2010 Share Posted January 25, 2010 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. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 Are you connecting to 'well known ports' or do you have your software listening on the other end for your connections. Quote Link to comment Share on other sites More sharing options...
Kerroy Posted January 25, 2010 Author Share Posted January 25, 2010 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? Quote Link to comment Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 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. Quote Link to comment Share on other sites More sharing options...
Kerroy Posted January 25, 2010 Author Share Posted January 25, 2010 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? Quote Link to comment Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 Hi ok, the first thing to check is if there is anything listening at the port you choose. You say you can choose any port, but if nothing is listening on that port, then you will not get a connection Quote Link to comment Share on other sites More sharing options...
Kerroy Posted January 25, 2010 Author Share Posted January 25, 2010 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. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 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 Quote Link to comment Share on other sites More sharing options...
Kerroy Posted January 25, 2010 Author Share Posted January 25, 2010 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? Quote Link to comment Share on other sites More sharing options...
Kerroy Posted January 26, 2010 Author Share Posted January 26, 2010 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 Quote Link to comment Share on other sites More sharing options...
Kerroy Posted January 26, 2010 Author Share Posted January 26, 2010 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????? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.