• 简单客户端服务器模型(C++、python和go语言示例)


    工作中用到了C/S模型,所做的也无非是给服务器发数据,但开发阶段会遇到程序自身的回环测试,需要用到简单的服务端以便验证数据发送的正确性。

    写软件用C++,跑测试用python,这段时间也刚好看go语言,所以都要有demo。以下三组程序实现的功能相同,这里一起做下总结。

    一、C++实现

    Boost.Asio是一个跨平台的C++库,它用现代C++方法为网络和底层I/O程序提供了一致的异步I/O模型。 为了跨平台,我用boost库实现,具体如下。

    服务端代码:

     1 /*
    2 File : svr.cpp
    3 Author : Mike
    4 E-Mail : Mike_Zhang@live.com
    5 */
    6
    7 #include <iostream>
    8 #include <boost/asio.hpp>
    9
    10 using boost::asio::ip::tcp;
    11 enum {max_length = 1024};
    12
    13 typedef boost::shared_ptr<tcp::socket> socket_ptr;
    14
    15 int main()
    16 {
    17 boost::asio::io_service io_service;
    18 tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), atoi("12345")));
    19 for (;;)
    20 {
    21 socket_ptr sock(new tcp::socket(io_service));
    22 a.accept(*sock);
    23 char data[max_length];
    24 boost::system::error_code error;
    25 size_t length = sock->read_some(boost::asio::buffer(data), error);
    26 data[length] = 0;
    27 std::cout<<data<<std::endl;
    28 sock->close();
    29 }
    30 return 0;
    31 }

    客户端代码:

     1 /*
    2 File : cli.cpp
    3 Author : Mike
    4 E-Mail : Mike_Zhang@live.com
    5 */
    6 #include <iostream>
    7 #include <boost/asio.hpp>
    8
    9 using boost::asio::ip::tcp;
    10 enum { max_length = 1024 };
    11
    12 int main(int argc, char* argv[])
    13 {
    14 boost::asio::io_service io_service;
    15 tcp::resolver resolver(io_service);
    16 tcp::resolver::query query(tcp::v4(), "127.0.0.1","12345");
    17 tcp::resolver::iterator iterator = resolver.resolve(query);
    18
    19 tcp::socket s(io_service);
    20 s.connect(*iterator);
    21
    22 std::cout << "Please input: ";
    23 char request[max_length];
    24 std::cin.getline(request, max_length);
    25 size_t request_length = strlen(request);
    26 boost::asio::write(s, boost::asio::buffer(request, request_length));
    27 return 0;
    28 }

    编译:g++ cli.cpp -o cli -lboost_system -lboost_thread-mt

    二、python实现

    服务端代码:

     1 '''
    2 File : svr.py
    3 Author : Mike
    4 E-Mail : Mike_Zhang@live.com
    5 '''
    6 import socket,os
    7 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    8 sock.bind(('127.0.0.1', 12345))
    9 sock.listen(5)
    10 while True:
    11 connection,address = sock.accept()
    12 buf = connection.recv(1024)
    13 print buf
    14 connection.close()

    客户端代码:

     1 '''
    2 File : cli.py
    3 Author : Mike
    4 E-Mail : Mike_Zhang@live.com
    5 '''
    6 import socket
    7 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    8 sock.connect(('127.0.0.1', 12345))
    9 #sock.send('Test\n')
    10 sock.send(raw_input("Please input : "))
    11 sock.close()

    三、go语言实现

    服务端代码:

     1 /*
    2 File : svr.go
    3 Author : Mike
    4 E-Mail : Mike_Zhang@live.com
    5 */
    6 package main
    7
    8 import(
    9 "net"
    10 "fmt"
    11 "bufio"
    12 )
    13
    14 func main() {
    15 client,err := net.Listen("tcp","127.0.0.1:12345")
    16 if err != nil {
    17 fmt.Printf("Error : %s\n",err.String())
    18 }
    19 for {
    20 if c, err := client.Accept(); err == nil {
    21 defer c.Close()
    22 line, _ := bufio.NewReader(c).ReadString('\n')
    23 fmt.Println(line)
    24 }
    25 }
    26 }

    客户端代码:

     1 /*
    2 File : cli.go
    3 Author : Mike
    4 E-Mail : Mike_Zhang@live.com
    5 */
    6 package main
    7
    8 import(
    9 "net"
    10 "fmt"
    11 )
    12
    13 func main() {
    14 conn, err := net.Dial("tcp", "127.0.0.1:12345")
    15 if err != nil {
    16 fmt.Printf("Error : %s\n",err.String())
    17 }
    18 conn.Write([]byte("Just a test"))
    19 }

    运行效果(go语言为例):

    好,就这些了,希望对你有帮助。

  • E-Mail : Mike_Zhang@live.com
  • 转载请注明出处,谢谢!
  • 相关阅读:
    log4j.properties配置详解
    maven常用命令集
    maven核心,pom.xml详解
    如何学习一门新技术?
    主题模型(概率潜语义分析PLSA、隐含狄利克雷分布LDA)
    聚类
    梯度下降中的学习率如何确定
    GPU版的tensorflow在windows上的安装时的错误解决方案
    maven笔记(maven使用与安装,eclipse中maven的配置,maven项目的创建)
    RNN的介绍
  • 原文地址:https://www.cnblogs.com/MikeZhang/p/simpleTcpClientServerCppPythonAndGo.html
  • Copyright © 2020-2023  润新知