Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Unanswered: problem using IO::socket

    I am new to perl, so please excuse me for this stupid question.

    I am trying to make a client program interact with a server program. This is working fine except for the fact that i can not make the server program send text to the client program. maybe it does, but the client program does not print it on the screen.

    Can someone have a look at a snapshot of the code i used?

    server.pl
    ######

    Code:
    while ($new_sock = $sock->accept()) {
    
    	$hostinfo = gethostbyaddr($new_sock->peeraddr);
    
    	$sel->add($new_sock); # let $sel manage the $new_sock handle
    
    	if ($sel->can_read()){
    
    		while (defined ($buf = <$new_sock>)) { 
    			printf "   Message from %s : $buf", $hostinfo->name || $new_sock->peerhost;
    
    
    		}
    
    	}
    
    	if ($sel->can_write()){
    
    		$msg="my text for the client here\n";
    		print $new_sock $msg;
    
    	}
    
    }

    client.pl
    #####

    Code:
    $sock = IO::Socket::INET->new(PeerAddr => $host,
    			PeerPort => $port,
    			Proto =>'tcp');
    die "Socket could not be created. Reason: $!\n" unless $sock;
    
    $sel = IO::Select->new;
    $sel->add($sock);
    
    if ($sel->can_write()){
    			print $sock "hello server\n";;
    	}
    
    if ($sel->can_read()){
    	# is it possible to read something ? TIME OUT =$time_out seconds,
    
    	while (defined ($buf = <$sock>)) { 
    		# receive data
    		printf STDOUT $buf; # write them to STDOUT
    	}
    } else { 
    	print "Time out receiving data\n"; # print time out message
    }
    
    $sel->remove($sock);
    
    close ($sock);

  2. #2
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    I wouldn't expect a socket to be instantly accessible after you've done something with it... How about replacing those ifs with something more aggressive, like:

    Code:
    while(!$sel->can_write()) { }
    If that doesn't work:

    Either fire up perl -d and run a trace or put lots of print STDERR "reached this" statements in so we can see that you're actually getting to the right places.

    Also, I suggest you use a packet sniffer to monitor your traffic.

  3. #3
    Join Date
    Oct 2006
    Posts
    2
    I tried the while(!$sel->can_write()) {} as you suggested this did not do the trick for me. As i can see when try several adjustments to my program is:

    the thing to do to get into can_write block of server.pl is to close the connection from client side by pressing CTRL+C(not the way to do it but for the moment...). I can see this gives me the possibility to print something on the sever. Maybe at this point i can print something to the clients but i will never receive this as i closed the session from the client side.

    Is there a way that i can send messages from the client to the server and otherwise at the same time.

    I am not familiar with the use of perl -d, and the trace option in it. Can you give me an example how to use the trace option once i am in perl -d?

  4. #4
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    I don't often roll my own network code because, as you're discovering, it's a real pain.

    You won't get anywhere just messing around; you need to work out your protocol or you'll just have endless bugs.

    IIWY, I'd adapt Net::Telnet for your client and Net:: Server for your server.

    Read perldoc perldebug for debugger info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •