Repository navigation
Expand file tree
/
Copy pathEchoClient.java
More file actions
40 lines (37 loc) · 1.45 KB
/
Copy pathEchoClient.java
File metadata and controls
40 lines (37 loc) · 1.45 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class EchoClient {
public final static int SERVER_PORT = 7;
public final static String SERVER_IP = "127.0.0.1";
public final static byte[] BUFFER = new byte[4096];
public static void main(String[] args) {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
System.out.println("Client started ");
InetAddress server = InetAddress.getByName(SERVER_IP);
while(true) {
System.out.println("Enter your messenger: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String theString = br.readLine();
byte[] data = theString.getBytes();
DatagramPacket dp = new DatagramPacket(data, data.length, server, SERVER_PORT);
ds.send(dp);
DatagramPacket incoming = new DatagramPacket(BUFFER, BUFFER.length);
ds.receive(incoming);
System.out.println(new String(incoming.getData(), 0, incoming.getLength()));
}
} catch (IOException e) {
System.err.println(e);
} finally {
if( ds != null) {
ds.close();
}
}
}
}