-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatUDP.java
98 lines (88 loc) · 3.15 KB
/
ChatUDP.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import javax.sound.sampled.Port;
import javax.swing.*;
import java.awt.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChatUDP extends JFrame {
private JTextArea taMain;
private JTextField tfMsg;
private final String FRM_TITLE = "Tiny Chat";
private final int FRM_LOC_X = 100;
private final int FRM_LOC_Y = 100;
private final int FRM_WIDTH = 400;
private final int FRM_HEIGHT = 400;
private final int PORT = 9876;
private final String IP_BROADCAST = "192.168.50.255";
private class thdReceiver extends Thread {
@Override
public void start(){
super.start();
try {
customize();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void customize() throws Exception{
DatagramSocket receiveSocket = new DatagramSocket(PORT);
while (true){
byte [] receiveData = new byte [4096];
DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
receiveSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
taMain.append(IP_BROADCAST.toString()+":"+PORT+": ");
taMain.append(sentence);
taMain.append("\r\n");
taMain.setCaretPosition(taMain.getText().length());
}
};
}
private void btnSend_Handler() throws Exception {
DatagramSocket sendSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(IP_BROADCAST);
byte[] sendData;
String sentence = tfMsg.getText();
tfMsg.setText("");
sendData = sentence.getBytes("UTF-8");
DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,PORT);
sendSocket.send(sendPacket);
}
private void framDraw (JFrame frame){
tfMsg = new JTextField();
taMain = new JTextArea(FRM_HEIGHT/19, 50);
JScrollPane spMain = new JScrollPane(taMain);
spMain.setLocation(0,0);
taMain.setLineWrap(true);
taMain.setEditable(false);
JButton btnSend = new JButton();
btnSend.setText("Send");
btnSend.setToolTipText("Broadcast a message");
btnSend.addActionListener(e -> {
try {
btnSend_Handler();
} catch (Exception ex) {
ex.printStackTrace();
}
});
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(FRM_TITLE);
frame.setLocation(FRM_LOC_X,FRM_LOC_Y);
frame.setSize(FRM_WIDTH,FRM_HEIGHT);
frame.setResizable(false);
frame.getContentPane().add(BorderLayout.NORTH,spMain);
frame.getContentPane().add(BorderLayout.CENTER,tfMsg);
frame.getContentPane().add(BorderLayout.EAST,btnSend);
frame.setVisible(true);
}
private void antistatic(){
framDraw(new ChatUDP());
new thdReceiver().start();
}
public static void main (String [] args){
new ChatUDP().antistatic();
}
}