http://blog.csdn.net/tujiaw/article/details/17619609
window socket编程:http://blog.csdn.net/shouhuqi/article/details/8038567
linux服务端,在有新连接进来的时候发送4字节的时间然后关闭连接。
- #include <iostream>
- #include <arpa/inet.h>
- #include <time.h>
- #include <event2/event.h>
- #include <event2/bufferevent.h>
- #include <event2/buffer.h>
- #include <event2/listener.h>
- void listener_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int socklen, void *val)
- {
- time_t now = ::time(NULL);
- int32_t be32 = htonl(now);
- struct evbuffer *buf = evbuffer_new();
- evbuffer_add(buf, &be32, sizeof(be32));
- evbuffer_write(buf, fd);
- evbuffer_free(buf);
- evutil_closesocket(fd);
- }
- int main()
- {
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = htonl(0);
- sin.sin_port = htons(5000);
- struct event_base *base = event_base_new();
- struct evconnlistener *listener = evconnlistener_new_bind(base, listener_cb, NULL, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
- -1, (struct sockaddr*)&sin, sizeof(sin));
- std::cout << "start..." << std::endl;
- event_base_dispatch(base);
- return 0;
- }
windows客户端:
- #include <iostream>
- #include <assert.h>
- #include <string.h>
- #include <stdint.h>
- #include <time.h>
- #include "event2/event.h"
- #include "event2/bufferevent.h"
- #include "event2/buffer.h"
- #include "event2/listener.h"
- #include "windowsSocket.h"
- const char* CONNECT_IP = "192.168.239.147";
- const unsigned short CONNECT_PORT = 5000;
- void read_cb(struct bufferevent *bufferEvent, void *)
- {
- std::cout << "read_cb is comming..." << std::endl;
- struct evbuffer *input = bufferevent_get_input(bufferEvent);
- int32_t be32;
- evbuffer_remove(input, &be32, sizeof(be32));
- time_t val = ntohl(be32);
- struct tm t;
- ::localtime_s(&t, &val);
- char buf[128] = {0};
- asctime_s(buf, &t);
- std::cout << buf << std::endl;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- WindowsSocket::Startup(2, 2);
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = inet_addr(CONNECT_IP);
- sin.sin_port = htons(CONNECT_PORT);
- struct event_base *base = event_base_new();
- struct bufferevent *be = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
- if (!be) {
- std::cout << "bufferevent_socket_new error!" << std::endl;
- return -1;
- }
- if (-1 == bufferevent_socket_connect(be, (struct sockaddr*)&sin, sizeof(sin))) {
- std::cout << "bufferevent_socket_connect error!" << std::endl;
- return -1;
- }
- bufferevent_setcb(be, read_cb, NULL, NULL, base);
- bufferevent_enable(be, EV_READ);
- event_base_dispatch(base);
- WindowsSocket::Cleanup();
- system("pause");
- return 0;
- }
- // windowsSocket.h
- #pragma once
- class WindowsSocket
- {
- public:
- static void Startup(unsigned char loVer, unsigned char hiVer);
- static void Cleanup();
- private:
- static bool s_isStarted;
- };
- // windowsSocket.cpp
- #include "windowsSocket.h"
- #include <Windows.h>
- bool WindowsSocket::s_isStarted = false;
- void WindowsSocket::Startup(unsigned char loVer, unsigned char hiVer)
- {
- if (s_isStarted) {
- OutputDebugString(_T("WindowsSocket already initialized."));
- return;
- }
- WSAData wsaData;
- if (WSAStartup(MAKEWORD(loVer, hiVer), &wsaData)) {
- OutputDebugString(_T("Failed to initialize WindowsSocket."));
- } else {
- s_isStarted = true;
- }
- }
- void WindowsSocket::Cleanup()
- {
- if (s_isStarted) {
- s_isStarted = false;
- WSACleanup();
- }
- }