jewee
asked on
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:
#Client
#Connect to socket
$socket
#Send 40 bytes of data to the server
my $sock = IO::Socket::INET->new(Prot
Type => SOCK_STREAM);
$sock->blocking(0);
my $addr = sockaddr_in($port,inet_ato
my $result = $sock->connect($addr);
#while loop - to get request from server (which will be sent every second)
while($sock)
{
#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...
oh, and maybe add a "sleep(1);" line after writing?
ASKER
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.
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.
jewee,
> 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.
> 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.
ASKER
How would i access the buffer? Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
while($sock->connected) {
$sock->read($buffer, 40);
$sock->write($db);
}
?