网络编程:
网络:
网络的分类:
广域网 城域网 局域网
网络层: IP协议
传输层: TCP/UDP协议
应用层: http
TCP 长连接 http短连接
TCP: 面向连接 ,可靠的传输层协议, 传输数据的大小无限制,不安全
UDP: 面向无连接的, 不可靠的传输层协议, 传输数据的大小是不能超过64kb, 安全
IP: 给网络中每一台设备,设定的唯一标识
255.255.255.255 4个字节 32 位
基于TCP网络编程:
客户端(发送,接受回复) 服务端(接受和回复)
Socket ServerSocekt
基于字节流实现的 写socket.getOutputStream
读:socket.getInputStream
accept() 接受客户端,有阻塞功能
1.创建Socket对象 1.创建ServerSocket对象
2. 准备数据 2. accept() 接受客户端
3. 获取输出流 3. 获取输入流对象
4. 将数据通过输出发送出去 4. 读取数据
5. 关闭流和socket 5. 关闭流和socket
基于UDP网络编程
发送方 接受方
DatagramSocket DatagramSocket
DatagramPacket DatagramPacket
1. DatagramSocket不需要添加参数 1. DatagramSocket(端口号)
2. DatagramPacket 5个参数 2. receive() 接受,需要准备一个空包裹
3. socket.send(packet) 3. 通过packet.getData(). getOffset() getLength
4. 关闭socket 4. 闭关socket
UDP
接收信息
Recieve
1. 接收方的快递点
2. 接受
3. 从仓库中读取数据
3. 关闭socket
注意:
发送方的端口号是默认提供的, 与接收方法的端口号一定不同
回复数据的时候 一定要用 p.getPort()
public class Recieve {
public static void main(String[] args) throws IOException {
System.out.println("接收方启动了");
DatagramSocket socket = new DatagramSocket(7777);
byte[] bs = new byte[1024];
DatagramPacket packet = new DatagramPacket(bs,bs.length);
socket.receive(packet);
String str = new String(packet.getData(),packet.getOffset(),packet.getLength());
System.out.println("发送方发送过来的数据:"+str);
System.out.println(packet.getAddress());
System.out.println(packet.getPort());
DatagramPacket p = new DatagramPacket("不开心".getBytes(), 0, "不开心".getBytes().length, packet.getAddress(), packet.getPort());
socket.send(p);
socket.close();
}
}
Send
. 发送方法
快递点
要发送的数据
将数据打包
发送
关闭
public class Send {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "开心";
DatagramPacket packet = new DatagramPacket(str.getBytes(), 0, str.getBytes().length, InetAddress.getByName("127.0.0.1"), 7777);
socket.send(packet);
byte[] bs = new byte[1024];
DatagramPacket p = new DatagramPacket(bs,bs.length);
socket.receive(p);
String str2 = new String(p.getData(),p.getOffset(),p.getLength());
System.out.println("接收方回复数:"+str2);
socket.close();
}
}
循环接收信息
Recieve
public class Recieve {
public static void main(String[] args) throws IOException {
System.out.println("====接收方启动====");
DatagramSocket socket = new DatagramSocket(5858);
Scanner sc = new Scanner(System.in);
byte[] bs = new byte[1024];
while(true) {
DatagramPacket packet = new DatagramPacket(bs, bs.length);
socket.receive(packet);
String str = new String(packet.getData(),0,packet.getLength());
System.out.println("发送方发送过来的数据:"+str);
if(str.equals("再见")) {
break;
}
String str2 = sc.next();
DatagramPacket packet2 = new DatagramPacket(str2.getBytes(),str2.getBytes().length,packet.getAddress(),packet.getPort());
socket.send(packet2);
if(str2.equals("再见")) {
break;
}
}
socket.close();
}
}
Send
public class Send {
public static void main(String[] args) throws IOException {
System.out.println("====发送方启动=====");
DatagramSocket socket = new DatagramSocket();
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.next();
DatagramPacket packet = new DatagramPacket(str.getBytes(), str.getBytes().length,
InetAddress.getByName("127.0.0.1"), 5858);
socket.send(packet);
if(str.equals("再见")) {
break;
}
byte[] bs = new byte[1024];
DatagramPacket packet2 = new DatagramPacket(bs, bs.length);
socket.receive(packet2);
String str2 = new String(packet2.getData(), 0, packet2.getLength());
System.out.println("接收方回复数据" + str2);
if (str2.equals("再见")) {
break;
}
}
socket.close();
}
}
TCP
循环接收信息
Client
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("192.168.43.132",7777);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("请输入要发送给服务端的信息");
String str = sc.next();
os.write(str.getBytes());
os.flush();
byte[] bs = new byte[1024];
int num = is.read(bs);
String str2 = new String(bs,0,num);
System.out.println("服务端给的回复:"+str2);
}
}
}
Server
public class Server {
public static void main(String[] args) throws IOException {
System.out.println("服务端启动了");
ServerSocket serversocket = new ServerSocket(7777);
Socket accept = serversocket.accept();
InputStream is = accept.getInputStream();
OutputStream os = accept.getOutputStream();
Scanner sc = new Scanner(System.in);
while(true) {
byte[] bs = new byte[1024];
int num = is.read(bs);
String str = new String(bs,0,num);
System.out.println("客户端发送过来的信息是:"+str);
System.out.println("请输入给客户端的回复");
String str2 = sc.next();
os.write(str2.getBytes());
os.flush();
}
}
}