软件结构
网络通信协议分类
UDP:主机A发送数据不需要确认连接主机B就可以传输数据
优点:消耗资源小,通信效率高
缺点:由于UDP的面向无连接,故不能保证数据的完整性,偶尔可能会丢失一两个数据包
TCP:首先要建立发送和接收的链接后才可以传输数据
网络编程三要素
ip地址
端口号
TCP通信的概述
TCP通信的客户端代码实现(用的都是一个字节流)
先起服务端,在起客户端
客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws IOException {
//1.
Socket socket = new Socket("192.168.1.3",8888);
//2.
OutputStream outputStream = socket.getOutputStream();
//3.
outputStream.write("你好服务器".getBytes());
//4
InputStream inputStream = socket.getInputStream();
//5
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
System.out.println(new String(bytes,0,len));
//6
socket.close();
}
}
服务端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException {
//1.
ServerSocket serverSocket = new ServerSocket(8888);
//2.
Socket accept = serverSocket.accept();
//3.
InputStream inputStream = accept.getInputStream();
//4.
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
System.out.println(new String((bytes),0,len));
System.out.println("读取个数"+len);
//5.
OutputStream outputStream = accept.getOutputStream();
outputStream.write("收到谢谢".getBytes());
//6.
serverSocket.close();
accept.close();
}
}
案例:文件上传
读取,就是接收信息,就是输入流->读入
往哪写就是输出信息,就是输出流->写出
说白了就是,先通过本地的流我们把文件读出来,通过socket,我们写出去,在通过socket我们在把文件读下来,在通过本地的流我们写下来
先起服务端,再起客户端
服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class TCPServer {
public static void main(String[] args) throws IOException {
//1.创建一个服务器对象
ServerSocket serverSocket = new ServerSocket(9999);
//让服务器一直处于监听状态
while (true){
//2.获取客户端对象
Socket socket = serverSocket.accept();
//当多个客户端同时上传文件时,我们可以采用多线程来提高效率
new Thread(()-> { //lambda表达式
try {
//判断文件夹是否存在
File file = new File("D:\\upload");
if (!file.exists()){
file.mkdir();
}
//自定义命名规则,防止同名文件被覆盖
String fileName = "itcast"+System.currentTimeMillis()+new Random().nextInt(9999)+".png";
//5.创建本地的写出流
FileOutputStream fileOutputStream = new FileOutputStream(file+"\\"+fileName);
//6一读一些
int len = 0;
byte[] bytes = new byte[1024];
InputStream inputStream = socket.getInputStream();
while ((len=inputStream.read(bytes))!=-1){//网络的读入
fileOutputStream.write(bytes,0,len);//用本地的写出
}
//7.用网络的写出,返回给客户端一个信息
socket.getOutputStream().write("上传成功+++".getBytes());
//8.释放资源
fileOutputStream.close();
socket.close();
}catch (IOException e){
System.out.println(e);
}
}).start();
//3.通过socket读取入客户端传来的数据
}
//服务器就不用关闭了
//serverSocket.close();
}
}
客户端
import java.io.*;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("C:\\1.png");
Socket socket = new Socket("192.168.1.2",9999);
OutputStream outputStream = socket.getOutputStream();
//文件先读出来
int len = 0;
byte[] bytes = new byte[1024];
while ((len=fileInputStream.read(bytes))!=-1){
//通过socket写出去->传输到服务器端
outputStream.write(bytes,0,len);
}
socket.shutdownOutput();
//通过socket读取->服务器返回的数据
InputStream inputStream = socket.getInputStream();
while ((len=inputStream.read())!=-1){
System.out.println(new String(bytes,0,len));
}
//释放资源
fileInputStream.close();
socket.close();
}
}