Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sockets? returning multi-line data from server?

Status
Not open for further replies.

parkers

Vendor
Oct 21, 2002
157
GB
Hi,

I've just started looking at Sockets as a method of getting data from one server to another.

My setup is relatively simple having a client and server system. The client sends commands to the server over TCP and ideally should receive multiple lines worth of data back for processing. The problem I have is that I get client 'hangs' which prevents the application from exiting when I ask for, then receive information from the server: (some data is sent back OK)

Client Application: (relevent code shown only)

Code:
$sock->autoflush(1);

print $sock "COMMAND\n"; # send command to server

# print output from server to client application
# end application will store to array

while (<$sock>)
	{
	print scalar <$sock>;
	}
close ($sock);

note: if I know how much information is returned from the server then I can retrieve as follows:

Code:
print scalar <$sock>;
print scalar <$sock>;
...
print scalar <$sock>;

unfortunately however the information returned is not fixed.


Server Application: (relevent code only)

Code:
STDOUT->autoflush(1);
my($new_sock, $buf);

while ( $new_sock = $sock->accept() )
	{
	while (defined($buf = <$sock_handle>))
		{
		# respond to client:
		foreach ($buf) 
			{
			 /^COMMAND$/ and 
	        	
	        	# e.g. return name of all text  files back to server 
	        	opendir (PATH,$path);
			foreach my $entry ( readdir PATH ) 
				{
				if ($entry =~ /\.txt/i)
					{
					print $sock_handle $entry,"\n";
					}
				}
			closedir (PATH);
                 	last;
                 	}
                 }
         close ($sock_handle);
         }

I'd really appreciate any hints you can provide on how to resolve...

Steven
 
note: noticed a typo ... any reference to $new_sock should actually be $sock_handle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top