Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

wangqiangcc/libasio

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libasio

A modern C++ network library based on asio for high performance network services

sample tcp server

#include <iostream>
#include "EventLoop.h"
#include "TcpServer.h"
#include "TcpConn.h"

void OnConnection(const TCPConnPtr& conn) {
	if (conn->IsConnected()) {
		std::cout << conn->GetName() <<" connection accepted.\n";
	}
	else if (conn->IsDisconnecting()) {
		std::cout << conn->GetName() <<" connection disconnecting.\n";
	}
}

void OnMessage(const TCPConnPtr& conn, ByteBuffer& buffer) {
    std::cout << "recv msg " << std::string(buffer.Data(), buffer.Size()) << std::endl;
    buffer.ReadBytes(buffer.Size());
    buffer.Normalize();
    conn->Send("server say hello!");
}

int main()
{
	EventLoop loop;
	TCPServer s(&loop, "0.0.0.0", 7788, "EchoServer", 4);
	s.SetConnectionCallback(OnConnection);
	s.SetMessageCallback(OnMessage);
	s.Init();
	s.Start();

	loop.Run();

	return 0;
}

sample tcp client

#include <iostream>
#include "EventLoop.h"
#include "TcpClient.h"
#include "TcpConn.h"

void OnConnection(const TCPConnPtr& conn) {
	if (conn->IsConnected()) {
		std::cout << conn->GetName() <<" connection accepted.\n";
                conn->Send("client say hello!");
	}
	else if (conn->IsDisconnecting()) {
		std::cout << conn->GetName() <<" connection disconnecting.\n";
	}
}

void OnMessage(const TCPConnPtr& conn, ByteBuffer& buffer) {
    std::cout << "recv msg " << std::string(buffer.Data(), buffer.Size()) << std::endl;
    buffer.ReadBytes(buffer.Size());
    buffer.Normalize();
    conn->Send("client say hello!");
}

int main()
{
	EventLoop loop;
	TCPClient client(&loop, "127.0.0.1", 7788, "EchoClient");
	client.Connect();
	client.SetAutoReconnect(true);
	client.SetConnCallback(OnConnection);
	client.SetMessageCallback(OnMessage);

	loop.Run();

	return 0;
}

About

A modern C++ network library based on asio for high performance network services

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.