-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientChat.java
More file actions
60 lines (57 loc) · 1.37 KB
/
ClientChat.java
File metadata and controls
60 lines (57 loc) · 1.37 KB
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
class ClientChat extends JFrame implements ActionListener, Runnable
{
JTextArea msgArea;
JTextField msgText;
JButton sendButton;
DatagramSocket ds;
int clientport=10,serverport=11;
Thread receiveThread;
ClientChat() throws Exception
{
msgArea=new JTextArea(10,20);
msgText=new JTextField(10);
sendButton=new JButton("Send");
setLayout(new FlowLayout(FlowLayout.CENTER));
add(msgArea);
add(msgText);
add(sendButton);
ds=new DatagramSocket(clientport);
receiveThread=new Thread(this);
receiveThread.start();
sendButton.addActionListener(this);
setBounds(10,10,400,400);
setVisible(true);
}
public void run()
{
byte b[]=new byte[1000];
while(true)
{
try
{
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String output=new String(b,0,dp.getLength());
msgArea.append("Others:"+output+"\n");
}catch(Exception ee){}
}
}
public void actionPerformed(ActionEvent e)
{
try
{
String data=msgText.getText();
DatagramPacket dp=new DatagramPacket(data.getBytes(),data.length(),InetAddress.getLocalHost(),serverport);
ds.send(dp);
msgArea.append("You:"+data+"\n");
}catch(Exception ee){}
}
public static void main(String args[]) throws Exception
{
ClientChat sc=new ClientChat();
}
}