-
Notifications
You must be signed in to change notification settings - Fork 106
/
MyUDPServer.java
43 lines (38 loc) · 1.19 KB
/
MyUDPServer.java
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
41
42
43
import java.net.*;
class MyUDPServer
{
public static int serverPort = 998;
public static int clientPort = 999;
public static int buffer_size = 1024;
public static DatagramSocket ds;
public static byte buffer[] = new byte[buffer_size];
public static void TheServer() throws Exception
{
int pos = 0;
System.out.println("\n Enter text(q to exit)... \n");
while (true) {
int c = System.in.read();
switch (c) {
case 'q':
System.out.println("Server Quits.");
return ;
case '\r':
break;
case '\n':
// for localhost target -----
ds.send(new DatagramPacket(buffer, pos, InetAddress.getLocalHost(), clientPort));
pos = 0;
break;
default:
buffer[pos++] = (byte) c;
break;
}
}
}
public static void main(String[] args) throws Exception
{
ds = new DatagramSocket(serverPort);
TheServer();
ds.close();
}
}