UDP使用的整理
Java使用DatagramSocket代表UDP协议的Socket,DatagramSocket本身只是码头,不维护状态,不能产生IO流,它的唯一作用就是接收和发送数据报,Java使用DatagramPacket来代表数据报,DatagramSocket接收和发送的数据都是通过DatagramPacket对象完成的。 (图都挂了,下次再补。。。。)
1.DatagramSocket的使用
DatagramSocket socket = new DatagramSocket();
DatagramSocket socket = new DatagramSocket(int port);
DatagramSocket socket = new DatagramSocket(SocketAddress address);
DatagramSocket socket = new DatagramSocket(int port,InetAddress address);
socket.send(DatagramPacket packet);
socket.receive(DatagramPacket packet);
PS:SocketAddress是一个抽象类用来封装IP和端口(及InetAddress和Port)
2.DatagramPacket的使用
DatagramPacket packet = new DatagramPacket(byte[] buf,int length);
DatagramPacket packet = new DatagramPacket(byte[] buf,int offset,int length);
DatagramPacket packet = new DatagramPacket(byte[] buf,int length,SocketAddress adress);
DatagramPacket packet = new DatagramPacket(byte[] buf,int length,InetAddress address,int port);
DatagramPacket packet = new DatagramPacket(byte[] buf,int offset,int length,SocketAddress adress);
DatagramPacket packet = new DatagramPacket(byte[] buf,int offset,int length,InetAddress address,int port);
packet.setData(byte[] bytes);
packet.getData();
3.DatagramSocket和DatagramPacket的配合使用,发送信息(单向)
客户端:发送信息
public class Client{
public static void main(String[] args){
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(new byte[1024*8],1024*8);
packet.setPort(9090);
packet.setInetAddress(InteAddress.getLoaclHost());
packet.setData("你好,服务器".getBytes(),0,"你好,服务器".getBytes().length);
socket.send(packet);
socket.close()
}
}
服务端:接受信息
public class Server{
public static void main(String[] args){
DatagramSocket socket = new DatagramSocket(9090);
DatagramPacket packet = new DatagramPacket(new bytes[1024*8],1024*8);
socket.receive(packet);
System.out.println(new String(packet.getData()));
socket.close();
}
}
先运行服务端再运行客户端,则可以看到输出结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5MM7Sjtc-1630306141872)(C:\Users\ASUS\Pictures\pic.png)]
4.DatagramSocket和DatagramPacket的配合使用:发送信息(双向)
客户端:Client
public class Client{
public static void main(String[] args){
DatagramSocket socket = new DatagramSocket(6666);
DatagramPacket packet = new DatagramPacket(new byte[1024*8],1024*8);
packet.setPort(9090);
packet.setInetAddress(InteAddress.getLoaclHost());
packet.setData("你好,服务器".getBytes(),0,"你好,服务器".getBytes().length);
socket.send(packet);
socket.receive(packet);
System.out.println(new String(packet.getData()));
socket.close()
}
}
服务端:Server
public class Server {
public static void main(String[] args) throws IOException{
DatagramSocket socket = new DatagramSocket(9090);
DatagramPacket packet = new DatagramPacket(new byte[1024*8],1024*8);
socket.receive(packet);
System.out.println(new String(packet.getData()));
packet.setPort(6666);
packet.setData("你好,客户端。".getBytes(),0,"你好,客户端。".getBytes().length);
socket.send(packet);
socket.close();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4MgKBoFQ-1630306141875)(C:\Users\ASUS\Pictures\pic1.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jyxtt7WR-1630306141877)(C:\Users\ASUS\Pictures\pic2.png)]
5.UDP和Swing的结合实现客户端和服务端的信息传递
服务端:Receiver
public class Receiver extends JFrame implements ActionListener,Runnable{
private Container con;
private JTextField input;
private JTextArea showMesseage;
private JButton SendMes;
private DatagramSocket client = new DatagramSocket(7845);
public Receiver() throws IOException{
this.setTitle("接受方");
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setBounds(400, 300, 400, 300);
this.setLayout(null);
con = this.getContentPane();
input = new JTextField();
showMesseage = new JTextArea();
SendMes = new JButton("发送");
con.add(SendMes);con.add(input);con.add(showMesseage);
SendMes.setBounds(0, 0, 400, 40);
showMesseage.setBounds(0, 40, 400, 200);
showMesseage.disable();
input.setBounds(0, 240, 400, 30);
SendMes.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
byte[] bytes = new byte[1024*8];
try {
DatagramPacket packet = new DatagramPacket(bytes, 1024*8);
packet.setAddress(InetAddress.getByName("localhost"));
packet.setPort(11001);
packet.setData(input.getText().getBytes(),0,input.getText().getBytes().length);
client.send(packet);
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Override
public void run() {
try {
while(true) {
byte[] bytes = new byte[1024*8];
DatagramPacket packet = new DatagramPacket(bytes, 8*1024);
client.receive(packet);
String ss = packet.getAddress().getHostName()+ new String(packet.getData())+'\n';
System.out.println(ss);
showMesseage.append(ss);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
new Thread(new Receiver()).start();
}
}
客户端:UDPtest
public class UDPtest extends JFrame implements ActionListener,Runnable{
private Container con;
private JTextField input;
private JTextArea showMesseage;
private JButton SendMes;
private DatagramSocket server = new DatagramSocket(11001);
public UDPtest() throws SocketException{
this.setTitle("发送方");
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setBounds(400, 300, 400, 300);
this.setLayout(null);
con = this.getContentPane();
input = new JTextField();
showMesseage = new JTextArea();
SendMes = new JButton("发送");
con.add(SendMes);con.add(input);con.add(showMesseage);
SendMes.setBounds(0, 0, 400, 40);
showMesseage.setBounds(0, 40, 400, 200);
showMesseage.disable();
input.setBounds(0, 240, 400, 30);
SendMes.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
DatagramPacket packet = new DatagramPacket(new byte[1024*8], 8*1024);
packet.setData(input.getText().getBytes());
packet.setPort(7845);
packet.setAddress(InetAddress.getLocalHost());
server.send(packet);
System.out.println(new String(packet.getData()));
} catch (SocketException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Override
public void run() {
while(true) {
try {
DatagramPacket packet = new DatagramPacket(new byte[1024*8], 8*1024);
server.receive(packet);
String data = packet.getAddress().getHostName()+ new String(packet.getData())+'\n';
showMesseage.append(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SocketException {
new Thread(new UDPtest()).start();
}
}
h (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
}
public static void main(String[] args) throws SocketException {
//UDPtest ui = new UDPtest();
new Thread(new UDPtest()).start();
}
} /* 2021/7/21 23:00*/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7bX2c8Rp-1630306141880)(C:\Users\ASUS\Pictures\pic3.png)]
|