estoque Posted March 14, 2013 Share Posted March 14, 2013 (edited) Need help on this guys. I'm working on a new project and I'm fairly new on socket connections. Background: I'm creating a PHP script that can receive and store new SMS messages from GSM modem via its API(TCP, request/response way). So far, the manufacturer told me that I should follow this flow on using the device's API: 1. GSM modem sends User Authentication message to Socket Server once a connection is made. 2. Socket Server sends a response. 3. Socket Server waits for incoming connections from GSM. This is my code as of the moment: <?php set_time_limit (0); // Set the ip and port we will listen on $address = '127.0.0.1'; $port = 8855; $sock = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($sock, $address, $port) or die('Could not bind to address'); socket_listen($sock); $client = socket_accept($sock); //Breakdown of received message from GSM modem, I needed the $ID as identifier on my response $length = bin2hex(socket_read($client, 4)); $ID = bin2hex(socket_read($client, 16)); $type = bin2hex(socket_read($client, 4)); $username = bin2hex(socket_read($client, 16)); $password = bin2hex(socket_read($client, 16)); //This is what I should send on the socket as a response $rlength=pack("H*", "00000001"); $rID=pack("H*", $ID); $rtype=pack("H*","00100000"); $rresult=pack("H*", "00"); $response=$rlength.$rID.$rtype.$rresult; //Write the response on the socket socket_write($client, $response, $strlen($response)); //After sending the data, I should wait for the GSM modem's incoming connection when a new SMS message is received. //Any insights what should I code below? socket_close($client); socket_close($sock); ?> Edited March 14, 2013 by ignace Added code tags Quote Link to comment https://forums.phpfreaks.com/topic/275669-php-socket-listener/ Share on other sites More sharing options...
ignace Posted March 14, 2013 Share Posted March 14, 2013 (edited) Please read the forum guidelines. Always surround your code with code tags. http://www.phpfreaks.com/page/rules-and-terms-of-service#toc_forum_guidelines Edited March 14, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/275669-php-socket-listener/#findComment-1418691 Share on other sites More sharing options...
kicken Posted March 14, 2013 Share Posted March 14, 2013 We'd have to know more details about the api to be of any use really. What you've provide is not much to go on. From the sounds of it you probably need to wrap your socket_accept in a loop to keep accepting new connections. while ($client=socket_accept($sock)){ //... process data ... socket_close($client); } Quote Link to comment https://forums.phpfreaks.com/topic/275669-php-socket-listener/#findComment-1418695 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.