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!

TCP socket nonblocking

Status
Not open for further replies.

csudess

Technical User
Apr 1, 2010
14
HU
Hi everybody!

I have a code which is send hex data with tcp socket but sometimes when i cant creat a socket or i cant recv the socket my program is stuck it wont anymore i have to restart it! Who could i make my program to non blocking here is my sendig source

sub csillag_ki2
{

sleep($sleep_time);

local $sockx = new IO::Socket::INET (
PeerAddr => '192.168.0.11',
PeerPort => '2101',
Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sockx;



local @sebessegek=("\x1","\x2","\x3","\x4","\x5","\x6","\x7","\x8","\x9","\xa","\xb","\xc","\xd","\xe","\xf","\x10","\x11","\x12","\x13","\x14",);

local $bufx;


print $sockx "\xfe\xfd";

$sockx->recv($bufx,10000);
print "CSILLAG TILT START \n";

local $speedx=$kiir_seb->get();
local @beallitasx=("\x01","\x0d","\x38","\x2b","\x03","\x05","\x1b",@sebessegek[$speedx]);


local $hossz_bealx=@beallitasx;
#print $sock "\x01\x0d\x38\x2b\x03\x05\x1b\x0f\x0d";
local $CRC_x=@beallitasx[0];

for(local $ex1=1;$ex1<$hossz_bealx;$ex1++)
{
$CRC_x^=@beallitasx[$ex1];

}

#print $sockx @beallitasx;
#print $sockx $CRC_x;

print $sockx "\x05\x11\x19\x13\x04\x05\x1c\x0a\x09";

$sockx->recv($bufx,10000);

print $sockx "\x00\x06\x00\x0a\x0c";
$sockx->recv($bufx,10000);

print "PORT CLOSE \n";

close($sockx);

return;
}
 
IO::Socket seems to have a timeout parameter you can set - while this won't stop it blocking, if you set it to a low enough value you can always retry it a couple of times.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top