Java Network Programming TCP protocol for data transmission

A, TCP Protocol Overview

TCP protocol is relatively stable with respect to UDP transport protocol, there is a three-way handshake here, to ensure that the connection status, and have a clear client and server division.
TCP service requires the server to start, need to listen to the specified port, waiting for client connections.
After the client actively connect to the server, and the server connection, can exchange data, the server can not take the initiative to connect the client.

For TCP operation, Java provides two Socket

1. 服务端Socket 
	java.net.ServerSocket;
	创建对应的ServerScoket开启服务器,等待客户端连接
2. 客户端Socket
	java.net.Socket
	创建客户端Scoket,并且连接服务器,同时将Socket发送给服务器绑定注册。

Two, Socket client

Provide data transmission to the client Socket object consistent with TCP / IP required

1, the constructor Constructor
Socket(String host, int port);
	host是服务器IP地址,port对应服务器程序的端口号
	通过指定的服务器IP地址和端口号,获取TCP连接对象
2, members of the method Method
InputStream getInputStream();
	获取Socket对象输入字节流,可以从服务器获取对应的数据
	InputStream是一个资源,需要在程序退出时关闭
	
OutputStream getOutputStream();
	获取Sokcet对象输出字节流,可以发送数据到服务器
	OutputStream是一个资源,需要在程序退出时关闭

void close();
	关闭客户端Socket

void shutdownOutput();
	禁止当前Socket发送数据

TCP / IP protocol corresponds Socket is implemented based on the IO stream.

Three, ServerSocket server

Open Socket Server on the server

1, the constructor Constructor
ServerSocket(int port);
	开启ServerSocket服务器,并且明确当前的服务端口
2, members of the method Method
Socket accept();
	监听并且连接,得到一个Socket对象,同时该方法是一个阻塞方法,会处于一个始终
	的监听状态。
	返回的是Socket,也就是客户端Socket对象,获取到当前Socket对象,相对于获取到
	客户端连接,同时使用的Socket和客户端一致。

Four, TCP protocol code demonstrates

1, the server process
1. 创建ServerSocket服务器,同时监听指定端口
2. 通过accept方法获取Socket连接,得到客户端Socket对象
3. 通过Socket对象,获取InputStream,读取客户端发送数据
4. 通过Socket对象,获取OutputStream,发送数据给客户端
5. 关闭服务
public class TcpServer {
	public static void main(String[] args) throws IOException {
		System.out.println("服务器已启动");
		// 1. 创建ServerSocket服务器,同时监听指定端口
		ServerSocket serverSocket = new ServerSocket(6666);
	
		// 2. 通过accept方法获取Socket连接,得到客户端Socket对象
		Socket socket = serverSocket.accept();
		
		// 3. 通过Socket对象,获取InputStream,读取客户端发送数据
		InputStream inputStream = socket.getInputStream();
		
		// IO流操作
		byte[] buf = new byte[1024];
		int length = inputStream.read(buf);
		System.out.println(new String(buf, 0, length));
        
		// 4. 通过Socket对象,获取OutputStream,发送数据给客户端
		OutputStream outputStream = socket.getOutputStream();
		String str = "这是来自服务器端的数据";
		
		outputStream.write(str.getBytes());
		
		// 5. 关闭Socket服务 同时关闭当前Socket使用的输入字节流和输出字节流	
		socket.close();	
	}
}
2, the client process
1. 创建Socket服务,同时明确连接服务器的IP地址和对应端口号
2. 通过Socket对象,获取对应的OutputStream对象,发送数据给服务器
3. 通过Socket对象,获取对应的InputStream对象,接收服务器发送数据
4. 关闭服务
public class TcpClient {
	public static void main(String[] args) 
		throws UnknownHostException, IOException {
		System.out.println("客户端已启动");
		
		// 1. 创建Socket服务,同时明确连接服务器的IP地址和对应端口号
		Socket socket = new Socket("192.168.246.13", 6666);
		
		// 2. 通过Socket对象,获取对应的OutputStream对象,发送数据给服务器
		OutputStream outputStream = socket.getOutputStream();
		
		outputStream.write("服务器,你好!".getBytes());
		
		// 3. 通过Socket对象,获取对应的InputStream对象,接收服务器发送数据
		InputStream inputStream = socket.getInputStream();
		
		byte[] buf = new byte[1024];
		int length = inputStream.read(buf);
		System.out.println(new String(buf, 0, length));
		
		// 4. 关闭服务
		socket.close();
	}
}

Fifth, the implementation file upload

1, process analysis

File upload flow chart

2, the client program
1. 创建对应文件的输入字节流操作,这里可以使用缓冲
2. 启动Socket,
3. 获取Socket输出OutputStream对象,发送数据给服务器
4. 边读边发
5. 当文件读取结束,发送完毕,关闭客户端
public class TcpClient {
	public static void main(String[] args) throws IOException {
		// 1. 创建对应文件的输入字节流操作,这里可以使用缓冲
		BufferedInputStream bis = new BufferedInputStream(
				new FileInputStream(new File("D:/1.mp4")));
		
		// 2. 启动Socket
		Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 6666);
		
		// 3. 获取Socket输出OutputStream对象,发送数据给服务器
		OutputStream outputStream = socket.getOutputStream();
		
		int length = -1;
		byte[] buf = new byte[1024 * 8];
		
		
		// 4. 读取数据,发送数据
		while ((length = bis.read(buf)) != -1) {
			outputStream.write(buf, 0, length);
		}
		
		// 5. 关闭资源
		socket.close();
		bis.close();
	}
}
3, server program
1. 开启服务端服务,创建ServerSocket对象
2. 明确保存文件的位置,创建对应文件夹的输出缓冲字节流
3. 读取数据,写入文件
4. 关闭服务器
public class TcpServer {
	public static void main(String[] args) throws IOException {
		// 1. 开启服务端服务,创建ServerSocket对象
		ServerSocket serverSocket = new ServerSocket(6666);
		
		Socket socket = serverSocket.accept();
		
		// 2. 明确保存文件的位置,创建对应文件夹的输出缓冲字节流
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(
						new File("D:/temp.mp4")));
		
		// 3. 获取Socket对应的输入流
		InputStream inputStream = socket.getInputStream();
		
		// 4. 边读边写
		int length = -1;
		byte[] buf = new byte[1024 * 8];
		
		while ((length = inputStream.read(buf)) != -1) {
			bos.write(buf, 0, length);
		}
		
		// 5. 关闭资源
		bos.close();
		socket.close();
	}
}
Published 14 original articles · won praise 27 · views 9516

Guess you like

Origin blog.csdn.net/Betterman_QS/article/details/104678731