标签:flush padding 要求 补充 sts 实现 new address lua
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。建立网络通信连接至少要一对端口号(socket)。socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口;HTTP是轿车,提供了封装或者显示数据的具体形式;Socket是发动机,提供了网络通信的能力。
Java Socket的通信时序图如下。
Java Socket的数据通信模型如下。
?
Server端 | Client端 |
|
|
public class TestServer { ????public static final String datePattern = "yyyy-MM-dd HH:mm:ss SSS"; ????public static final SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern); ???? ????public static void main(String[] args) throws IOException ????{ ????????//1.创建一个server socket服务 ????????ServerSocket serverSocket = new ServerSocket(); ????????//2.绑定端口 ????????InetSocketAddress address = new InetSocketAddress("localhost", 18824); ????????serverSocket.bind(address); ????????//3.等待和接收端口的通信请求,返回的是一个socket ????????PrintConsoleMsg("等待连接..."); ????????Socket socket = serverSocket.accept(); ????????PrintConsoleMsg("连接成功!"); ???????? ????????//服务端的输入与输出 ????????BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); ????????PrintWriter writer = new PrintWriter(socket.getOutputStream(), true); //true表示autoflush ???????? ????????//获取键盘输入 ????????BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); ???????? ????????while(true) ????????{ ????????????if(reader.ready()) ????????????{ ????????????????//捕获client的socket发来的消息 ????????????????PrintClientMsg(reader.readLine()); ????????????} ????????????if(keyboard.ready()) ????????????{ ????????????????//捕获当前server的键盘输入 ????????????????String content = keyboard.readLine(); ????????????????//打印在server的屏幕 ????????????????PrintConsoleMsg(content); ????????????????//发送到client ????????????????writer.println(content); ????????????} ????????} ???????? ????} ???? ????public static void PrintConsoleMsg(String msg) ????{ ????????System.out.println("Server:\t" + dateFormat.format(new Date()) + "\t" + msg); ????} ???? ????public static void PrintClientMsg(String msg) ????{ ????????System.out.println("Client:\t" + dateFormat.format(new Date()) + "\t" + msg); ????} } |
?
public class TestClient { ????public static final String datePattern = "yyyy-MM-dd HH:mm:ss SSS"; ????public static final SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern); ????public static void main(String[] args) throws UnknownHostException, IOException { ???????? ????????//1.创建一个socket ????????Socket socket = new Socket(); ????????//2.连接server的IP:端口 ????????InetSocketAddress address = new InetSocketAddress("localhost", 18824); ????????socket.connect(address); ????????//3.client socket的输入流和输出流 ????????BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); ????????PrintWriter writer = new PrintWriter(socket.getOutputStream(),true); ????????//当前client的键盘输入流 ????????BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); ???????? ????????while(true) ????????{ ????????????if(reader.ready()) ????????????{ ????????????????PrintServerMsg(reader.readLine()); ????????????} ????????????if(keyboard.ready()) ????????????{ ????????????????String content = keyboard.readLine(); ????????????????//打印在client的console ????????????????PrintConsoleMsg(content); ????????????????//发送给server ????????????????writer.println(content); ????????????} ????????} ????} ???? ????public static void PrintConsoleMsg(String msg) ????{ ????????System.out.println("Client:\t" + dateFormat.format(new Date()) + "\t" + msg); ????} ???? ????public static void PrintServerMsg(String msg) ????{ ????????System.out.println("Server:\t" + dateFormat.format(new Date()) + "\t" + msg); ????} } |
?
打开2个power shell分别作为server和client端。
待补充。
PS:word+cnblog发表博客first test。
标签:flush padding 要求 补充 sts 实现 new address lua
原文地址:https://www.cnblogs.com/sinkinben/p/10574762.html