Simple client server model (example of C ++, Python, And go LANGUAGE)

Source: Internet
Author: User

Used in workC/SThe model does nothing more than sending data to the server, but the development stage will encounterProgramIn its own loop test, a simple server needs to be used to verify the correctness of data transmission.

For software writingC ++For testingPython, This time is just to seeGoLanguage, so there must beDemo. The functions of the following three groups of programs are the same. Here we will summarize them.

I,C ++Implementation

Boost. ASIO Is a cross-platform C ++ Library, which uses modern C ++ Method: Network and bottom layer I/O The program provides consistent asynchronous I/O Model.For cross-platform use Boost Library implementation, as follows.

ServerCode:

 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;
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 }

Client code:

 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;
15 TCP: resolver (io_service );
16 TCP: resolver: Query query (TCP: V4 (), " 127.0.0.1 " , " 12345 " );
17 TCP: resolver: 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 }

Compile: G ++ cli. cpp-O cli-lboost_system-lboost_thread-mt

II,PythonImplementation

Server code:

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

Client code:

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

III,GoLanguage implementation

Server code:

 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 }

Client code:

 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 }

Running Effect (GoLanguage example ):

Okay, that's all. I hope it will help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.