资源简介
libevent多线程,
参考mariotcp
参考http://blog.csdn.net/i_am_jojo/article/details/7587838流程图
代码片段和文件信息
// Client.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include “../Server/CProtocol.h“
#include
#pragma comment (lib“ws2_32.lib“)
#pragma comment (lib“wsock32.lib“)
class Client
{
public:
Client()
:m_sock_id(INVALID_SOCKET)
{
WSADATA WSAData;
WSAStartup(0x0201 &WSAData);
}
~Client()
{
if (m_sock_id!=INVALID_SOCKET)
{
closesocket(m_sock_id);
}
WSACleanup();
}
void Test(char* serverint port)
{
if (m_sock_id==INVALID_SOCKET)
{
m_sock_id=create_socket(server port);
}
if (m_sock_id)
{
login(m_sock_id);
logout(m_sock_id);
}
}
private:
SOCKET m_sock_id;
private:
SOCKET create_socket(char* server int port)
{
SOCKET sockfd=socket(AF_INET SOCK_STREAM 0);
if (SOCKET_ERROR!=sockfd)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(server);
addr.sin_port = htons(port);
connect(sockfd (struct sockaddr *) &addr sizeof(struct sockaddr));
}
return sockfd;
}
int send_cmd(SOCKET c int cmd char *data size_t data_len)
{
Head *h = (Head *) (data);
h->pkglen = sizeof(Head)+data_len;
h->stx = emStx;
h->nFunID = cmd;
int rs = send(c data h->pkglen 0);
return rs == h->pkglen;
}
int recv_cmd(SOCKET c char *data size_t data_len)
{
int rs=recv(cdatadata_len0);
if (rs>0)
{
Head *h = (Head *)(data);
if (h->pkglen==rs)
{
Proto_Login_Rsp *login=(Proto_Login_Rsp*)(data+sizeof(Head));
return 1;
}
}
return 0;
}
void login(SOCKET sock_id)
{
char buf[emMaxBuffLen]={0};
Proto_Login_Req *login=(Proto_Login_Req*)(buf+sizeof(Head));
login->uid=10;
send_cmd(sock_idemFunLoginbufsizeof(Proto_Login_Req));
}
void logout(SOCKET sock_id)
{
char buf[emMaxBuffLen]={0};
if (recv_cmd(sock_idbufsizeof(buf)))
{
Proto_Login_Req *login=(Proto_Login_Req*)(buf+sizeof(Head));
login->uid=10;
send_cmd(sock_idemFunLogoutbufsizeof(Proto_Login_Req));
}
}
};
int _tmain(int argc _TCHAR* argv[])
{
char* server = “127.0.0.1“;
int port = 7000;
Client client;
client.Test(serverport);
getchar();
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
.CA.... 1995 2014-06-19 11:06 Server\CLibEventData.h
.CA.... 1422 2014-06-19 12:13 Server\CProtocol.h
.CA.... 8205 2014-06-19 12:25 Server\LibEvent.cpp
.CA.... 835 2014-06-19 12:09 Server\LibEvent.h
.CA.... 1263 2014-06-19 12:14 Server\LibEventFunction.cpp
.CA.... 2563 2014-06-19 12:14 Server\LibEventFunction.h
.CA.... 198 2014-06-19 10:16 Server\LibPOCO.cpp
.CA.... 1797 2014-06-19 10:16 Server\LibPOCO.h
.CA.... 859 2014-06-19 12:21 Server\LibUserFunction.cpp
.CA.... 321 2014-06-19 12:09 Server\LibUserFunction.h
.CA.... 932 2014-06-19 10:16 Server\ReadMe.txt
.CA.... 259 2014-06-19 10:16 Server\Server.cpp
.CA.... 209 2014-06-19 10:16 Server\stdafx.cpp
.CA.... 333 2014-06-19 10:16 Server\stdafx.h
.CA.... 5111 2014-06-19 11:00 Server\Test.vcproj
.CA.... 2195 2014-06-19 12:20 Client\Client.cpp
.CA.... 4484 2014-06-19 11:00 Client\Client.vcproj
.CA.... 944 2014-06-19 11:00 Client\ReadMe.txt
.CA.... 211 2014-06-19 11:00 Client\stdafx.cpp
.CA.... 276 2014-06-19 11:00 Client\stdafx.h
.CA.... 12719 2014-06-19 10:16 libevent\arc4random.c
.CA.... 72383 2014-06-19 10:16 libevent\buffer.c
.CA.... 15801 2014-06-19 10:16 libevent\bufferevent-internal.h
.CA.... 22547 2014-06-19 10:16 libevent\bufferevent.c
.CA.... 18234 2014-06-19 10:16 libevent\bufferevent_async.c
.CA.... 15048 2014-06-19 10:16 libevent\bufferevent_filter.c
.CA.... 8815 2014-06-19 10:16 libevent\bufferevent_pair.c
.CA.... 28521 2014-06-19 10:16 libevent\bufferevent_ratelim.c
.CA.... 17806 2014-06-19 10:16 libevent\bufferevent_sock.c
.CA.... 8604 2014-06-19 10:16 libevent\buffer_iocp.c
............此处省略78个文件信息
- 上一篇:RRT 随机森林全局路径规划
- 下一篇:PortMonNt.rar
评论
共有 条评论