converting normal server to multi-client one

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • über
    New Member
    • Sep 2007
    • 31

    converting normal server to multi-client one

    I have normal server code here, and i have no idea how to make it work with many clients. The server would print what one client says to every client.
    Code:
    #!/usr/bin/perl -w
    use IO::Socket::INET;
    use strict;
    
    my $port = '1584';
    
    my $socket = IO::Socket::INET->new('LocalPort' => $port,
                                       'Proto' => 'tcp',
                                       'Listen' => SOMAXCONN)
        or die "Can't create socket ($!)\n";
    print "[Server created at port:$port]\n";
    while (my $client = $socket->accept) {
    $client->autoflush(1);
        my $name = gethostbyaddr($client->peeraddr, AF_INET);
        printf "[Connect from %s]\n", $client->peerhost;
        my $port = $client->peerport;
        while (<$client>) {
            print "[$name $port] $_";
            print $client "[$name $port] $_";
        }
        close $client
            or die "Can't close ($!)\n";
    }
    die "Can't accept socket ($!)\n";
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Originally posted by Icecrack
    This seems like a script kiddy, script at least part of it,

    and from the previous post he/she has posted it looks like it,
    I am not sure what a script kiddy is or what your implication is. The code looks benign to me, I don't know how to answer his question though.

    Comment

    • Icecrack
      Recognized Expert New Member
      • Sep 2008
      • 174

      #3
      Can i ask what is this being used for, and we don't understand what you mean

      Comment

      • über
        New Member
        • Sep 2007
        • 31

        #4
        im making a chat script so clients can connect to one server and speak in there, now it works with only with 2 persons so every person who want chat would need to make own servers and get the own clients to other person servers
        and then the desktop will be full of those command prompt windows
        Last edited by über; Oct 20 '08, 11:45 AM. Reason: desktop* not desktot

        Comment

        • Icecrack
          Recognized Expert New Member
          • Sep 2008
          • 174

          #5
          Originally posted by über
          im making a chat script so clients can connect to one server and speak in there, now it works with only with 2 persons so every person who want chat would need to make own servers and get the own clients to other person servers
          and then the desktop will be full of those command prompt windows

          is that the whole script??, i would like to look at the client script
          because from what i have followed up on this it should accept more then one connection.

          Comment

          • über
            New Member
            • Sep 2007
            • 31

            #6
            Here is the client script
            Code:
            #!/usr/bin/perl
            #didnt make this
            use IO::Socket::INET;
            use strict;
            
            my $name = 'localhost';
            my $port = '1584';
            
            my $socket = IO::Socket::INET->new('PeerAddr' => $name,
                                               'PeerPort' => $port,
                                               'Proto' => 'tcp')
                or die "Can't create socket ($!)\n";
            print "Client sending\n";
            while (<STDIN>) {
                print $socket $_;
                print scalar <$socket>;
            }
            close $socket
                or die "Can't close socket ($!)\n";
            The problem is when i connect the server with 2 clients, the server prints that "127.0.0.1 has connected" only once and when i try to send something with the other client the server wont print it

            Comment

            • Icecrack
              Recognized Expert New Member
              • Sep 2008
              • 174

              #7
              Originally posted by über
              Here is the client script
              Code:
              #!/usr/bin/perl
              #didnt make this
              use IO::Socket::INET;
              use strict;
              
              my $name = 'localhost';
              my $port = '1584';
              
              my $socket = IO::Socket::INET->new('PeerAddr' => $name,
                                                 'PeerPort' => $port,
                                                 'Proto' => 'tcp')
                  or die "Can't create socket ($!)\n";
              print "Client sending\n";
              while (<STDIN>) {
                  print $socket $_;
                  print scalar <$socket>;
              }
              close $socket
                  or die "Can't close socket ($!)\n";
              The problem is when i connect the server with 2 clients, the server prints that "127.0.0.1 has connected" only once and when i try to send something with the other client the server wont print it
              I'm Testing your code will get back to you shortly,

              thanks

              Comment

              • Icecrack
                Recognized Expert New Member
                • Sep 2008
                • 174

                #8
                Originally posted by Icecrack
                I'm Testing your code will get back to you shortly,

                thanks

                sorry im still looking at your code and testing this...

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  The code should not compile, I spotted at least one syntax error:

                  Code:
                  print $socket $_;
                  That should probably be:

                  Code:
                  print "$socket $_";

                  Comment

                  • Icecrack
                    Recognized Expert New Member
                    • Sep 2008
                    • 174

                    #10
                    Originally posted by KevinADC
                    The code should not compile, I spotted at least one syntax error:

                    Code:
                    print $socket $_;
                    That should probably be:

                    Code:
                    print "$socket $_";

                    it does compile Kevin, i have tested it, i think its more to do with the port being used on one computer im trying to test this not on the same machine (Server on one, 2 clients on 2 other machines)

                    Comment

                    Working...