LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2012, 11:20 AM   #1
maaking
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Question need linux script to listen to port 12111 read request verify it and reply


Hello there,

I need to create linux bash script to listen to port eg. 12111 read user request verify it then reply one line of data.


the user will send this: userid|mobile_imei|symbid

then should take userid and search in database


PHP Code:
$sql mysql_query("select * from users where u_id=userid");
if(
mysql_numrows($sql) == 1){
//if the user is in database then
//grab data from table
$row mysql_fetach_array($sql);

reply with the following

{1}{$row[mobile_activation_number]}


I did this on my machine:

Code:

Code:
nc -l 12111
the port was open okay . and i received the request. BUT i cannot do anything more.

any help will be appreciated .


regards
 
Old 01-28-2012, 02:11 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
You seem to know PHP, so why not use the CLI (command-line-interface) PHP for this? Here is a skeleton to get you fully started:
PHP Code:
#!/usr/bin/php5
<?PHP

if (count($argv) != 3) {
    
fprintf(STDERR"Usage: %s address port\n"$argv[0]);
    exit(
1);
}

$socket = @socket_create(AF_INETSOCK_STREAMSOL_TCP);
if (
$socket === FALSE) {
    
fprintf(STDERR"Cannot create an IPv4 TCP socket.\n");
    exit(
1);
}

if (@
socket_bind($socket$argv[1], intval($argv[2])) === FALSE) {
    
fprintf(STDERR"Cannot bind to %s port %d: %s.\n",
                    
$argv[1], intval($argv[2]),
                    
socket_strerror(socket_last_error()));
    exit(
1);
}

if (@
socket_listen($socket) === FALSE) {
    
fprintf(STDERR"Cannot listen %s port %d: %s.\n",
                    
$argv[1], intval($argv[2]),
                    
socket_strerror(socket_last_error()));
    exit(
1);
}

fprintf(STDERR"Waiting for incoming connections.\n");
fflush(STDERR);

/* Single-threaded incoming service loop. */
while (($connection = @socket_accept($socket)) !== FALSE) {

    
/* Find out who the other end is. */
    
$client_address "";
    
$client_port 0;
    if (@
socket_getpeername($connection$client_address$client_port) === TRUE) {

        
/* Show the connection information. */
        
fprintf(STDERR"Connection from %s port %d:\n",
                        
$client_address$client_port);
        
fflush(STDERR);

        
/* Read the request, accepting up to 256 bytes only. */
        
$request "";
        
$length = @socket_recv($connection$request2560);

        
/* Filter out all unwanted characters. Simpler than escaping. */
        
$request preg_replace('/[^-._\/0-9@A-Z_a-z|]/'""$request);

        
/* Split the request into components. */
        
list($user$mobile$sym$ignore) = explode('|'$request "|||"4);

        
/* Check the request. */
        
if ((strlen($user)   > && strlen($user)   < 64) &&
            (
strlen($mobile) > && strlen($mobile) < 32) &&
            (
strlen($sym)    > && strlen($sym)    < 32)) {

            
/* Okay, looks like a valid request. */
            
fprintf(STDERR"\tRequest for user '%s', mobile '%s', sym '%s'.\n",
                            
$user$mobile$sym);
            
fflush(STDERR);

            
/* Process the request.
             * This example just returns the SHA1 hash of the request fields,
             * as text, with a newline appended.
            */
            
$response sha1($user "|" $mobile "|" $symFALSE) . "\n";

            
/* Send the response. Includes end-of-connection mark. */
            
$response_length strlen($response);
            
$response_sent   0;
            while (
$response_sent $response_length) {
                
$bytes socket_send($connection,
                                     
substr($response$response_sent),
                                     
$response_length $response_sent,
                                     
MSG_EOF);
                if (
$bytes 0)
                    
$response_sent += $bytes;
                else
                    break;
            }
            if (
$response_sent == $response_length)
                
fprintf(STDERR"\tResponse sent successfully.\n");
            else
                
fprintf(STDERR"\tError sending response: %s.\n",
                                
socket_strerror(socket_last_error($connection)));

        } else
            
fprintf(STDERR"\tInvalid request '%s'.\n"$request);

    } else
        
fprintf(STDERR"Incoming connection was aborted.\n");

    
/* Tell the client we are fully done, and close the connection. */
    
@socket_shutdown($connection2);
    @
socket_close($connection);

    
fprintf(STDERR"\tConnection closed.\n");
    
fflush(STDERR);
}

/* Report the cause of the malfunction. */
fprintf(STDERR"Cannot accept connection: %s.\n",
                
socket_strerror(socket_last_error()));

/* Close socket, making sure no connections are left to linger. */
@socket_set_option($socketSOL_SOCKETSO_LINGER, array(00));
@
socket_set_option($socketSOL_SOCKETSO_KEEPALIVE0);
@
socket_set_option($socketSOL_SOCKETSO_REUSEADDR1);
@
socket_close($socket);

exit(
0);
?>
Save it as ./myserver, allow it to be executed (chmod u=rwx,g=rx ./myserver), and run it specifying both the address and port, e.g.
Code:
./myserver 127.0.0.1 50000
Then you can see what it returns by using e.g.
Code:
echo "foo|bar|baz" | nc 127.0.0.1 50000
There is nothing PHP-y in the connection. It is exactly the same as you'd get by using Python or Ruby or Perl or C++ or C on the server side. The resource use is not that different, either, but if you find you want something more powerful, then you can switch languages without any effect on the clients.

Last edited by Nominal Animal; 01-28-2012 at 02:15 PM.
 
1 members found this post helpful.
Old 02-07-2012, 11:09 AM   #3
maaking
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Original Poster
Rep: Reputation: 0
hello Nominal Animal


your code has worked fine with me. I am very happy for it.

I am unable to thank you for this. you have saved my live.


much much much appreciated.

regards
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to listen to HTTP Request using C++/C ? samuellawrence Programming 4 09-16-2009 09:07 PM
550 Sender verify failed (in reply to RCPT TO command) Brandon.Wamboldt Linux - Server 3 05-31-2009 10:35 AM
webserver doesn't reply to external request but it reply's to local request ziba Linux - Server 4 05-11-2009 05:27 PM
request reply (sendmail) sqn Linux - Networking 4 12-05-2003 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration