https://github.com/threez/net-cpp
EventMachine like reactor implementation for cpp based on libevent
https://github.com/threez/net-cpp
Last synced: 10 months ago
JSON representation
EventMachine like reactor implementation for cpp based on libevent
- Host: GitHub
- URL: https://github.com/threez/net-cpp
- Owner: threez
- Created: 2011-07-13T21:45:05.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2011-07-13T22:25:14.000Z (almost 15 years ago)
- Last Synced: 2025-08-03T10:38:43.761Z (11 months ago)
- Language: C++
- Homepage:
- Size: 97.7 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Net-cpp
This is a simple library and test on building a EventMachine like interface
for implementing servers in C++. The Naming conventions should be familiar.
The implementation uses the very known libevent for implementing the reactor.
## Echo server sample
Here is a simple Application starting an Echo Server on port 9002.
#include "net.h"
class EchoConnection : public Net::Connection {
public:
void receivedData(const char * data, int length) {
if (strcmp(data, "exit\r\n") == 0) {
closeConnection();
} else {
sendData(data, length);
}
}
};
int main (int argc, char * const argv[]) {
Net::Server echoServer;
try {
Net::init();
Net::startServer("0.0.0.0", 9002, &echoServer);
Net::run();
return 0;
} catch (Net::Exception* ex) {
perror(ex->what()); // print network errors
return 1;
}
}
## HTTP server sample
This is another sample application implementing an http server:
#include "net.h"
const char * response = "HTTP/1.0 200 OK\r\nServer: SimpleHTTP\r\nHost: localhost\r\nContent-Length: 4\r\nContent-Type: text/html\r\n\r\nTest";
const int responseSize = strlen(response);
class HTTPConnection : public Net::Connection {
public:
void receivedData(const char * data, int length) {
sendData(response, responseSize);
closeConnection();
}
};
int main (int argc, char * const argv[]) {
Net::Server httpServer;
try {
Net::init();
Net::startServer("0.0.0.0", 9292, &httpServer);
Net::run();
return 0;
} catch (Net::Exception* ex) {
perror(ex->what()); // print network errors
return 1;
}
}
## Running more servers
Both Servers can easily run at the same time:
#include "net.h"
class EchoConnection : public Net::Connection {
public:
void receivedData(const char * data, int length) {
if (strcmp(data, "exit\r\n") == 0) {
closeConnection();
} else {
sendData(data, length);
}
}
};
const char * response = "HTTP/1.0 200 OK\r\nServer: SimpleHTTP\r\nHost: localhost\r\nContent-Length: 4\r\nContent-Type: text/html\r\n\r\nTest";
const int responseSize = strlen(response);
class HTTPConnection : public Net::Connection {
public:
void receivedData(const char * data, int length) {
sendData(response, responseSize);
closeConnection();
}
};
int main (int argc, char * const argv[]) {
Net::Server echoServer;
Net::Server httpServer;
try {
Net::init();
Net::startServer("0.0.0.0", 9002, &echoServer);
Net::startServer("0.0.0.0", 9292, &httpServer);
Net::run();
return 0;
} catch (Net::Exception* ex) {
perror(ex->what()); // print network errors
return 1;
}
}
# Trying out
Simply install libevent, example for Mac OS X:
brew install libevent
And then compile using the Makefile
make
Start the server using:
./net-cpp-test
And connect using a telnet client or curl:
telnet localhost 9002
curl http://localhost:9292/