[SOLVED] need linux script to listen to port 12111 read request verify it and reply
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_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, $request, 256, 0);
/* 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);
/* 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 . "|" . $sym, FALSE) . "\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)));
/* 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($socket, SOL_SOCKET, SO_LINGER, array(0, 0)); @socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 0); @socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); @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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.