码迷,mamicode.com
首页 > 其他好文 > 详细

Socket编程入门

时间:2019-03-21 21:38:13      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:flush   padding   要求   补充   sts   实现   new   address   lua   

  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:\t" + dateFormat.format(new Date()) + "\t" + msg);

????}

????

????public static void PrintClientMsg(String msg)

????{

????????System.out.println("Client:\t" + dateFormat.format(new Date()) + "\t" + 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:\t" + dateFormat.format(new Date()) + "\t" + msg);

????}

????

????public static void PrintServerMsg(String msg)

????{

????????System.out.println("Server:\t" + dateFormat.format(new Date()) + "\t" + 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编程入门

标签:flush   padding   要求   补充   sts   实现   new   address   lua   

原文地址:https://www.cnblogs.com/sinkinben/p/10574762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!