TCP通信
需要先创建连接 - 并且在创建连接的过程中 需要经过三次握手
底层通过 流 发送数据 数据没有大小限制
可靠的传输机制 - 丢包重发 包的顺序的保障
传输速度相对比较慢
对于速度要求不太高 但是对可靠性要求比较高的场景下 - 文件上传 下载
因为TCP在通信的过程中 是需要创建连接的 连接的发起者称为客户端 监听端口等待被连接的一端称为服务端
1、客户端:Client.java
package ChatRoom.v3; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner; /**
* @author 赵瑞鑫 E-mail:1922250303@qq.com
* @version 1.0
* @创建时间:2020年8月3日 下午4:38:50
* @类说明: 服务端
*/
public class Client {
private static Socket socket;
private static String cli = "客户端:";
private static OutputStream out;
private static InputStream in;
private static PrintWriter pw;
private static BufferedReader br; public Client() {
try {
socket = new Socket("127.0.0.1", 8888);
System.out.println(cli + "套接字创建成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void start() {
// 1、套接字对象
try { while (true) {
// 2、获取输出流,写信息
out = socket.getOutputStream(); // 3、获取输入流,读信息
in = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
while (true) {
System.out.println("input:");
Scanner sc = new Scanner(System.in);
String msg = sc.next();
pw = new PrintWriter(out);
pw.println(msg);
pw.flush();
msg = br.readLine();
System.out.println(msg + "\n"); }
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 4、关闭流
try {
socket.close();
out.close();
pw.close();
in.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public static void main(String[] args) {
// TODO Auto-generated method stub
Client client = new Client();
client.start();
} }
2、服务端:Server.java
package ChatRoom.v3; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket; /**
* @author 赵瑞鑫 E-mail:1922250303@qq.com
* @version 1.0
* @创建时间:2020年8月3日 下午4:38:36
* @类说明: 客户端 端口号:8888
*/
public class Server {
private static ServerSocket serversocket;
private static Socket socket;
private static String ser = "服务端:";
private static OutputStream out;
private static InputStream in;
private static PrintWriter pw;
private static BufferedReader br; public Server() {
try {
serversocket = new ServerSocket(8888);
System.out.println(ser + "套接字创建成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public static void start() {
// 1、套接字对象
try { while (true) {
// 2、调用accept方法,接收请求
socket = serversocket.accept();
System.out.println(ser + "已接收请求,等待处理");
// 3、获取输入流对象,读客户端数据
in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while (true) {
String msg = br.readLine();
System.out.println(ser + "处理成功,请等待");
// 4、处理后的数据通过输出流写出去
out = socket.getOutputStream();
pw = new PrintWriter(out);
pw.println(ser + msg);
pw.flush();
} }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 5、关闭流和套接字对象
try {
serversocket.close();
br.close();
pw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public static void main(String[] args) {
// TODO Auto-generated method stub
Server server = new Server();
server.start();
} }
实现客户端与服务端的通信是后续实现多人聊天室的基础,理清socket编程和输入输出流的使用非常重要。