Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help with chat server

  Asked By: Tracey    Date: Mar 10    Category: Java    Views: 2030
  

Iam a very begineer in java programming and no knowledge of
networks.I have an assignment to do.Can somebody help me with that.

Goal: Develop a chat server and a chat client using Java.


You need only one port number for this project, even if you are
going to run both the client along with the server on same system.

Client programs may run either from your home PC or from school .
Alternatively, you may run one client from school (system s1)and
other client from home PC. You need to run at least 2 client
processes connected to the server at a time.

Initially, Server process is running on s1 on an assigned port
number, then start a client processes to send user-chosen unique
username through a datagram to the server using hostname or by using
IP address and the port number. Similarly, start another client, but
select a different username. The usernames are not authenticated
anywhere, they are used to identify the clients -- those who are in
the chat session currently. There is no need of passwords either.

When the server receives a datagram with username, it makes an entry
of that client (username) in an array. The array holds information
such as

Username ClientIPaddress ClientPortNumber
A xxx.xxx.xxx.xxx xxxxxx
B xxx.xxx.xx?.xx? xxxx??

the IP address could be different from that of server and
port numbers and are different if they are on the same host.
However, you do not need to know what port number the client will
use to receive the datagram thru. The UDP protocol that serves your
client will decide what port to receive the response of the server
from. The UDP on the client side will send that info along with the
datagram to the server each time it makes a request. The UDP on the
server end will have to extract the information of the client's IP
and port number that came along with the datagram and your Server
program has to make an entry in the array as above.

Any time a client sends a datagram, the server program will check to
see if the user has chosen a unique userid. The server's response
would be either Welcome <username> or Please choose another
username.

There are two types of datagrams the client will send to the user.
The first is a LOGIN datagram. LOGIN datagram will have following
strings in it.

LOGIN <USERID>

where <USERID> is the actual userid chosen by the user, for example.

LOGIN A

The other type of datagram is a message. This datagram has following

format.
> sentence.
for example,
> Hi Everybody.

The server will look for first string of the datagram to distinguish
between a login datagram and a message datagram.

Now, Server will send a received message to all the clients that are
in the array.Server will add the username of the sender (client)
along with the message that was received from it and broadcast the
message to all the users in the array. Therefore, the sender
(client) will receive its own message back as others will receive it
as well.

When the user logs out the server does not know. Therefore, the
client should send a datagram telling the server to log the client
out.
LOGOUT

There is no username to be sent with this. So that users will not be
able to log other users out.
The server will lookup the IP address of the sender in the array and
it will remove the client's entry from the array.
If the client process terminates unusually, then the server will not
know that the client is not online anymore.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Jacob Evans     Answered On: Mar 10

This article should get you started.

www.javaworld.com/.../jw-01-chat.html

 
Answer #2    Answered By: Chaths Massri     Answered On: Mar 10

here's a part of a chat  server program.Iam not able to correct some
errors here.Can somebody help  me with this?



import java.applet.*;
import java.awt.*;
import java.awt.List;
import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.event.*;
/*
<applet code ="chatApplet" width =475 height =350>
</applet>
*/
public class chatApplet extends Applet implements ActionListener
{
static TextArea msg_txt = null;
static TextArea chat_txt = null;
static TextField name_txt = null;
String CONNECT ="Connect";
String DISCONNECT = "Disconnect";
String send  ="send";
static Socket soc = null;
PrintStream ps= null;
static List list = null;
static Listen listen = null;

public void init()
{
resize(475,350);
setLayout( new BorderLayout());
Panel con = new Panel();
con.setLayout(new FlowLayout());
name_txt = new TextField(20);
name_txt.setEditable(true);
con.add(new Label("Name:"));
con.add(name_txt);
con.add(new Button("Connect"));
con.add(new Button("Disconnect"));
add("North",con);

Panel msg = new Panel();
msg.setLayout(new FlowLayout());
msg_txt = new TextArea(3,30);
msg_txt.setEditable(true);
msg.add(new Label("Message:"));
msg.add(msg_txt);
msg.add(new Button("Send"));
add("South",msg);

Panel view = new Panel();
view.setLayout(new FlowLayout());
chat_txt = new TextArea(15,30);
chat_txt.setEditable(false);
view.add(chat_txt);
list = new List(13,false);
view.add(list);
add("Center",view);


}
public void stop()
{
disconnect();
}

public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Connect"))
{
if(soc==null)
{
try
{
soc = new Socket
(InetAddress.getLocalHost(),2525);
ps= new PrintStream
(soc.getOutputStream());
ps.println(name_txt.getText
());
ps.flush();
listen = new Listen
(this,name_txt.getText(),soc);
listen.start();

}
catch(IOException e)
{
System.out.println("Error" +
e);
disconnect();
}
}
}
else if (str.equals("Disconnect"))
{
disconnect();
}
else if (str.equals("Send"))
{
if(socket != null)
{
StringBuffer msg = new StringBuffer
("MSG: ");
ps.println(msg.append(msg_txt.getText
()));
ps.flush();
}
}


public void disconnect()
{
if(soc != null)
{
try
{
listen.suspend();
ps.println("QUIT");
ps.flush();
soc.close();
}
catch(IOException e)
{
System.out.println("error" +
e);
}
finally
{
listen.stop();
listen=null;
soc=null;
list.clear();
}
}
}
}
class Listen extends Thread
{
String name = null;
DataInputStream dis = null;
PrintStream ps = null;
Socket socket = null;
chatApplet parent = null;

public Listen(chatApplet p,String n, Socket s)
{
parent =p;
name= n;
socket=s;
try
{
dis = new DataInputStream(s.getInputStream
());
ps = new PrintStream(s.getOutputStream());

}
catch(IOException e)
{
System.out.println("Error:" +e );
parent.disconnect();
}
}
public void run()
{
String msg = null;
while(true)
{
try
{
msg.readLine();
}

catch(IOException e)
{
System.out.println("Error:" +e );
parent.disconnect();
}
if (msg == null)
{
chatApplet.listen = null;
chatApplet.socket=null;
chatApplet.list.clear();
return;
}
StringTokenizer st = new StringTokenizer
(msg,":");
String keyword = st.nextToken();
if(keyword.equals("PEOPLE"))
{
chatApplet.list.clear();
while(st.hasMoreTokens())
{
String str =st.nexttoken();
chatApplet.list.addItem(str);
}
}

else if(keyword.equals("MSG"))
{
String usr=st.nextToken();
chatApplet.chat_txt.appendText(usr);
chatApplet.chat_txt.appendText
(st.nextToken("\0"));
chatApplet.chat_txt.appendText
("\n\n");
}
else if(keyword.equals("QUIT"))
{
chatApplet.listen= null;
chatApplet.socket=null;
chatApplet.list.clear();

}
}


}
}

 
Answer #3    Answered By: Tarron Thompson     Answered On: Mar 10

I am sure someone can. But then again, that's probably not the
question you wanted to have answered, is it? Maybe you should ask a
more well-formed question.

 
Didn't find what you were looking for? Find more on Help with chat server Or get search suggestion and latest updates.




Tagged: