in reply to Bidirectional use of a Socket

The socket is bidirectional and not.

This depends on the meaning of your question. If you are asking about whether you can both write to and read from the same socket, the answer is yes, the socket is bidirectional, you can write to and also read from the same socket.

On the other hand, you also asked the question about the difference between LocalPort and PeerPort. In that sense, socket is not "bidirectional", as a socket can either act as a server or a client, but not both. If you want the socket to act as a server and listen for connection from client, you create it with LocalPort; if you want the socket to act as a client and initiates teh connection to a server, then you use PeerPort (in which case, a LocalPort on the local is picked for you).

Replies are listed 'Best First'.
Re^2: Bidirectional use of a Socket
by scotchfx (Sexton) on Jul 04, 2005 at 00:44 UTC
    What if I initiate the conversation (send a message) then need to listen for a response. If I open a socket:
    my $t_socket = IO::Socket::INET->new( 'PeerAddr' => "localhost", 'PeerPort' => $T_PORT, 'Proto' => 'tcp') or die "Second: Can't create socket to Third($!)\n";
    Send some data..
    if($name eq $match) { print "Second found match: $name\n"; print "Second sending \"$name\" to Third\n"; print $t_socket "$match\n"; }
    Then listen for a response...
    while(my $third = $t_socket->accept) { print "Second has accepted socket to Third...\n"; my $t_host = gethostbyaddr($third->peeraddr, AF_INET); my $t_port = $third->peerport; while (<$third>) { my $name = $_; chomp($name); print "Received from [$f_host $f_port]: $name\n"; if($name eq $match) { print "Received \"$name\" from Third\n"; last; } else { last; } } }
    Do I need to do a $t_socket->accept if the connection has already been established?

    Can you tell what I am doing wrong in the above example (since it's not working)?

    Thanks!

      One obvious problem is that accept. You don't need to accept, as the connection (between client and server) has been created already, at the time when you call ->new().

      On the client side, the porgram above, you can use $t_socket for both write and read, to "listen for response", all what you need to do is <$t_socket>. Of course , for better control, you can always use select, which is something you want to learn next step.

        Is there a way to tell if there is an incoming message without having to go into a while(<$t_socket>){...} loop?

        (something like an if(<$t_socket>) or perhaps a function I can call on the $t_socket itself?

        Thanks!

        I still can't seem to get the following section of code to work - it never enters the while<$t_socket>{...} loop to receive input from the Third process (the one it sends $name)

        if($name eq $match) { print "Second found match: $name\n"; print "Second sending \"$name\" to Third\n"; print $t_socket "$name\n"; while(<$t_socket>) { #print "Second has accepted socket to Third...\n"; my $name = $_; chomp($name); my $t_host = gethostbyaddr($t_socket->peeraddr, AF_INE +T); my $t_port = $t_socket->peerport; print "Received from [$f_host $f_port]: $name\n"; } }

        Do I need to do anything in particular to "listen" on the <$t_socket>?