工作中用到了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語言為例):
好,就這些了,希望對你有協助。