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