Problem regarding socket

V

Vikrant

hi

In the following code i am receving data from the client in a
buffer($sBuffer) and concatenating it into a another variable
$sData_Recevied.

My problem is that i can not understand what condition i should give in
do-while loop to exit if a certain time period elapses without any
data being received from the client. I have already tried the code
below, but it is not exiting the loop after 10 seconds.

########################################################################

#!/usr/bin/perl -w

use Socket;

$SOCKET_PORT =36545;

#Create
socket(hSERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));

$iListener_Socket = sockaddr_in($SOCKET_PORT, INADDR_ANY);

#Binding
bind(hSERVER,$iListener_Socket)
or die "Couldn't connect to Remote Host : $!\n";

#Listen
listen(hSERVER,4);
accept(hCLIENT,hSERVER);

$iEndTime = time() + 10;

do
{
recv(hCLIENT,$sBuffer,1024,0); #Receving Data at Socket
$sData_Recevied.=$sBuffer;

}while($iEndTime > time());

$sReturnXML = "Data has been recevied";

send(hCLIENT,$sReturnXML,0);

close(hCLIENT);
close(hSERVER);

#################################################################################

Thanks
Vikrant
 
P

Paul Lalli

Vikrant said:
hi

In the following code i am receving data from the client in a
buffer($sBuffer) and concatenating it into a another variable
$sData_Recevied.

My problem is that i can not understand what condition i should give in
do-while loop to exit if a certain time period elapses without any
data being received from the client. I have already tried the code
below, but it is not exiting the loop after 10 seconds.

$iEndTime = time() + 10;

do
{
recv(hCLIENT,$sBuffer,1024,0); #Receving Data at Socket
$sData_Recevied.=$sBuffer;

}while($iEndTime > time());

I'm willing to bet that recv() is a blocking call, meaning that the
code below it (both the concat-and-assign operation and the while lood
conditional) are never evaluated.

Have you considered using the alarm function? The documentation for
that function describes how to time-out long running processes:

perldoc -f alarm

Paul Lalli
 
V

Vikrant

hi

thanks for the information.i use the following code to solve the problem


######################################################
#!/usr/bin/perl -w

use IO::Socket::INET;
use IO::Select;

$socket = new IO::Socket::INET (LocalPort =>36545,
Proto => "tcp",
Listen => 4,
Type => SOCK_STREAM)
or die &WriteLog("Error creating socket");

$obSelected_Socket = IO::Select->new($socket);

while( $obSelected_Socket->can_read(20)) #Waiting for 20 seconds
{
sysread($socket,$sBuffer,1<<10);
$sData_Recevied.=$sBuffer;
}


##########################################################

vikrant
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top