You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
808 B
23 lines
808 B
package 发送接收数据;
|
|
|
|
import java.io.IOException;
|
|
import java.net.DatagramPacket;
|
|
import java.net.DatagramSocket;
|
|
import java.net.InetAddress;
|
|
import java.net.SocketException;
|
|
|
|
public class receiverMessage {
|
|
public static void main(String[] args) throws IOException {
|
|
DatagramSocket ds=new DatagramSocket(10086);
|
|
byte[] bytes=new byte[1024];
|
|
DatagramPacket dp=new DatagramPacket(bytes,bytes.length);
|
|
ds.receive(dp);
|
|
byte[] data=dp.getData();
|
|
int len=dp.getLength();
|
|
InetAddress address=dp.getAddress();
|
|
int port=dp.getPort();
|
|
System.out.println("收到的数据为:"+new String(data,0, len));
|
|
System.out.println("从"+address+"电脑发出,长度为:"+len+"端口号为:"+port);
|
|
}
|
|
}
|