Socket编程入门

Posted sinkinben

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket编程入门相关的知识,希望对你有一定的参考价值。

  1. socket基本知识

网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。建立网络通信连接至少要一对端口号(socket)。socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口;HTTP是轿车,提供了封装或者显示数据的具体形式;Socket是发动机,提供了网络通信的能力。

Java Socket的通信时序图如下。

技术图片

Java Socket的数据通信模型如下。

技术图片

?

  1. Java编程

  2. 通信步骤

Server端

Client端

  1. 创建ServerSocker
  2. 绑定端口
  3. 等待端口的通信请求(此步会返回一个socket,这个socket作为server端的socket)
  4. 建立server端的socket的输入流(reader)和输出流(writer)
  5. reader可以获取client的通信数据,writer可以向client发送数据
  1. 创建一个socket
  2. 连接IP:port(要求server存在)
  3. 建立client的输入流(reader)和输出流(writer)
  4. reader可以获取server的通信数据,writer可以向server发送数据
  1. 代码实例

  • 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())

????????????{

????????????????//捕获clientsocket发来的消息

????????????????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.连接serverIP:端口

????????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();

????????????????//打印在clientconsole

????????????????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);

????}

}

?

  1. 运行结果

打开2个power shell分别作为server和client端。

  1. 运行server端

技术图片

  1. 运行client进行连接

技术图片

技术图片

  1. client输入信息,模拟通信

技术图片

技术图片

  1. server输入回复信息

技术图片

技术图片

  1. client输入回复信息

技术图片

技术图片

  1. C++编程

待补充。

PS:word+cnblog发表博客first test。

参考文献

以上是关于Socket编程入门的主要内容,如果未能解决你的问题,请参考以下文章

Socket编程入门

python3socket编程入门

socket编程入门

Go 语言网络编程系列— Socket 编程入门:Dial 函数及其使用

[转帖]脑残式网络编程入门:我们在读写Socket时,究竟在读写什么?

[8421论坛]2019 Socket网络编程入门到进阶与实战完整视频教程