For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > how can I send messages from both sides through socket?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how can I send messages from both sides through socket?
B. W.

2004-05-20, 10:38 am

I want to send a request from A to B through socket, then B returns the
process result back to A for display. I run B script first then start A
script, but I got error message at B side "can't use an undefined value as a
symbol reference at line 'print $sock_send "$result";'. How can I have both
sides to support sending messages?

the script I created is as:

A side script:
use IO::Socket;
my $sock_recv = new IO::Socket::INET(Localhost
=>'localhost';

LocalPort =>'7071',

Proto =>'tcp',

Listen =>1,

Reuse =>1,

Timeout =>20,
);
die "error create receive socket" unless $sock_recv;

$sock_send = new IO::Socket::INET( PeerAddr
=>'localhost',

PeerPort =>'7070',

Proto =>'tcp',
);
die "error create send socket" unless $sock_send;

print $sock_send "$request";

$new_sock = $sock_recv->accept();
while (defined ($buf = <$new_sock> )) {
print $buf;
}

close ($sock_send);
close ($sock_recv);

B side script:
use IO::Socket;
my $sock_recv = new IO::Socket::INET(Localhost
=>'localhost';

LocalPort =>'7070',

Proto =>'tcp',

Listen =>1,

Reuse =>1,

Timeout =>20,
);
die "error create receive socket" unless $sock_recv;

$new_sock = $sock_recv->accept();
while (defined ($buf = <$new_sock> )) {
$sock_send = new IO::Socket::INET( PeerAddr
=>'localhost',

PeerPort =>'7070',

Proto =>'tcp',
);
die "error create send socket" unless $sock_send;

$result = <some lines to get result from received
$buf>;
print $sock_send "$buf";
close ($sock_send);
}
close ($sock_recv);


thanks,
B.W.


A. Sinan Unur

2004-05-20, 1:32 pm

"B. W." <mysympatico001@sympatico.ca> wrote in
news:x32rc.69143$325.1485289@news20.bellglobal.com:

> I want to send a request from A to B through socket, then B returns
> the process result back to A for display. I run B script first then
> start A script, but I got error message at B side "can't use an
> undefined value as a symbol reference at line 'print $sock_send
> "$result";'. How can I have both sides to support sending messages?
>
> the script I created is as:
>
> A side script:
> use IO::Socket;
> my $sock_recv = new IO::Socket::INET(Localhost
> =>'localhost';
>
> LocalPort =>'7071',
>
> Proto =>'tcp',
>
> Listen =>1,


You might want to spend some time properly formatting your code for
others to want to read it.

When one side is done sending, it should tell the other side it is done
sending:

perldoc -f shutdown.

--
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)
Eric Bohlman

2004-05-20, 1:32 pm

"B. W." <mysympatico001@sympatico.ca> wrote in
news:x32rc.69143$325.1485289@news20.bellglobal.com:

> I want to send a request from A to B through socket, then B returns
> the process result back to A for display. I run B script first then
> start A script, but I got error message at B side "can't use an
> undefined value as a symbol reference at line 'print $sock_send
> "$result";'. How can I have both sides to support sending messages?
>
> the script I created is as:
>
> A side script:
> use IO::Socket;


No 'use strict'.
No 'use warnings'.

Let perl find as many of your problems as it can.

> my $sock_recv = new IO::Socket::INET(Localhost
> =>'localhost';


That semicolon should be a comma. Given that I see this in two different
places in your code, and that nowhere does your code contain the
actual statement that you claim gives the error message, I can only
conclude that the code you posted resembles, but is not identical to, the
code you're actually trying to run. Posting mutant code is a Bad Thing;
all it accomplishes is wasting everyone's time. None of us here, AFAIK,
have the psychic powers they'd need in order to determine what your actual
code is from what you've posted.

Try again (*after* enabling warnings and strictures and correcting anything
that perl complains about when you do), but this time *copy and paste* the
*exact* code that's giving you the problem. Not something "like" it, but
the Real Thing. Post something that any of us can copy from our
newsreaders and run without having to alter it. Otherwise the people who
know what they're talking about will refuse to engage in futile guesswork,
and any responses you'll get will be likely to come from people who may not
really know what they're talking about.

Jim Gibson

2004-05-21, 3:32 pm

In article <x32rc.69143$325.1485289@news20.bellglobal.com>, B. W.
<mysympatico001@sympatico.ca> wrote:

> I want to send a request from A to B through socket, then B returns the
> process result back to A for display. I run B script first then start A
> script, but I got error message at B side "can't use an undefined value as a
> symbol reference at line 'print $sock_send "$result";'. How can I have both
> sides to support sending messages?


1. You don't have the line 'print $sock_send "$result"' in your code.

2. You need to use bind() and listen() calls in your server (script B).
Get a book on network socket programming or check out 'perldoc perlipc'
and search for 'Sockets'.

3. You can send data both ways over a socket. There is no need to use
two sockets.


[ scripts snipped]
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com