网络通信协议:速率,传输码率,代码结构,传输控制……
TCP/IP协议簇(实际上是一组协议)
- TCP:用户传输协议
- UDP:用户数据报协议
TCP与UDP对比:
TCP:
- 连接、稳定
三次握手
,四次挥手
最少需要三次,保证稳定链接 A:咋地! B:干啥? A:干一架 A:我要走了 B:你要走了吗 B:你真的要走了吗 A:走了
- 客户端、服务端
- 传输完成,释放连接。效率低
UDP:
不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
- DDOS(饱和攻击)
TCP
客户端:
1、连接服务器socket
2、发送消息
package com.network.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @Author suaxi
* @Date 2020/11/20 17:37
* 客户端
*/
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
//1、要知道服务器的地址、端口号
try {
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
int port = 8888;
//2、创建一个socke连接
socket = new Socket(serverIp, port);
//3、发送消息、IO流
os = socket.getOutputStream();
os.write("故乡的樱花开了".getBytes());
} catch (Exception 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();
}
}
}
}
}
服务器
1、建立服务端口ServerSocket
2、等待用户连接
3、接收客户端发送的消息(管道流)
package com.network.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author suaxi
* @Date 2020/11/20 17:38
* 服务端
*/
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、建立服务器地址
serverSocket = new ServerSocket(8888);
while (true){
//2、等待客户端连接
socket = serverSocket.accept();
//3、读取客户端消息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length=is.read(buffer))!=-1){
baos.write(buffer,0,length);
}
System.out.println(baos);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (baos!=null){
try {
baos.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.network.lesson02;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author suaxi
* @Date 2020/11/22 11:26
* 文件上传服务端
*/
public class TcpServerDemo02 {
public static void main(String[] args) throws IOException {
//1、创建服务
ServerSocket serverSocket = new ServerSocket(8889);
//2、监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//3、获取输入流
InputStream is = socket.getInputStream();
//4、文件输出
FileOutputStream fileOutputStream = new FileOutputStream("recive.jpg");
byte[] bytes = new byte[1024];
int length;
while ((length=is.read(bytes))!=-1){
fileOutputStream.write(bytes,0,length);
}
//通知客户端文件接收完毕
OutputStream os = socket.getOutputStream();
os.write("文件上传成功!".getBytes());
//5、关闭资源
fileOutputStream.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
package com.network.lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* @Author suaxi
* @Date 2020/11/22 11:26
* 文件上传客户端
*/
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//1、创建一个socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8889);
//2、创建一个输出流
OutputStream os = socket.getOutputStream();
//3、读取文件
FileInputStream fileInputStream = new FileInputStream(new File("test.jpg"));
//4、写出文件
byte[] bytes1 = new byte[1024];
int length1;
while((length1=fileInputStream.read(bytes1))!=-1){
os.write(bytes1,0,length1);
}
//通知服务器已经上传完毕
socket.shutdownOutput();
//确定服务器接收完毕,才断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes2 = new byte[1024];
int length2;
while ((length2=inputStream.read(bytes2))!=-1){
baos.write(bytes2,0,length2);
}
System.out.println(baos.toString());
//5、关闭资源
baos.close();
inputStream.close();
fileInputStream.close();
os.close();
socket.close();
}
}
评论 (0)