/*
时间:2015年3月15日09:58:53
测试的UDP的连接和传输信息
DatagramPacket
此类表示数据报包。
数据报包用来实现无连接包投递服务。
每条报文仅根据该包中包含的信息从一台机器路由到另一台机器
。从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序
到达。不对包投递做出保证。
构造方法有两大类:
DatagramPacket(byte[] buf, int length);构造DatagramPacket,用来接受长度为length的数据包
DatagramPacket(byte[] buf, int length, InetAddress address, int port);
构造数据报包,用来将长度为length的包发送到指定主机上的指定端口号。
DatagramPacket(byte[], int length, SocketAddress address);
构造数据报包,用来将长度为length的包发送到指定主机上的指定端口号。
SocketAddress的直接子类为InetSocketAddress
InetSocketAddress(String hostname, int port)
根据主机名和端口号创建套接字地址。
DatagramSocket s = new DatagramSocket(null);
s.bind(new InetSocketAddress(8888));
这等价于:
DatagramSocket s = new DatagramSocket(8888);
两个例子都能创建能够在 UDP 8888 端口上接收广播的
DatagramSocket。
public void receive(DatagramPacket p) throws IOException;
从此套接字接收数据报包。
当此方法返回时,DatagramPacket 的缓冲区填充了接收的数据。
数据报包也包含发送方的 IP 地址和发送方机器上的端口号。
此方法在接收到数据报前一直阻塞。
数据报包对象的 length 字段包含所接收信息的长度。
如果信息比包的长度长,该信息将被截短。
时间:2015年3月15日09:58:53
测试的UDP的连接和传输信息
DatagramPacket
此类表示数据报包。
数据报包用来实现无连接包投递服务。
每条报文仅根据该包中包含的信息从一台机器路由到另一台机器
。从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序
到达。不对包投递做出保证。
构造方法有两大类:
DatagramPacket(byte[] buf, int length);构造DatagramPacket,用来接受长度为length的数据包
DatagramPacket(byte[] buf, int length, InetAddress address, int port);
构造数据报包,用来将长度为length的包发送到指定主机上的指定端口号。
DatagramPacket(byte[], int length, SocketAddress address);
构造数据报包,用来将长度为length的包发送到指定主机上的指定端口号。
SocketAddress的直接子类为InetSocketAddress
InetSocketAddress(String hostname, int port)
根据主机名和端口号创建套接字地址。
DatagramSocket s = new DatagramSocket(null);
s.bind(new InetSocketAddress(8888));
这等价于:
DatagramSocket s = new DatagramSocket(8888);
两个例子都能创建能够在 UDP 8888 端口上接收广播的
DatagramSocket。
public void receive(DatagramPacket p) throws IOException;
从此套接字接收数据报包。
当此方法返回时,DatagramPacket 的缓冲区填充了接收的数据。
数据报包也包含发送方的 IP 地址和发送方机器上的端口号。
此方法在接收到数据报前一直阻塞。
数据报包对象的 length 字段包含所接收信息的长度。
如果信息比包的长度长,该信息将被截短。
*/
import java.io.*;
import java.net.*;
import java.net.*;
public class TestUDPServer {
public static void main(String[] args) {
byte buf[] = new byte[1024];
DatagramPacket dp = null;
DatagramSocket ds = null;
ByteArrayInputStream bis = null;
DataInputStream dis = null;
try {
dp = new DatagramPacket(buf, buf.length);
ds = new DatagramSocket(7788);//创建数据报套接字,并将其绑定到本地主机的指定端口
ds.receive(dp);
bis = new ByteArrayInputStream(buf);
dis = new DataInputStream(bis);
System.out.println(ds.getInetAddress());//返回此套接字连接的地址
System.out.println(ds.getLocalAddress());//返回此套接字绑定的本地地址
System.out.println(ds.getLocalPort());//返回此套接字绑定的本地主机上的端口号
System.out.println("此套接字的端口 " + ds.getPort());
System.out.println("此套接字的地址" + ds.getInetAddress());
System.out.println("此套接字连接的远程端点的地址: " + ds.getRemoteSocketAddress());
System.out.println("此套接字绑定的端点的本地地址 " + ds.getLocalSocketAddress());
System.out.println(dis.readLong());
dis.close();
bis.close();
public static void main(String[] args) {
byte buf[] = new byte[1024];
DatagramPacket dp = null;
DatagramSocket ds = null;
ByteArrayInputStream bis = null;
DataInputStream dis = null;
try {
dp = new DatagramPacket(buf, buf.length);
ds = new DatagramSocket(7788);//创建数据报套接字,并将其绑定到本地主机的指定端口
ds.receive(dp);
bis = new ByteArrayInputStream(buf);
dis = new DataInputStream(bis);
System.out.println(ds.getInetAddress());//返回此套接字连接的地址
System.out.println(ds.getLocalAddress());//返回此套接字绑定的本地地址
System.out.println(ds.getLocalPort());//返回此套接字绑定的本地主机上的端口号
System.out.println("此套接字的端口 " + ds.getPort());
System.out.println("此套接字的地址" + ds.getInetAddress());
System.out.println("此套接字连接的远程端点的地址: " + ds.getRemoteSocketAddress());
System.out.println("此套接字绑定的端点的本地地址 " + ds.getLocalSocketAddress());
System.out.println(dis.readLong());
dis.close();
bis.close();
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
D:\java\socket>javac TestUDPServer.java
D:\java\socket>java TestUDPServer
null
0.0.0.0/0.0.0.0
7788
此套接字的端口 -1
此套接字的地址null
此套接字连接的远程端点的地址: null
此套接字绑定的端点的本地地址 0.0.0.0/0.0.0.0:7788
10000
*/
null
0.0.0.0/0.0.0.0
7788
此套接字的端口 -1
此套接字的地址null
此套接字连接的远程端点的地址: null
此套接字绑定的端点的本地地址 0.0.0.0/0.0.0.0:7788
10000
*/
/*
时间:2015年3月15日10:34:53
测试UDP
public void send(DatagramPacket p)
throws IOException从此套接字发送数据报包。
DatagramPacket 包含的信息指示:
将要发送的数据、其长度、远程主机的 IP 地址和远程主机的端口号。
*/
import java.net.*;
import java.io.*;
时间:2015年3月15日10:34:53
测试UDP
public void send(DatagramPacket p)
throws IOException从此套接字发送数据报包。
DatagramPacket 包含的信息指示:
将要发送的数据、其长度、远程主机的 IP 地址和远程主机的端口号。
*/
import java.net.*;
import java.io.*;
public class TestUDPClient {
public static void main(String[] args) {
long l = 10000L;
ByteArrayOutputStream bos = null;
DataOutputStream dos = null;
DatagramPacket dp = null;
DatagramSocket ds = null;
try {
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
dos.writeLong(l);
byte[] b = bos.toByteArray();
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 7788);
dp = new DatagramPacket(b, b.length, isa);
System.out.println("InetSocketAddresss");
System.out.println("=====================");
System.out.println(isa.getAddress());
System.out.println(isa.getHostName());
System.out.println(isa.getPort());
System.out.println(isa.toString());
System.out.println("=====================");
System.out.println(b.length);
System.out.println(dp.getLength());
System.out.println("远端端口号: " + dp.getPort());
ds = new DatagramSocket(5678);
System.out.println(ds.getLocalPort());//返回此套接字绑定的本地主机上的端口号
ds.send(dp);//从此套接字发送数据报包
ds.close();
bos.close();
bos.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
public static void main(String[] args) {
long l = 10000L;
ByteArrayOutputStream bos = null;
DataOutputStream dos = null;
DatagramPacket dp = null;
DatagramSocket ds = null;
try {
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
dos.writeLong(l);
byte[] b = bos.toByteArray();
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 7788);
dp = new DatagramPacket(b, b.length, isa);
System.out.println("InetSocketAddresss");
System.out.println("=====================");
System.out.println(isa.getAddress());
System.out.println(isa.getHostName());
System.out.println(isa.getPort());
System.out.println(isa.toString());
System.out.println("=====================");
System.out.println(b.length);
System.out.println(dp.getLength());
System.out.println("远端端口号: " + dp.getPort());
ds = new DatagramSocket(5678);
System.out.println(ds.getLocalPort());//返回此套接字绑定的本地主机上的端口号
ds.send(dp);//从此套接字发送数据报包
ds.close();
bos.close();
bos.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
D:\java\socket>javac TestUDPClient.java
D:\java\socket>java TestUDPClient
InetSocketAddresss
=====================
/127.0.0.1
127.0.0.1
7788
127.0.0.1/127.0.0.1:7788
=====================
8
8
远端端口号: 7788
5678
InetSocketAddresss
=====================
/127.0.0.1
127.0.0.1
7788
127.0.0.1/127.0.0.1:7788
=====================
8
8
远端端口号: 7788
5678
*/