By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

converting normal server to multi-client one

P: 31
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.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use IO::Socket::INET;
  3. use strict;
  4.  
  5. my $port = '1584';
  6.  
  7. my $socket = IO::Socket::INET->new('LocalPort' => $port,
  8.                                    'Proto' => 'tcp',
  9.                                    'Listen' => SOMAXCONN)
  10.     or die "Can't create socket ($!)\n";
  11. print "[Server created at port:$port]\n";
  12. while (my $client = $socket->accept) {
  13. $client->autoflush(1);
  14.     my $name = gethostbyaddr($client->peeraddr, AF_INET);
  15.     printf "[Connect from %s]\n", $client->peerhost;
  16.     my $port = $client->peerport;
  17.     while (<$client>) {
  18.         print "[$name $port] $_";
  19.         print $client "[$name $port] $_";
  20.     }
  21.     close $client
  22.         or die "Can't close ($!)\n";
  23. }
  24. die "Can't accept socket ($!)\n";
Oct 19 '08 #1
Share this Question
Share on Google+
9 Replies


KevinADC
Expert 2.5K+
P: 4,059
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.
Oct 20 '08 #2

Icecrack
Expert 100+
P: 174
Can i ask what is this being used for, and we don't understand what you mean
Oct 20 '08 #3

P: 31
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
Oct 20 '08 #4

Icecrack
Expert 100+
P: 174
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.
Oct 21 '08 #5

P: 31
Here is the client script
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #didnt make this
  3. use IO::Socket::INET;
  4. use strict;
  5.  
  6. my $name = 'localhost';
  7. my $port = '1584';
  8.  
  9. my $socket = IO::Socket::INET->new('PeerAddr' => $name,
  10.                                    'PeerPort' => $port,
  11.                                    'Proto' => 'tcp')
  12.     or die "Can't create socket ($!)\n";
  13. print "Client sending\n";
  14. while (<STDIN>) {
  15.     print $socket $_;
  16.     print scalar <$socket>;
  17. }
  18. close $socket
  19.     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
Oct 21 '08 #6

Icecrack
Expert 100+
P: 174
Here is the client script
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #didnt make this
  3. use IO::Socket::INET;
  4. use strict;
  5.  
  6. my $name = 'localhost';
  7. my $port = '1584';
  8.  
  9. my $socket = IO::Socket::INET->new('PeerAddr' => $name,
  10.                                    'PeerPort' => $port,
  11.                                    'Proto' => 'tcp')
  12.     or die "Can't create socket ($!)\n";
  13. print "Client sending\n";
  14. while (<STDIN>) {
  15.     print $socket $_;
  16.     print scalar <$socket>;
  17. }
  18. close $socket
  19.     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
Oct 22 '08 #7

Icecrack
Expert 100+
P: 174
I'm Testing your code will get back to you shortly,

thanks

sorry im still looking at your code and testing this...
Oct 24 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
The code should not compile, I spotted at least one syntax error:

Expand|Select|Wrap|Line Numbers
  1. print $socket $_;
That should probably be:

Expand|Select|Wrap|Line Numbers
  1. print "$socket $_";
Oct 24 '08 #9

Icecrack
Expert 100+
P: 174
The code should not compile, I spotted at least one syntax error:

Expand|Select|Wrap|Line Numbers
  1. print $socket $_;
That should probably be:

Expand|Select|Wrap|Line Numbers
  1. 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)
Oct 25 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.