perl chat server

M

Majid Dehghan

hey everyone,
i'm new to Perl,i'm curios to know how to send a message to a specific user, EX:from user A to user B,not to everyone,
thanks in advance.
 
J

Jürgen Exner

Majid Dehghan said:
i'm new to Perl,i'm curios to know how to send a message to a specific user, EX:from user A to user B,not to everyone,

Usually you use email. On CPAN (cpan.org) you will find numerous modules
that help dealing with any aspect related to email.

jue
 
M

Majid Dehghan

thanks for the reply,
but this is a simple chat server,i mentioned it in subject,
after accepting clients,i want to be able to send a message from one user to another one,just the two of them (something like private message)
like keeping all the connected socket in an array,but i just don't know how to!
thanks
 
M

Majid Dehghan

much appreciated Jim Gibson,
but you see ("# Handle a connection. Register the new user, and broadcast a message # to whoever is already connected.")
i don't want that,i just want to send a message from one user to only another one,not all connected client.
i googled it too,but i need someone to guide me

thanks
 
J

Jürgen Exner

Majid Dehghan said:
thanks for the reply,
but this is a simple chat server,i mentioned it in subject,
after accepting clients,i want to be able to send a message from one user to another one,just the two of them (something like private message)
like keeping all the connected socket in an array,but i just don't know how to!
thanks

Honestly, what on earth are you talking about? Do you want to write an
Instant Messaging System (IM) from scratch? Do you want to connect to an
existing IM server using a Perl client? Do you have a multi-user chat
system up and running (WHICH ONE?) and want to enhance its
functionality? What do you want?

Sockets and arrays are implementation details which are waaaaaaaay to
detailed at this time.

jue
 
M

Majid Dehghan

Consider a chat server,clients connect to this server and chat to each other,this is my code so far:
use strict;
use IO::Socket;
use IO::Select;
my $srv=IO::Socket::INET->new(LocalPort=>'5000',Proto=>'tcp',Reuse=>1,Listen=>5) or die 'SOCKET';
print "SEVER READY\nWaiting For Connection\n";
my $sel=IO::Select->new;
$sel->add($srv);

while(my @inp=$sel->can_read()) {
foreach my $c (@inp) {

if($c eq $srv) {
my $clnt=$srv->accept();
my $host=$clnt->peerhost();
my $port=$clnt->peerport();
my $c=$sel->count;
print $clnt "--- $c users(s) online ---\nPress q for exit\n";
$sel->add($clnt);
print "Got Connection from \"$host\" at port $port\n";
}
else {
my $msgin=<$c>;
chop($msgin);
chop($msgin);
if($msgin eq "q") {
$sel->remove($c);
$c->close; }

else {
foreach($sel->can_write()) {
print $_ $msgin;}}
}
}}
but whenever any client try to send a message it'll be broadcast,consider i want to get a message from the client#2 and send it to client#3 what should i do?
i hope i make myself clear.
 
J

Jim Gibson

Majid Dehghan said:
much appreciated Jim Gibson,
but you see ("# Handle a connection. Register the new user, and broadcast a
message # to whoever is already connected.")
i don't want that,i just want to send a message from one user to only another
one,not all connected client.
i googled it too,but i need someone to guide me

You need a way for clients to identify themselves to each other and to
the server. Then, the client needs to specify to which other client his
message will be sent. Then, instead of sending each incoming message to
all clients, you send it to only the client specified by the sender.

When a client connects, you should still probably send out a message to
all clients informing them that a new client has joined the service, is
ready to receive messages, and what their "handle" is. You will also
have to send the new client the list of existing clients.
 
J

Jim Gibson

Majid Dehghan said:
thanks Jim
the serve.

could you be more specific,
how to do that?

That is up to you. Each client needs to have some short, text unique
name by which to identify himself. I think you have two basic
approaches:

1. You assign each client a unique name when they connect.
2. You allow each client to provide a name, then check to make sure
that name is unique.

The name must be known to all of the clients, so each client can decide
to whom he is sending a message.

Just how this is done depends upon the number of clients, how well they
know each other, and how they normally identify themselves to each
other. I have no idea what would be appropriate to your environment,
and that is really outside the scope of this newsgroup.

Once you have decided upon a scheme for uniquely identifying clients,
then how to implement that scheme is within the scope of this
newsgroup.

Once you have a unique name for each client, you can use that name
within your program as array elements or hash keys to maintain
information about each client.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top