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