|
Tips & Tricks |
|
MyClient(String name)throws IOException{
frame = new JFrame(“Client Side”);
tf=new JTextField();
model=new DefaultListModel();
model1=new DefaultListModel();
label=new JLabel(“Message”);
list=new JList(model);
list1=new JList(model1);
button=new JButton(“Send”);
lout=new JButton(“Logout”);
scrollpane=new JScrollPane(list);
scrollpane1=new JScrollPane(list1);
JPanel panel = new JPanel();
button.addActionListener(this);
lout.addActionListener(this);
panel.add(tf);panel.add(button);
panel.add(scrollpane);
panel.add(label);panel.add(lout);
panel.add(scrollpane1);
scrollpane.setBounds(10,20,180,150);
scrollpane1.setBounds(250,20,100,150);
label.setBounds(20,180,80,30);
tf.setBounds(100,180,140,30);
button.setBounds(260,180,90,30);
lout.setBounds(260,230,90,30);
frame.add(panel);
panel.setLayout(null);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultClose
Operation(JFrame.EXIT_ON_CLOSE);
this.name=name;
frame.addWindowListener(this);
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 message
My m=new My(din,model);
Thread t=new Thread(m);
t.start();
}
public void actionPerformed(ActionEvent e){
// sending message
if(e.getSource()==button){
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);}
}
// client logout
if (e.getSource()==lout){
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){}
}
}
public void windowClosing(WindowEvent w){
try{
dlout.writeUTF(name);
dlout.flush();
Thread.currentThread().sleep(1000);
System.exit(1);
}catch(Exception oe){}
}
}
// class is used to maintaning the list of user
name
|
|
|
Nov
2007 | Java Jazz Up |62 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 Download PDF |
|
|
|
|
|
|
|
|
|