views:

1141

answers:

8

I want to devlop java chat application. I am looking for any java framework that will help me in devloping chat application. So that i can easliy devlop the chat application using the framework. The application needs to run only in LAN. I want the chat application like yahoo chat (with only one main room).

Please suggent me any open source framework. If you know any open source chat application readiy availabe plz proveide the link.

The following are detailed requirement.

I need a simple Java (GUI Based) Chat server / Client that can run on different platforms, I mean like Java not O/S dependent.

Things I will need in it

1) Server file: File can run on any machine and can accept connection from client regardless what ever their IP on Local Network such as 192.168.0.xxx. I mean they can connect from any machine on network without specify IP address every time. Note: User authentication required and no two users from same ID can connect at a time. All users details will save in simple text file.

2) Client: Able to connect to server over LAN. can sent msg (like Mirc etc) on main window. Can send private msh to users and able to send email using that software.

3) Menu bar in client option: every day options such as register, login, chat save, send pvt. msg, send email. close etc.

4) Interface is not a issue just need simple Java GUI interface, I have no special requirement for network settings can use any port or ip address such as 192.168.0.xxx. ASCII text will be used so easily use on Unix and

+3  A: 

Hello,

You nerver used the word "Socket". So I think you have to take a look at Sockets

This is a bit the basic:

Server

ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
BufferedWriter writer = new BufferedWriter(new StreamWriter(socket.getOutputStream()));
writer.write("Hello, how are you?");
writer.flush();

Client

Socket socket = new Socket("192.168.1.xxx", port); // you can use another address.
BufferedReader reader = new BufferedReader(new StreamReader(socket.getInputStream()));
String message = reader.readLine();
System.out.println(message);

I hope this helps!

Martijn Courteaux
+3  A: 

Have a look at JXTA - https://jxta.dev.java.net/ They also have demo application MyJXTA which meets your requirements you can download it here

Its source is available here

Xinus
+1 for JXTA. Great demo
Martijn Courteaux
+2  A: 

You should take a look at OpenFire

non sequitor
+1  A: 

Besides the already mentioned Sockets for the communication you could use Swing for a simple GUI.

Fabian Steeg
+2  A: 

It depends on what you want out of this - a learning experience or an end product.

If it's a learning experience, feel free to implement the front-end/back-end/protocol yourself using sockets like others have suggested.

If it's an end product that you want (or you're more interested in building a chat client itself), I'd build on non-sequitor's suggestion and suggest using OpenFire as the back-end server. To expound on that suggestion - OpenFire uses the XMPP/Jabber protocol - this is a widely used XML-based protocol used by many chat applications. The same folks who provide OpenFire have also provided the Smack API - a Java binding for the XMPP/Jabber protocol. You can use this to write your client's interface to the server.

Nate
+1  A: 

Check out KryoNet. It comes with a concise, documented chat client/server example that uses Swing for a GUI. You can use the Client.discoverHost method to automatically find the IP of the chat server running on the LAN. You will need to implement authentication yourself, but it should be straightforward after reading the KryoNet documentation. You will also need to implement private messages, menus, and the other features you want, just use the chat example as a foundation to build on.

It also has an RMI based chat example.

NateS
thanks a lot.This framework is very good and also the examples are good.Have u worked on it?Do u have some more exapmles?
Thanks, glad you like it. I'm the author of KryoNet (and Kryo). You can find more example code in the "test" folder in the KryoNet source.
NateS
A: 
//Server.java

import java.net.*;
import java.io.*;
import java.util.*;
public class Server{
   private ServerSocket ss;
   private Socket socket; 
   Map<Socket,DataOutputStream> map=new HashMap<Socket,DataOutputStream>();
      public static void main(String[] args)
      {
           try
           {
                int port=Integer.parseInt(args[0]);
    new Server(port);
           }
          catch(NumberFormatException nfe)
           {
    System.out.println("Usage:> java Server <portno(int)>");
    return;
           }
          catch(ArrayIndexOutOfBoundsException aioobe)
          {
                System.out.println("Usage:> java Server <portno(int)>");
    return;
          }    
      } 
      public Server(int port)
      {
          try{
    listen(port);
                }
          catch(IOException ioe)
          {
                ioe.printStackTrace();
          }
      }
      private  void listen(int port) throws IOException{
         ss=new ServerSocket(port);
         System.out.println("Server started");
         while(true)
               {
                   socket=ss.accept();
                   System.out.println("connected "+socket);
                   DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
                   map.put(socket,dout);
                   new ServerThread(this,socket); 
               }
      }
     public void sendToAll(String message,Socket cur_soc)
     {
            synchronized(map){
    Set<Socket> socket_set=map.keySet();
    Iterator<Socket> itr=socket_set.iterator();
    while(itr.hasNext())
    {
      try{
                Socket s=itr.next();
                if(s!=cur_soc)
                 {  
                    DataOutputStream dout=map.get(s);
                    dout.writeUTF(message);
                 } 
            }
     catch(IOException ioe){} 
    }
            }   
     }   
     public void removeConnection(Socket soc)
     {
         synchronized(map)
          { 
             map.remove(soc);
             System.out.println("removed "+soc); 
             try
               {
                  soc.close();  
               }
             catch(IOException e)
             {
             }
         }
     }   
 }


//ServerThread.java
import java.net.*;
import java.io.*;
public class ServerThread extends Thread{
    private Server server;
    private Socket socket;
        public ServerThread(Server server,Socket socket)
        {
            this.server=server;
    this.socket=socket;
    start();
        }
       public void run()
       {
           try{
        DataInputStream din=new DataInputStream(socket.getInputStream());
    while(true)
    {
         String message=din.readUTF();
         server.sendToAll(message,socket);
    }
                }
           catch(EOFException eof){}
           catch(IOException ioe){}
           finally{
                 server.removeConnection(socket); 
           }    
       } 
}

// client.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Client1.java
 *
 * Created on May 17, 2010, 1:28:24 AM
 */

/**
 *
 * @author COM
 */

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Client1 extends javax.swing.JFrame implements Runnable {


    /** Creates new form Client1 */
    public Client1() {
        initComponents();
        registerComponents();
    }
   public void run(){ 
         try
           {
               while(true)
    {
         String msg=din.readUTF();
         jconv_area.append(msg+"\n");    
    }        
           }
         catch(IOException ioe){
    System.out.println("IOError");
         }
    } 
   private void registerComponents(){
        jlogin_btn.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
          if(jlogin_field.getText().equals(""))
        juser_info.setText("Please specify Name");
          else{
                    try{
            addr=new InetSocketAddress(InetAddress.getLocalHost().getHostName(),5000);
            //wait while connection establishes
                setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
            socket.connect(addr);
            din=new DataInputStream(socket.getInputStream());
            dout=new DataOutputStream(socket.getOutputStream());
            new Thread(Client1.this).start(); 
           }
                    catch(UnknownHostException uhe){ 
             JOptionPane.showMessageDialog(Client1.this,"Unknown Host!!!","Global-Chat",JOptionPane.INFORMATION_MESSAGE);
             return;
                     }  
                    catch(IOException ioe){
             JOptionPane.showMessageDialog(Client1.this,"Connection refused!!!","Global-Chat",JOptionPane.INFORMATION_MESSAGE);
             return;
                     }  
                   finally{
             setCursor(java.awt.Cursor.getDefaultCursor());          
                   }    

        login_name=jlogin_field.getText();
        juser_info.setText("Welcome "+login_name);
        jlogin_btn.setEnabled(false);
        jlogin_field.setEnabled(false);
        jmsg_area.setEnabled(true);
                       jsend.setEnabled(true);
        jdisconnect.setEnabled(true);
                  }
    }
        });

       jdisconnect.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){

                    if(ae.getActionCommand().equals("Really???"))
         try{
                 socket.close();
                 Client1.this.setVisible(false);
                 System.exit(0);        
               }
         catch(IOException ioe){} 

     if(ae.getActionCommand().equals("Disconnect"))
                 jdisconnect.setText("Really???");
     }
      });

     jmsg_area.addKeyListener(new KeyAdapter(){
              public void keyPressed(KeyEvent ke){
                 try{
       if(ke.getKeyCode()==KeyEvent.VK_ENTER){
              write();
       }
                  }  
                  catch(IOException ioe){}
              }
    });

   jsend.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
    try{
           write();
          }
    catch(IOException ioe){} 
    }
  }); 
}
   private void write() throws IOException{
         if(jdisconnect.getText().equals("Really???"))
     jdisconnect.setText("Disconnect");
         String msg=jmsg_area.getText().trim(); 
         System.out.print(msg); 
         dout.writeUTF(login_name+":"+msg);  
         jconv_area.append("You:"+msg+"\n");     
         jmsg_area.setText("");
         jmsg_area.moveCaretPosition(0);    

   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        String look="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    try{
                           UIManager.setLookAndFeel(look); 
                          SwingUtilities.updateComponentTreeUI(this);
         }
    catch(Exception e){}

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jconv_area = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jlogin_field = new javax.swing.JTextField();
        jlogin_btn = new javax.swing.JButton();
        juser_info = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jmsg_area = new javax.swing.JTextArea();
        jmsg_area.setEnabled(false);

        jdisconnect = new javax.swing.JButton(); 
        jdisconnect.setEnabled(false);

        jsend = new javax.swing.JButton();
        jsend.setEnabled(false);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Global-Chat");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 51, 102));
        jLabel1.setText("G");
        jPanel1.add(jLabel1);

        jLabel2.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(11, 187, 59));
        jLabel2.setText("LO");
        jPanel1.add(jLabel2);

        jLabel3.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(50, 8, 241));
        jLabel3.setText("BA");
        jPanel1.add(jLabel3);

        jLabel4.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(249, 11, 237));
        jLabel4.setText("L");
        jPanel1.add(jLabel4);

        jLabel5.setFont(new java.awt.Font("Tempus Sans ITC", 1, 24)); // NOI18N
        jLabel5.setForeground(java.awt.Color.orange);
        jLabel5.setText("C H A T");
        jPanel1.add(jLabel5);

        jconv_area.setColumns(20);
        jconv_area.setEditable(false);
        jconv_area.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
        jconv_area.setForeground(new java.awt.Color(255, 102, 102));
        jconv_area.setRows(5);
        jScrollPane1.setViewportView(jconv_area);

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel6.setText("Login Name");

        jlogin_btn.setText("Login");

        juser_info.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        juser_info.setText("Login to Chat");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)
                        .addComponent(jlogin_field, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jlogin_btn))
                    .addComponent(juser_info, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlogin_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(jlogin_btn)
                .addGap(26, 26, 26)
                .addComponent(juser_info)
                .addContainerGap(234, Short.MAX_VALUE))
        );

        jmsg_area.setColumns(20);
        jmsg_area.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jmsg_area.setForeground(new java.awt.Color(6, 123, 252));
        jmsg_area.setLineWrap(true);
        jmsg_area.setRows(5);
        jmsg_area.setWrapStyleWord(true);
        jmsg_area.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jmsg_area.setMargin(new java.awt.Insets(5, 10, 2, 2));
        jScrollPane2.setViewportView(jmsg_area);

        jdisconnect.setText("Disconnect");

        jsend.setText("Send");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jdisconnect)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 444, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jsend, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jsend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jdisconnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Client1().setVisible(true);
            }
        });
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jconv_area;
    private javax.swing.JButton jdisconnect;
    private javax.swing.JButton jlogin_btn;
    private javax.swing.JTextField jlogin_field;
    private javax.swing.JTextArea jmsg_area;
    private javax.swing.JButton jsend;
    private javax.swing.JLabel juser_info; 

    private Socket socket=new Socket();
    private InetSocketAddress addr; 
    private String login_name;
    private DataInputStream din;
    private DataOutputStream dout;



    // End of variables declaration//GEN-END:variables

}
rohit
A: 

I know this isn't a programming answer but isn't this easily solved by just setting up a private irc server on your LAN?

Tyler Brock