If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > problem using IO::socket

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-17-06, 03:45
steven@be steven@be is offline
Registered User
 
Join Date: Oct 2006
Posts: 2
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);
Reply With Quote
  #2 (permalink)  
Old 10-20-06, 02:50
sco08y sco08y is offline
Registered User
 
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.
Reply With Quote
  #3 (permalink)  
Old 10-20-06, 07:57
steven@be steven@be is offline
Registered User
 
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?
Reply With Quote
  #4 (permalink)  
Old 10-22-06, 15:08
sco08y sco08y is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On