Click here to get back home

Server Socket programming with Perl

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Server Socket programming with Perl paul f 04-11-2008
Get Chitika Premium
Posted by paul f on April 11, 2008, 5:21 am
Please log in for more thread options
Hi there ,
I have a GPRS device that sends it's IMEI data after a sucessfull TCP
socket is established with a Unix Server.
It waits for a sucess or failure ( 1 or 0) from the Server. The GPRS
decvice then sends it's packet of data back to server (approx 100
bytes).

Can somebody help me find out how I can rearrange code below to read
IMEI data send back a 1 and read 100 byte data and save incomming data
to a logfile (say log.txt)


Source code below:


#! /usr/bin/perl -w
# server0.pl
#--------------------
use strict;
use Socket;

# use port 7879 as default
my $port = shift || 7879;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock:
$!";

# grab a port on this machine
my $paddr = sockaddr_in($port, INADDR_ANY);

# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port ";
. # accepting a connection
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER))
{
# find out who connected
my ($client_port, $client_ip) = sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host = gethostbyaddr($client_ip, AF_INET);
# print who has connected
print "got a connection from: $client_host","[$client_ipnum] ";
# send them a message, close connection
print CLIENT "Smile from the server";
close CLIENT;
}



Posted by Brian McCauley on April 12, 2008, 5:04 am
Please log in for more thread options
> Hi there ,
> I have a GPRS device that sends it's IMEI data after a sucessfull TCP
> socket is established with a Unix Server.
> It waits for a sucess or failure ( 1 or 0) from the Server. The GPRS
> decvice then sends it's packet of data back to server (approx 100
> bytes).
>
> Can somebody help me find out how I can rearrange code below to read
> IMEI data send back a 1 and read 100 byte data and save incomming data
> to a logfile (say log.txt)

No you can't just rearrange it - you need to add the statements that
read and write. Oh yes and you need to open the log file too.

You need first to be 100% clear by what you mean by glib phrases like
"sends it's IMEI" (fixed length record? Terminated record? (What
terminator?) String representation? Binary representation? (Which
one?) Terminated record? (What terminator?).

Same questions again for "send back a 1".

To send a byte 1.

print CLIENT "\x01";

To read fixed length records use read() (you can also use readline()
by setting the read terminator $/ to a reference to a scalar
containing the number of records in the if you prefer).

read ( CLIENT, my $record, 100) or whatever...

As for logging a 100 byte binary record to a textual log file unpack/
pack/sprintf are your friends..

print LOG unpack 'H*', $record;

What format would you like it in?

Can you be more precise about where you are getting stuck?

Posted by paul f on April 14, 2008, 5:04 pm
Please log in for more thread options
>
> > Hi there ,
> > I have aGPRSdevice that sends it's IMEI data after a sucessfull TCP
> > socket is established with a Unix Server.
> > It waits for a sucess or failure ( 1 or 0) from the Server. TheGPRS
> > decvice then sends it's packet of data back to server (approx 100
> > bytes).
>
> > Can somebody help me find out how I can rearrange code below to read
> > IMEI data send back a 1 and read 100 byte data and save incomming data
> > to a logfile (say log.txt)
>
> No you can't just rearrange it - you need to add the statements that
> read and write. Oh yes and you need to open the log file too.
>
> You need first to be 100% clear by what you mean by glib phrases like
> "sends it's IMEI" (fixed length record? Terminated record? (What
> terminator?) String representation? Binary representation? (Which
> one?) Terminated record? (What terminator?).
>
> Same questions again for "send back a 1".
>
> To send a byte 1.
>
> print CLIENT "\x01";
>
> To read fixed length records use read() (you can also use readline()
> by setting the read terminator $/ to a reference to a scalar
> containing the number of records in the if you prefer).
>
> read ( CLIENT, my $record, 100) or whatever...
>
> As for logging a 100 byte binary record to a textual log file unpack/
> pack/sprintf are your friends..
>
> print LOG unpack 'H*', $record;
>
> What format would you like it in?
>
> Can you be more precise about where you are getting stuck?

How do I send two bytes to Client zero then 1 (01 hex)?

Similar ThreadsPosted
Socket programming blunders.... February 3, 2005, 4:45 pm
Socket programming muddle November 25, 2005, 5:43 pm
Socket programming muddle November 25, 2005, 8:12 pm
A question about socket programming January 12, 2007, 8:49 am
Client socket programming eating up CPU/memory April 12, 2005, 6:31 am
Server-side programming language -- which is best? January 6, 2006, 6:53 pm
IO::Socket server September 5, 2006, 1:23 am
Server-side programming for form processing January 6, 2006, 7:55 pm
IO::Socket echo server not echoing April 22, 2005, 3:31 pm
sample client server socket issue March 6, 2008, 5:35 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap