Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

CMD ie ChatServer & chat client

Tue Feb 03, 2009 7:17 pm

i do have a Chat application which is quite good in all terms.
According to the project

1)Start the server using CMD ie ChatServer.java

2)Run the ChatClient with this command --javac ChatClient TOM

3)again open a new CMD repeat the above step 3 with different name
say Sam
Code example :
java code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.rmi.*;
import java.util.*;
import java.rmi.server.*;

public class ChatClient extends JFrame implements ChatClientInterface,ActionListener
{
CenterPanel centerPanel;
EastPanel eastPanel;
SouthPanel southPanel;
Container contentPane;
String clientName;
//String refIdentity="rmi://localhost:1099/Anoop";
String refIdentity;
ChatServerInterface serverRef;
Hashtable hs;

public ChatClient(final String clientName)
{
super("Chat Client "+clientName);
this.clientName=clientName;
refIdentity="serverstub";
hs=new Hashtable();
contentPane=getContentPane();
centerPanel=new CenterPanel();
eastPanel=new EastPanel();
southPanel=new SouthPanel();

contentPane.add(centerPanel,BorderLayout.CENTER);
contentPane.add(eastPanel,BorderLayout.EAST);
contentPane.add(southPanel,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
southPanel.sendBtn.addActionListener(this);
southPanel.textField.addActionListener(this);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try
{
serverRef.logout(clientName);
}
catch (Exception exception)
{
System.out.println(exception);
}
}
});

eastPanel.list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if (evt.getClickCount()==2)
{
String hisName=(String)eastPanel.list.getSelectedValue();
MiniWindow mw=(MiniWindow)hs.get(hisName);
if (mw==null)
{
try
{
ChatClientInterface hisRef=serverRef.giveRef(hisName);
mw=new MiniWindow(hisName,hisRef);
hs.put(hisName,mw);
}
catch (Exception e)
{
System.out.println(e);
}
}
eastPanel.list.clearSelection();
southPanel.textField.requestFocus();
}
}
});
pack();
setVisible(true);
setResizable(false);
try
{
//ChatServerInterface
serverRef=(ChatServerInterface)Naming.lookup(refIdentity);
UnicastRemoteObject.exportObject(this);
serverRef.login(clientName,this);
}
catch (Exception e)
{
System.out.println(e);
}
}

public void pm(final String hisName,final ChatClientInterface hisRef,final String hisMsg)
{
MiniWindow mw=(MiniWindow)hs.get(hisName);
if (mw==null)
{
mw=new MiniWindow(hisName,hisRef);
hs.put(hisName,mw);
}
mw.p1.ta.append(hisMsg+"\n");
}

public void actionPerformed(ActionEvent e)
{
String clientMsg=southPanel.textField.getText();
clientMsg=clientName+" : "+clientMsg;
try
{
serverRef.takeMsg(clientMsg);
}
catch (Exception exception)
{
System.out.println(exception);
}
southPanel.textField.setText("");
}

public void takeMsg(String serverMsg)
{
centerPanel.textArea.append(serverMsg+"\n");
}

public void takeClientList(Vector clientNames)
{
eastPanel.listModel.removeAllElements();
Enumeration en=clientNames.elements();
while (en.hasMoreElements())
{
String clientName=(String)en.nextElement();
eastPanel.listModel.addElement(clientName);
}
}

class MiniWindow extends JFrame implements ActionListener
{
Panel1 p1;
Panel2 p2;
String hisName;
ChatClientInterface hisRef;

public MiniWindow(final String hisName,final ChatClientInterface hisRef)
{
super("From "+clientName+ " to "+hisName);
this.hisName=hisName;
this.hisRef=hisRef;
p1=new Panel1();
p2=new Panel2();
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
hs.remove(hisName);
}
});
p2.t.addActionListener(this);
p2.b.addActionListener(this);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
setResizable(false);
p2.t.requestFocus();
}

public void actionPerformed(ActionEvent evt)
{
String msg=p2.t.getText();
msg=clientName + ":" +msg;
try
{
hisRef.pm(clientName,ChatClient.this,msg);
}
catch (Exception e)
{
System.out.println(e);
}
p1.ta.append(msg+"\n");
p2.t.setText("");
}

