Jump to content
Fórum Script Brasil
  • 0

Sockets Non-blocking


Fausto Lev

Question

Iai pessoal, td numa boa?

Pessoal estou com um problema quanto ao uso de sockets não bloqueadores que não estou conseguindo resolver.

Como podem ver no código abaixo o na craição do socket já está sando passado o parametro Blocking => 0 mas minha aplicação insiste em ficar presa na leiutura do socket impedindo que envie dados através dela.

Espero que alguém possa me ajudar!

se IO::Socket;

use IO::Select;

use IO::Handle;

use threads;

use threads::shared;

use Win32::Mutex;

if ($#ARGV < 0) {

print "Informe a porta que escutara as conexoes: "; $port_listen=<STDIN>;

} else {

$port_listen=$ARGV[0];

}

print "\nServidor sendo executado na porta $port_listen\n";

$socket =IO::Socket::INET->new(LocalPort =>$port_listen,

Type => SOCK_STREAM,

Proto => 'tcp',

Listen => 5,

Reuse=> 1,

Blocking => 0 );

$socket->autoflush(1);

$| = 1;

$ss = IO::Select->new();

$ss -> add ($socket);

$mutex = Win32::Mutex->new(init,'mutex');

while(1) {

@connections_pending = $ss->can_read();

foreach (@connections_pending) {

$remote = accept($fh, $_);

($port,$iaddr) = sockaddr_in($remote);

$peeraddress = inet_ntoa($iaddr);

print "Conexao estabelecida com $peeraddress na porta $port.\n";

$t = threads->create(\&new_connection, $fh);

$t->detach();

close($fh);

}

}

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

# Threads

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

sub new_connection {

$fh = shift;

binmode $fh;

while ($request_line = <$fh>) {

#print $request_line, "\n";

chomp($request_line);

chop($request_line);

@parameters=split(/ /,$request_line);

if ($parameters[0] eq "Get_toc") {

$mutex->wait(0);

print $fh &send_toc();

print $fh "\r\n";

$mutex->release;

}elsif ($parameters[0] eq "Get_site") {

$mutex->wait(0);

print $fh send_content($parameters[1]);

print $fh "\r\n";

$mutex->release;

}elsif ($parameters[0] eq "Edit_site") {

$mutex->wait(0);

update_content($parameters[1],$parameters[2], $parameters[3]);

$mutex->release;

}elsif ($parameters[0] eq "Close") {

close($fh);

threads->exit;

}

}

}

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Eu dei uma leitura rápida no manual do IO::Socket::INET e lá diz o seguinte:

Although it is not illegal, the use of MultiHomed on a socket which is in non-blocking mode is of little use. This is because the first connect will never fail with a timeout as the connect call will not block.

http://perldoc.perl.org/IO/Socket/INET.html

Também achei mais 3 referências no Google,

http://www.linuxquestions.org/questions/pr...in-perl-354499/

http://www.justskins.com/forums/non-blocki...-for-10990.html

http://coding.derkeiler.com/Archive/Perl/c...04-11/1915.html

Boa leitura.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.1k
    • Total Posts
      651.8k
×
×
  • Create New...