Hi wavenator,

You seem to be trying to make two socket connections to the same port on the same host.  What you'll need to do is to move the logic for the socket connections into each of the threads, setup individual server and client socket connections, and use the accept call to accept and handle the client connection.

Here's a working example, based on your code, with a few modifications:

use strict; use warnings; + use threads; use IO::Socket::INET; my $port = 1300; my $thread = threads->create("server_thread"); my $thread1 = threads->create("client_thread"); sub server_thread { my %params = ( LocalHost => '127.0.0.1', LocalPort => $port, Proto => 'tcp', Listen => 1, Reuse => 1, ReuseAddr => 1, ); my $so = new IO::Socket::INET(%params); die "Server Socket Error: $!\n" unless $so; print STDERR "[Server Socket Connected]\n"; + my $client = $so->accept(); $client->autoflush(1); + while (1) { my $txt = <$client>; last unless defined($txt); chomp $txt; last if ($txt eq 'quit'); print "server : $txt\n"; } print "[Server finished]\n"; } sub client_thread { sleep 1; # Give the server a head start my %params = ( 'PeerAddr' => '127.0.0.1', 'PeerPort' => $port, 'Proto' => 'tcp', 'ReuseAddr' => 1, ); my $so = new IO::Socket::INET(%params); $so->autoflush(1); die "Client Socket Error: $!\n" unless $so; print STDERR "[Client Socket Connected]\n"; while (my $msg = <STDIN>) { chomp $msg; last if ($msg eq 'quit'); print $so "$msg\n"; } print "[Client finished]\n"; } $thread->join(); $thread1->join();

The client thread waits for 1 second, to make sure the server thread has time to connect first.  Then the server continues to fetch input from the filehandle returned by the accept call, until the user types "quit", at which point both the server and client threads finish.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: socket with threads problem by liverpole
in thread socket with threads problem by wavenator

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.