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);
?>