Total members 11893 |It is currently Tue Nov 05, 2024 6:37 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 1200 times


Last edited by DrRakha on Sun Jan 20, 2013 10:20 pm, edited 2 times in total.
changing title
Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

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

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 3 posts ] 

  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     -  
 Chat Room application     -  



Topic Tags

Java Networking
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com