Simple Client server code sample implemented in C + +, Python, and go language _c language

Source: Internet
Author: User

The work of the C/S model, what is done is to send data to the server, but the development phase will encounter the loop test of the program itself, need to use a simple server to verify the correctness of the data sent.

Write software in C + +, run test with Python, this time also just see go language, so have to have demo. The following three sets of programs implement the same functionality, here is a summary.

First, C + + implementation

Boost.asio is a cross-platform C + + library that provides a consistent asynchronous I/O model for network and underlying I/O programs using modern C + + methods. In order to cross the platform, I use Boost library implementation, as follows.

Service-Side code:

Copy Code code as follows:

/*
File:svr.cpp
Author:mike
E-mail:mike_zhang@live.com
*/

#include <iostream>
#include <boost/asio.hpp>

Using Boost::asio::ip::tcp;
enum {max_length = 1024};

typedef boost::shared_ptr<tcp::socket> SOCKET_PTR;

int main ()
{
Boost::asio::io_service Io_service;
Tcp::acceptor A (Io_service, Tcp::endpoint (Tcp::v4 (), Atoi ("12345"));
for (;;)
{
Socket_ptr Sock (new Tcp::socket (Io_service));
A.accept (*sock);
Char Data[max_length];
Boost::system::error_code error;
size_t length = Sock->read_some (Boost::asio::buffer (data), error);
Data[length] = 0;
std::cout<<data<<std::endl;
Sock->close ();
}
return 0;
}

Client code:

Copy Code code as follows:

/*
File:cli.cpp
Author:mike
E-mail:mike_zhang@live.com
*/
#include <iostream>
#include <boost/asio.hpp>

Using Boost::asio::ip::tcp;
enum {max_length = 1024};

int main (int argc, char* argv[])
{
Boost::asio::io_service Io_service;
Tcp::resolver Resolver (io_service);
Tcp::resolver::query query (TCP::V4 (), "127.0.0.1", "12345");
Tcp::resolver::iterator iterator = resolver.resolve (query);

Tcp::socket s (io_service);
S.connect (*iterator);

Std::cout << "Please input:";
Char Request[max_length];
Std::cin.getline (Request, max_length);
size_t request_length = strlen (request);
Boost::asio::write (S, boost::asio::buffer (Request, request_length));
return 0;
}

Compiling: g++ cli.cpp-o cli-lboost_system-lboost_thread-mt

Second, Python implementation

Service-Side code:

Copy Code code as follows:

'''
File:svr.py
Author:mike
E-mail:mike_zhang@live.com
'''
Import Socket,os
Sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
Sock.bind ((' 127.0.0.1 ', 12345))
Sock.listen (5)
While True:
Connection,address = Sock.accept ()
BUF = CONNECTION.RECV (1024)
Print BUF
Connection.close ()

Client code:

Copy Code code as follows:

'''
File:cli.py
Author:mike
E-mail:mike_zhang@live.com
'''
Import socket
Sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
Sock.connect ((' 127.0.0.1 ', 12345))
#sock. Send (' test\n ')
Sock.send (raw_input ("Please input:")
Sock.close ()

Third, go language implementation

Service-Side code:

Copy Code code as follows:

/*
File:svr.go
Author:mike
E-mail:mike_zhang@live.com
*/
Package Main

Import
"NET"
"FMT"
"Bufio"
)

Func Main () {
Client,err: = Net. Listen ("TCP", "127.0.0.1:12345")
If Err!= nil {
Fmt. Printf ("Error:%s\n", err.) String ())
}
for {
If C, err: = client. Accept (); Err = = Nil {
Defer C.close ()
Line, _: = Bufio. Newreader (c). ReadString (' \ n ')
Fmt. Println (line)
}
}
}


Client code:

Copy Code code as follows:

/*
File:cli.go
Author:mike
E-mail:mike_zhang@live.com
*/
Package Main

Import
"NET"
"FMT"
)

Func Main () {
Conn, Err: = Net. Dial ("TCP", "127.0.0.1:12345")
If Err!= nil {
Fmt. Printf ("Error:%s\n", err.) String ())
}
Conn. Write ([]byte ("Just a Test")
}

Run effect (for example, go language):

Well, that's all, I hope to 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.