forked from deepdalsania/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPChat_Server.java
More file actions
40 lines (33 loc) · 1.01 KB
/
UDPChat_Server.java
File metadata and controls
40 lines (33 loc) · 1.01 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
package netpck;
import java.io.BufferedReader;
// import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPChat_Server {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(1203);
byte rb[] = new byte[1024];
byte sb[] = new byte[1024];
while (true) {
DatagramPacket rp = new DatagramPacket(rb, rb.length);
ds.receive(rp);
String rs = new String(rp.getData());
rs = rs.trim();
System.out.println("[SERVER] Received message is : " + rs);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("[SERVER] Enter your message : ");
String s = br.readLine();
sb = s.getBytes();
InetAddress ip = rp.getAddress();
int port = rp.getPort();
DatagramPacket sp = new DatagramPacket(sb, sb.length, ip, port);
ds.send(sp);
if (rs.equalsIgnoreCase("bye")) {
break;
}
}
ds.close();
}
}