class Panel1 extends JPanel
{
JTextArea ta;
JScrollPane sp;
public Panel1()
{
ta=new JTextArea(10,20);
add(sp=new JScrollPane(ta));
ta.setEditable(false);
ta.setBackground(Color.lightGray);
Border raisedbevel=BorderFactory.createRaisedBevelBorder();
TitledBorder title=BorderFactory.createTitledBorder(raisedbevel,"View Massages");
title.setTitleJustification(TitledBorder.CENTER);
ta.setBorder(title);
}
};

class Panel2 extends JPanel
{
JTextField t;
JButton b;

public Panel2()
{
add(t=new JTextField(10));
Border backline=BorderFactory.createLineBorder(Color.black);
TitledBorder title=BorderFactory.createTitledBorder(backline,"Enter Massage");
title.setTitleJustification(TitledBorder.CENTER);
t.setBorder(title);
add(b=new JButton("SEND"));
}
};
};

public static void main(String args[])
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e)
{
System.out.println(e);
}
new ChatClient(args[0]);

}
};

class CenterPanel extends JPanel
{
JTextArea textArea;
JScrollPane scrollpane;

public CenterPanel()
{
textArea=new JTextArea(20,35);
add(scrollpane=new JScrollPane(textArea));
textArea.setEditable(false);
textArea.setOpaque(true);
textArea.setBackground(Color.lightGray);
Border raisedbevel=BorderFactory.createRaisedBevelBorder();
TitledBorder title=BorderFactory.createTitledBorder(raisedbevel,"View Massages");
title.setTitleJustification(TitledBorder.CENTER);
textArea.setBorder(title);
}
};

class EastPanel extends JPanel
{
JList list;
JScrollPane scrollpane;
DefaultListModel listModel;
public EastPanel()
{
listModel=new DefaultListModel();
list=new JList(listModel);
add(scrollpane= new JScrollPane(list));
list.setVisibleRowCount(20);
list.setBackground(Color.lightGray);
Border raisedbevel=BorderFactory.createRaisedBevelBorder();
TitledBorder title=BorderFactory.createTitledBorder(raisedbevel,"View Clients");
title.setTitleJustification(TitledBorder.CENTER);
list.setBorder(title);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
};

class SouthPanel extends JPanel
{
JTextField textField;
JButton sendBtn;

public SouthPanel()
{
add(textField=new JTextField(30));
Border blackline=BorderFactory.createLineBorder(Color.black);
TitledBorder title=BorderFactory.createTitledBorder(blackline,"Enter Message");
title.setTitleJustification(TitledBorder.CENTER);
textField.setBorder(title);
add(sendBtn=new JButton("Send Message"));
setSize(40,100);
}

};

now with a single machine iam able to chat with TOM and Sam


But my Question is suppose this iam deploying in actual LAN

1)What Changes should i make and how ?


2)How will machine number 1 will access the server and then to the right Machine number


iam a beginner in java so pls do help.
i am attaching the program also.

thank u
[email protected]


Attachments
Chat Application.rar
(17.82 KiB) Downloaded 1166 times
Last edited by DrRakha on Sun Jan 20, 2013 10:20 pm, edited 2 times in total.
Reason: changing title

Re: Need Your help to Exceute this Java Code

Wed Feb 04, 2009 12:18 am

finished-projects/java-chat-t644.html
this code will help you so much :)

Re: CMD ie ChatServer & chat client

Sun Jan 20, 2013 10:53 pm

updated.

Post a reply
  Related Posts  to : CMD ie ChatServer & chat client
 Command line chat (client and server)     -  
 Java Chat Program with client & Server     -  
 Chat client and server, save messages and delete     -  
 client server client     -  
 Chat Help     -  
 Java Chat     -  
 Chat Room in JSP     -  
 I need chat application using php     -  
 video chat     -  
 MultiClient Bluetooth Chat     -  

Topic Tags

Java Networking