Hi,
I have a perl script which uses socket calls in perl.
Server Side:
use Socket;
my $port = shift || 7001;
my $proto = getprotobyname('tcp');
my $server = "localhost";
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "Can't open socket
+ $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "Can't set sock
+et option to SO_REUSEADDR $!\n";
bind( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "Can
+'t bind to port $port! \n";
listen(SOCKET, 5) or die "listen: $!";
my $client_addr;
while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
send(NEW_SOCKET,"Hello",0);
close NEW_SOCKET;
}
Client Side:
use Socket;
my $host = shift || 'localhost';
my $port = shift || 7001;
my $server = "localhost";
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]) or die "
+Can't create a socket $!\n";
connect( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "
+Can't connect to port $port! \n";
my $line;
my $val;
my $p;
my $iaddr;
$line = recv(SOCKET,$val,5,0);
($p, $iaddr) = sockaddr_in($line);
if(defined $line){
print "$val from $iaddr\n";
}
close SOCKET or die "close: $!";
The problem happens only when i use the statement
($p, $iaddr) = sockaddr_in($line);. The recv call receives the message "Hello". If the statement
($p, $iaddr) = sockaddr_in($line); is removed, the script receives the correct value in recv and will print the same. Hence the problem is only with the return value in $line of recv which seems to be 'undef' in this case.
Could you please help to identify the issue here?
Thank you!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.