how can I send messages from both sides through socket?

B

B. W.

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

A. Sinan Unur

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.
 
E

Eric Bohlman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top