Jump to content

PHP Socket Listener


Recommended Posts

Need help on this guys.

I'm working on a new project and I'm fairly new on socket connections.


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:

set_time_limit (0);
// Set the ip and port we will listen on
$address = '';
$port = 8855;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind to address');
$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);
$rresult=pack("H*", "00");
//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?
Edited by ignace
Added code tags
Link to comment
Share on other sites

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 ...

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.

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.