千家信息网

12.7-全栈Java笔记:Java网络编程(五)

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,UDP通讯的实现1.DatagramSocket:用于发送或接收数据包当服务器要向客户端发送数据时,需要在服务器端产生一个DatagramSocket对象,在客户端产生一个DatagramSocket
千家信息网最后更新 2024年09月22日12.7-全栈Java笔记:Java网络编程(五)

UDP通讯的实现

1.DatagramSocket:用于发送或接收数据包

当服务器要向客户端发送数据时,需要在服务器端产生一个DatagramSocket对象,在客户端产生一个DatagramSocket对象。服务器端的DatagramSocket将DatagramPacket发送到网络上,然后被客户端的DatagramSocket接收。

DatagramSocket有两种构造函数。一种是无需任何参数的,常用于客户端。另一种需要指定端口,常用于服务器。

常用方法:send、receive、 close

2.DatagramPacket:数据容器(封包)的作用

常用方法:构造函数、getAddrress(获取发送或接收方计算机的IP地址)、getData(获取发送或接收的数据)、setData(设置发送的数据)

3.UDP通信编程基本步骤:

a)创建客户端的DatagramSocket,创建时,定义客户端的监听端口

b)创建服务器端的DatagramSocket,创建时,定义服务器端的监听端口

c)在服务器端定义DatagramPacket对象,封装待发送的数据包。

d)服务器端将数据包发送出去

e)客户端接收数据包

【示例1】客户端与服务器端单向通信之客户端

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetSocketAddress;

public class Client {

public static void main(String[] args) throws Exception {

byte[] b = "aaaa".getBytes();

//必须告诉数据包要发到哪里去

DatagramPacket dp = new DatagramPacket(b,b.length,new InetSocketAddress("localhost",8999));

//我本身占用9000端口向外面机器发数据包

DatagramSocket ds = new DatagramSocket(9000);

ds.send(dp);

ds.close();

}

}

【示例2】客户端与服务器端单向通信之服务器端

import java.net.DatagramPacket;

import java.net.DatagramSocket;

public class Server {

public static void main(String[] args) throws Exception {

DatagramSocket ds = new DatagramSocket(8999);

byte[] b = new byte[1024];

DatagramPacket dp = new DatagramPacket(b,b.length);

ds.receive(dp); //阻塞式方法

String string = new String(dp.getData(),0,dp.getLength()); //dp.getLength()返回实际收到的数据的字节数

System.out.println(string);

ds.close();

}

}

通过ByteArrayInputStreamByteArrayOutputStream可以传递基本类型数据。

【示例3】客户端

public class Client {

public static void main(String[] args) throws Exception {

long n = 2000L;

ByteArrayOutputStream bos = new ByteArrayOutputStream();

DataOutputStream dos = new DataOutputStream(bos);

dos.writeLong(n);

byte[] b = bos.toByteArray();

//必须告诉数据包要发到哪里去

DatagramPacket dp = new DatagramPacket(b,b.length,new InetSocketAddress("localhost",8999));

//我本身占用9000端口向外面机器发数据包

DatagramSocket ds = new DatagramSocket(9000);

ds.send(dp);

ds.close();

}

}

【示例4】服务器端

public class Server {

public static void main(String[] args) throws Exception {

DatagramSocket ds = new DatagramSocket(8999);

byte[] b = new byte[1024];

DatagramPacket dp = new DatagramPacket(b,b.length);

ds.receive(dp); //阻塞式方法

ByteArrayInputStream bis = new ByteArrayInputStream(dp.getData());

DataInputStream dis = new DataInputStream(bis);

System.out.println(dis.readLong());

ds.close();

}

}

通过ByteArrayInputStreamByteArrayOutputStream可以传递对象。

【示例5Person(客户端与服务器端都需要存在Person)

class Person implements Serializable{

int age;

String name;

public Person(int age, String name) {

super();

this.age = age;

this.name = name;

}

}

【示例6】客户端

public class Client {

public static void main(String[] args) throws Exception {

Person person = new Person(20,"aa");

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(bos);

oos.writeObject(person);

byte[] b = bos.toByteArray();

//必须告诉数据包要发到哪里去

DatagramPacket dp = new DatagramPacket(b,b.length,new InetSocketAddress("localhost",8999));

//我本身占用9000端口向外面机器发数据包

DatagramSocket ds = new DatagramSocket(9000);

ds.send(dp);

ds.close();

}

}

【示例7】服务器端

public class Server {

public static void main(String[] args) throws Exception {

DatagramSocket ds = new DatagramSocket(8999);

byte[] b = new byte[1024];

DatagramPacket dp = new DatagramPacket(b,b.length);

ds.receive(dp); //阻塞式方法

ByteArrayInputStream bis = new ByteArrayInputStream(dp.getData());

ObjectInputStream ois = new ObjectInputStream(bis);

Person person = (Person) ois.readObject();

System.out.println(person.name);

ds.close();

}

}




「全栈Java笔记」是一部能帮大家从零到一成长为全栈Java工程师系列笔记。笔者江湖人称 Mr. G,10年Java研发经验,曾在神州数码、航天院某所研发中心从事软件设计及研发工作,从小白逐渐做到工程师、高级工程师、架构师。精通Java平台软件开发,精通JAVAEE,熟悉各种流行开发框架。


笔记包含从浅入深的六大部分:

A-Java入门阶段

B-数据库从入门到精通

C-手刃移动前端和Web前端

D-J2EE从了解到实战

E-Java高级框架精解

F-Linux和Hadoop



0