import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
// class is used to maintaning the list of user name
//class is used to received the messages
public class MyClient extends javax.swing.JFrame {
Socket s,s1,s2;
DataInputStream din;
DataOutputStream dout;
DataOutputStream dlout;
DataOutputStream dout1;
DataInputStream din1;
static String name;
JFrame frame;
// DefaultListModel list;
//DefaultListModel list1;
public MyClient(String name) throws IOException{
initComponents();
//list=new DefaultListModel();
//list1=new DefaultListModel();
// list=new JList(model);
//list1=new JList(model1);
s=new Socket("localhost",1004); //creates a socket object
s1=new Socket("localhost",1004);
s2=new Socket("localhost",1004);
//create inputstream for a particular socket
din=new DataInputStream(s.getInputStream());
//create outputstream
dout=new DataOutputStream(s.getOutputStream());
//sending a message for login
dout.writeUTF(name+" has Logged in");
dlout=new DataOutputStream(s1.getOutputStream());
dout1=new DataOutputStream(s2.getOutputStream());
din1=new DataInputStream(s2.getInputStream());
// creating a thread for maintaning the list of user name
My1 m1=new My1(dout1,model1,name,din1);
Thread t1=new Thread(m1);
t1.start();
//creating a thread for receiving a messages
My m=new My(din,model);
Thread t=new Thread(m);
t.start();
}
public void windowClosing(WindowEvent w){
try{
dlout.writeUTF(name);
dlout.flush();
Thread.currentThread().sleep(1000);
System.exit(1);
}
catch(Exception oe){}
}
/**
* 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">
private void initComponents() {
button = new javax.swing.JButton();
lout = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
tf = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
model = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
model1 = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button.setText("Send");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
lout.setText("Logout");
lout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loutActionPerformed(evt);
}
});
jLabel1.setText("message:");
jScrollPane1.setViewportView(model);
jScrollPane2.setViewportView(model1);
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lout))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
.addComponent(button))
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)))))
.addGap(25, 25, 25))
);
layout.setVerticalGroup(
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(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button)
.addComponent(jLabel1)
.addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(lout)
.addGap(34, 34, 34))
);
pack();
}// </editor-fold>
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
String str="";
str=tf.getText();
tf.setText("");
str=name+": > "+str;
try{
dout.writeUTF(str);
System.out.println(str);
dout.flush();
}
catch(IOException ae){
System.out.println(ae);}
}
private void loutActionPerformed(java.awt.event.ActionEvent evt) {
frame.dispose();
try{
//sending the message for logout
dout.writeUTF(name+" has Logged out");
dlout.writeUTF(name);
dlout.flush();
Thread.currentThread().sleep(1000);
System.exit(1);
}
catch(Exception oe){}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new MyClient(name).setVisible(true);
} catch (IOException ex) {
//Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton button;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JButton lout;
private javax.swing.JList model;
private javax.swing.JList model1;
private javax.swing.JTextField tf;
// End of variables declaration
}
class My1 implements Runnable{
DataOutputStream dout1;
//DefaultListModel model1;
JList model1;
DataInputStream din1;
String name,lname;
ArrayList alname=new ArrayList(); //stores the list of user names
ObjectInputStream obj; // read the list of user names
int i=0;
// My1(DataOutputStream dout1,DefaultListModel model1,String name,DataInputStream din1){
// this.dout1=dout1;
// this.model1=model1;
// this.name=name;
// this.din1=din1;
// }
My1(DataOutputStream dout1, JList model1, String name, DataInputStream din1) {
this.dout1=dout1;
this.model1=model1;
this.name=name;
this.din1=din1;
}
@Override
public void run(){
try{
new MyClient(name).setVisible(true);
dout1.writeUTF(name); // write the user name in output stream
while(true){
obj=new ObjectInputStream(din1);
//read the list of user names
alname=(ArrayList)obj.readObject();
if(i>0)
//model1.clear();
model1.removeAll();
Iterator i1=alname.iterator();
System.out.println(alname);
while(i1.hasNext()){
lname=(String)i1.next();
i++;
//add the user names in list box
//model1.addElement(lname);
model1.add(lname, model1);
}
}
}catch(Exception oe){}
}
}
//class is used to received the messages
class My implements Runnable{
DataInputStream din;
DefaultListModel model;
// My(DataInputStream din, DefaultListModel model){
// this.din=din;
// this.model=model;
// }
My(DataInputStream din, JList list) {
this.din=din;
this.model=model;
}
public void run(){
String str1="";
while(true){
try{
str1=din.readUTF(); // receive the message
// add the message in list box
model.addElement(str1);
}
catch(Exception e){}
}
}
}