-
Notifications
You must be signed in to change notification settings - Fork 0
/
发送方和接收方通信
43 lines (41 loc) · 1.33 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
41
42
43
import java.net.*;
import java.io.*;
class Sender {
public static void main(String[] args) {
DatagramPacket dp = null;
DatagramSocket ds = null;
try {
ds = new DatagramSocket(3000);
} catch(IOException e) {}
String str = "hello world! ";
try {
dp = new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("localhost"),9000);
} catch (UnknownHostException e) {}
try {
ds.send(dp);
} catch(IOException e) {}
ds.close();
}
}
class Reciever {
public static void main(String[] args) {
DatagramSocket ds = null;
byte[] buf = new byte[1024];
DatagramPacket dp = null, dp1 = null;
try {
ds = new DatagramSocket(9000);
} catch(SocketException e) {}
dp = new DatagramPacket(buf,1024);
try {
ds.receive(dp);
} catch(IOException e) {}
String str = new String(dp.getData(),0,dp.getLength()) + " from " + dp.getAddress().getHostAddress() + ":" + dp.getPort();
System.out.println(str);
String s = "I have received!";
try {
dp1 = new DatagramPacket(s.getBytes(),s.length(),InetAddress.getByName("localhost"),3000);
ds.send(dp1);
} catch(IOException e) {}
ds.close();
}
}