-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPClient.java
More file actions
39 lines (30 loc) · 905 Bytes
/
UDPClient.java
File metadata and controls
39 lines (30 loc) · 905 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
38
39
import java.net.*;
import java.io.*;
import java.util.*;
public class UDPClient {
public static void main(String[] args) throws Exception{
DatagramSocket soc = new DatagramSocket(3001);
DataInputStream rfconsole = new DataInputStream(System.in);
PrintStream otconsole = new PrintStream(System.out);
//otconsole.print("Client: ");
//String s = rfconsole.readLine();
while(true){
byte[] b = new byte[100];
DatagramPacket dp = new DatagramPacket(b,b.length);
soc.receive(dp);
b = dp.getData();
String s = new String(b,0,dp.getLength());
otconsole.println("Server: " + s);
if(s.equals("exit"))
break;
otconsole.print("Client: ");
String s2 = rfconsole.readLine();
b = s2.getBytes();
dp = new DatagramPacket(b,b.length,InetAddress.getByName("localhost"),3000);
soc.send(dp);
if(s2.equals("exit"))
break;
}
soc.close();
}
}