通信协议
协议:约定,就好比我们说的语言一样。
网络通信协议:速率,传输码率,代码结构,传输控制…
问题:非常复杂
TCP/IP协议簇
- TCP:用户传输协议
- UDP:用户数据报协议
- TCP/IP,实际上是一组协议,网络互联协议。
TCP UDP对比
TCP:比作打电话
- 相连接,稳定
- 三次握手,四次挥手
//三次握手,最少三次稳定链接
A:你瞅啥?B:瞅你咋地?A:干一架(A发送请求,B回应请求,A接受B的回应并进行反应或者进行下一步)
//四次挥手
A:我要断开了B:你真的要断开了吗B:你确定真的要断开了吗A:我真的要断开了(A发送断开请求,B接受请求并发送确认请求,B发送确认请求,A回复确认请求,完成断开)
- 客户端,服务端
- 传输完成,释放链接,效率低
UDP:比作发短信
-
不连接,不稳定
-
客户端,服务的没有明确的界限
-
不管有没有准备好,都会发送
-
DDOS:洪水攻击!(饱和攻击)
TCP
客户端
- 链接服务器,通过Socket
- 发送消息
package com.lean.lession01;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) throws IOException {
Socket socket =null;
OutputStream os =null;
//1.要知道服务器的地址
try {
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
//2. 端口号
int port = 9999;
//3.创建一个socket连接
socket = new Socket(serverIp,port);
//3.发送消息IO流
os = socket.getOutputStream();
os.write("Hello,World".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
}finally {
//关闭资源
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器
- 建立服务端口 ServerSocket
- 等待用户的连接 accecpt
- 接受用户的消息
package com.lean.lession01;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器
public abstract class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket =null;
Socket socket =null;
InputStream is =null;
ByteArrayOutputStream bas =null;
try {
//1.我得有一个地址
serverSocket = new ServerSocket(9999);
while(true){
//2.等待客户端链接过来
socket = serverSocket.accept();
//读取客户端的信息
is = socket.getInputStream();
/*
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer))!=-1){
String msg = new String(buffer,0,len);
System.out.println(msg);
}
*/
//管道流
bas = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
bas.write(buffer,0,len);
}
System.out.println(bas.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if (bas!=null) {
try {
bas.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件上传
package com.lean.lession01;
//客户端
import javax.sound.midi.Soundbank;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9900);
//创建一个输出流
OutputStream os = socket.getOutputStream();
//文件流 读取文件
FileInputStream fis = new FileInputStream(new File("food.png"));
//写出文件
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//通知服务器,我已经结束了
socket.shutdownOutput();//我已经传输完了
//确定服务器接受完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[2048];
int len2;
while ((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
//关闭资源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
package com.lean.lession01;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器
public class TcpServerDemo02 {
public static void main(String[] args)throws Exception {
//创建服务
ServerSocket serverSocket = new ServerSocket(9900);
//监听用户进程
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//获取输入流
InputStream is = socket.getInputStream();
//文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.png"));
byte[] buffer =new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//通知客户端接受完毕
OutputStream os = socket.getOutputStream();
os.write("接收完毕".getBytes());
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
UDP
不用链接,但是需要知道对方的地址
package com.lean.lession03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
//不需要连接服务器
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//建立一个Socket
DatagramSocket socket = new DatagramSocket();
//建个包
String msg = "Hello,World!";
//发送给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length, localhost, port);
//发送包
socket.send(packet);
//关闭资源
socket.close();
}
}
package com.lean.lession03;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
public class UdpServerDemo {
public static void main(String[] args) throws IOException {
//开放端口
DatagramSocket datagramSocket = new DatagramSocket(9090);
//接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
datagramSocket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0, packet.getLength()));
//关闭连接
datagramSocket.close();
}
}