Thursday, August 19, 2010

Sample Chat Client-Server Application

ServerApplication.java
==============================================

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

/**
*
*/

/**
* @author lavakushv
*
*/
public class ServerApp {
ArrayList clientOutputStreams;
public class ClientHandler implements Runnable{
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSocket){

try {
sock=clientSocket;
InputStreamReader isr=new InputStreamReader(sock.getInputStream());
reader=new BufferedReader(isr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
String message;
try {
while((message=reader.readLine())!=null)
{
System.out.println("read :"+message);
tellEveryone(message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void tellEveryone(String message) {
// TODO Auto-generated method stub
Iterator itr=clientOutputStreams.iterator();
while(itr.hasNext()){
PrintWriter pWriter=(PrintWriter)itr.next();
pWriter.println(message);
pWriter.flush();
}
}

}
public static void main(String[] args) {
// TODO Auto-generated method stub

new ServerApp().createSocket();
}
private void createSocket() {
// TODO Auto-generated method stub
clientOutputStreams=new ArrayList();
try {
ServerSocket socket=new ServerSocket(5000);
while(true){
Socket clientSocket=socket.accept();
PrintWriter writer=new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("Got a Connection to the Client");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

==================================================

Chat Client Source Code
===================================================
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

/**
*
*/

/**
* @author lavakushv
*
*/
public class ChatClient {
JTextField outgoing;
JTextArea incoming;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public void layOutDesign(){
JFrame frame= new JFrame("Simple Chat Client");
JPanel mainPanel= new JPanel();
incoming= new JTextArea(15,25);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller=new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing=new JTextField(20);
JButton sendButton=new JButton("Send");
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setupNetworking();
Thread readerThread=new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(400, 500);
frame.setVisible(true);
}
private void setupNetworking() {
// TODO Auto-generated method stub
try {
sock=new Socket("10.30.10.156", 5000);
InputStreamReader isR=new InputStreamReader(sock.getInputStream());
reader=new BufferedReader(isR);
writer=new PrintWriter(sock.getOutputStream());
System.out.println("Network Established.");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class SendButtonListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent ev) {
// TODO Auto-generated method stub
try{
writer.println(outgoing.getText());
writer.flush();
}catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}

outgoing.setText("");
outgoing.requestFocus();
}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ChatClient().layOutDesign();
}
public class IncomingReader implements Runnable{

String message;
@Override
public void run() {
// TODO Auto-generated method stub
try {
while((message=reader.readLine())!=null){
System.out.println("Read :"+message);
incoming.append(message+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

====================================================
Go to server code...
and change the IP of your System
then connect to the Clients


And enjoy the Chatting between your colleagues in your Network....


Thanks for Dropping into this.

lavkush

9 comments:

  1. Love your site man keep up the good work

    aroma essential oils

    ReplyDelete
  2. can i use this web databse to android chat

    ReplyDelete
  3. how can develop a chat application for client to server

    ReplyDelete
  4. This is one of the pleasurable post.Your blog information is very unique and good.I like your blog detail.
    Android app developers

    ReplyDelete
  5. So what exactly has this code anything to do with Android?

    ReplyDelete
  6. is this for android, or you want to kidding with people..... dofa

    ReplyDelete
  7. there is nothing Android about this !!

    ReplyDelete
  8. Thanks
    but kindly give all source code in on compressed folder.
    and with steps by steps.Either in which IDE it will run.

    ReplyDelete
  9. Dear Author is this android ? I cant even find a single line that resembles android ... This is pure java not used android SDK n all. Its not Android

    ReplyDelete