Socket编程入门
Posted sinkinben
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket编程入门相关的知识,希望对你有一定的参考价值。
socket基本知识
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。建立网络通信连接至少要一对端口号(socket)。socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口;HTTP是轿车,提供了封装或者显示数据的具体形式;Socket是发动机,提供了网络通信的能力。
Java Socket的通信时序图如下。
Java Socket的数据通信模型如下。
?
Java编程
通信步骤
Server端 | Client端 |
|
|
代码实例
- Server端
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: " + dateFormat.format(new Date()) + " " + msg); ????} ???? ????public static void PrintClientMsg(String msg) ????{ ????????System.out.println("Client: " + dateFormat.format(new Date()) + " " + msg); ????} } |
?
- Client端
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: " + dateFormat.format(new Date()) + " " + msg); ????} ???? ????public static void PrintServerMsg(String msg) ????{ ????????System.out.println("Server: " + dateFormat.format(new Date()) + " " + msg); ????} } |
?
运行结果
打开2个power shell分别作为server和client端。
- 运行server端
- 运行client进行连接
- client输入信息,模拟通信
- server输入回复信息
- client输入回复信息
C++编程
待补充。
PS:word+cnblog发表博客first test。
参考文献
以上是关于Socket编程入门的主要内容,如果未能解决你的问题,请参考以下文章
Go 语言网络编程系列— Socket 编程入门:Dial 函数及其使用