[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Socket code - nonblocking in perl

I am writing a script to send a 40 byte header to the server, then the server will respond with 40 bytes of data which I will parse.

This data will be sent to me every 1 second.  This is what I have so far:
#Connect to socket

#Send 40 bytes of data to the server
  my $sock = IO::Socket::INET->new(Proto => 'tcp',
                                   Type  => SOCK_STREAM);
  my $addr = sockaddr_in($port,inet_aton($host));
  my $result = $sock->connect($addr);
#while loop - to get request from server (which will be sent every second)
  #Get data from server    
  print $socket $db
  #Parse this data - store into a file.


I want the connection to remain active even if there is no data passed to the client.
I already have the code to write it to a file.  I guess I am unsure as far as how to keep the connection active...

  • 4
  • 2
2 Solutions
can't you simply do:

    while($sock->connected) {
        $sock->read($buffer, 40);

oh, and maybe add a "sleep(1);" line after writing?
jeweeAuthor Commented:
Maybe i misinterpreted.  I was trying to follow the format of winsock...but yes, that would be much easier!  perl newbie re: the socket programming.

However, what happens if I do not receive 40 bytes, maybe more?  I guess I could read in 24 bytes (which is the header size), then extract the message size from within there.  How would I access the buffer, as far as extracting the first 4 bytes, checking to see if a bit is set within the 1st 4 bytes, then the 2nd 4 bytes, get the message size?

I'm not sure as far as how it is stored.  I'm assuming I should use unpack?

I was told to use nonblocking sockets for this, just to avoid it waiting.

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

> However, what happens if I do not receive 40 bytes, maybe more?

I suppose the remainder will stay in the socket, but I don't know right now how that would affect the following write. Your approach sounds sensible enough, if you have a header that contains the message size.

> I'm assuming I should use unpack?

That would be my first suggestion, yes.

> I was told to use nonblocking sockets for this, just to avoid it waiting.

That does make sense, although I don't see a particular reason in your code yet why waiting for new data would be so bad.
jeweeAuthor Commented:
How would i access the buffer?  Thanks!

    $sock->read($buffer, 24);

this reads 24 bytes into $buffer. You'd then unpack that according to the layout of your header.

    my ($flags, $size, $rest) = unpack "C4 C4 C16", $buffer;

(C stands for unsigned char (only does bytes)).

    $flag & 0x02; # test for second bit

unpack "C4 C4 C16", $buffer; #unpacks 24 fields of numbers from 0 to 255
unpack "a4 a4 a16", $buffer; #unpacks 3 string fields with lengths 4, 4, and 16
unpack "N N a16", $buffer; #unpacks 2  32 bit integers in big-endian order, and a 16 character string


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now