Thursday, June 2, 2011

Chat Module

Server side coding.........

WhiteboardChatServer.java

Description : -
In this class, simple concept of socket programing is used.

new WhiteboardChatServer(s);

The program create a thread to store, share and edit the chat data. When a client request is accepted by initiator, for each client a new thread is started in server.


socket s is the reference of the incoming the socket. where ss is the server socket where the incoming socket data is stored.

osChat is the array List where the chat data is stored. the Array List is shared by all the other users.
Synchronized word is used on "osChat", because when a client is accessing the data other client can not access the same data.

Client Side coding : ----


this is client side code
when client wants to connect with server, he sends a socket to server, if the server accepts the request , he begins to chat!!
here two separate process is working.
1) Updating the chat box.(read)
2) if any text is entered, it is sent to server!(write)

n=new ChatInputThread(www,streamOut);

This line is used to create the write operation
while(true)
{
try
{
String line = console.readLine();
String Line=www.ccf.getText();
www.ccf.setText(Line+line);
}
catch(Exception e)
{
System.out.print("chatclient error"+e);
}
}
ths operation is used for continuous updation of the chat box, if new data is read then it will update the text field, ccf.

ChatInputThread.java



This thread is used to scan the input continuously, if any data is inserted and the submit button is pressed then this thread helps to send the data to server.

here
Whiteboard s1;
s1 stores the reference of the containing whiteboard.

String line=s1.cf.getText();

This is the text which is entered in textfield cf and updated into the server.

s1.TextBoxUpdateFlag=0;
TextBoxUpdateFlag is a flag in main whiteboard, if any data is entered the flag becomes 1 (true)
after updating the data at server, the thread changes it with 0.

No comments:

Post a Comment