#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <event.h>
#define PORT 25341
#define BACKLOG 5
#define MEM_SIZE 10
struct event_base* base;
struct sock_ev {
struct event* read_ev;
struct event* write_ev;
char* buffer;
};
int setnonblock(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL);
flags |= O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
}
void release_sock_event(struct sock_ev* ev)
{
event_del(ev->read_ev);
free(ev->read_ev);
free(ev->write_ev);
free(ev->buffer);
free(ev);
ev = NULL;
}
void on_write(int sock, short event, void* arg)
{
char* buffer = (char*)arg;
printf("send:[%d]%s,%u\n",buffer,buffer,strlen(buffer));
send(sock, buffer, strlen(buffer), 0);
free(buffer);
}
void on_read(int sock, short event, void* arg)
{
struct event* write_ev;
int size;
struct sock_ev* ev = (struct sock_ev*)arg;
ev->buffer = (char*)malloc(MEM_SIZE);
bzero(ev->buffer, MEM_SIZE);
size = recv(sock, ev->buffer, MEM_SIZE-2, 0);
printf("begin\n");
ev->buffer[size] = '\n';
printf("paddr:%d\n",ev->buffer);
printf("receive data:%s, size:%d\n", ev->buffer, size);
if (size == 0 ) { // || size > MEM_SIZE-1 不加这个判断条件,在输入超长的时候就会报错
release_sock_event(ev);
close(sock);
return;
}
printf("end1\n");
event_set(ev->write_ev, sock, EV_WRITE, on_write, ev->buffer);
event_base_set(base, ev->write_ev);
event_add(ev->write_ev, NULL);
printf("end2\n");
}
void on_accept(int sock, short event, void* arg)
{
struct sockaddr_in cli_addr;
int newfd, sin_size;
struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));
ev->read_ev = (struct event*)malloc(sizeof(struct event));
ev->write_ev = (struct event*)malloc(sizeof(struct event));
sin_size = sizeof(struct sockaddr_in);
newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
setnonblock(newfd);
event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, ev); //EV_PERSIST 去掉这个,只read一次就退出了
event_set(ev->write_ev, newfd, EV_WRITE|EV_PERSIST, on_write, ev->buffer);
event_base_set(base, ev->write_ev);
event_base_set(base, ev->read_ev);
event_add(ev->read_ev, NULL);
}
int main(int argc, char* argv[])
{
struct sockaddr_in my_addr;
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
int yes = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
listen(sock, BACKLOG);
struct event listen_ev;
base = event_base_new();
setnonblock(sock);
event_set(&listen_ev, sock, EV_READ, on_accept, NULL); //EV_PERSIST
event_base_set(base, &listen_ev);
event_add(&listen_ev, NULL);
event_base_dispatch(base);
return 0;
}
在学习libevent的时候,遇到一个问题,在上面的例子里,如果对服务器发送超过10个以上长度的字符串的时候,服务器就会没有反应,求牛人指点。
我自己觉得问题出在这个函数里面的buffer处理上
void on_read(int sock, short event, void* arg)
{
struct event* write_ev;
int size;
struct sock_ev* ev = (struct sock_ev*)arg;
ev->buffer = (char*)malloc(MEM_SIZE);
bzero(ev->buffer, MEM_SIZE);
size = recv(sock, ev->buffer, MEM_SIZE-2, 0);
printf("begin\n");
ev->buffer[size] = '\n';
printf("paddr:%d\n",ev->buffer);
printf("receive data:%s, size:%d\n", ev->buffer, size);
if (size == 0 ) { // || size > MEM_SIZE-1 不加这个判断条件,在输入超长的时候就会报错
release_sock_event(ev);
close(sock);
return;
}
printf("end1\n");
event_set(ev->write_ev, sock, EV_WRITE, on_write, ev->buffer);
event_base_set(base, ev->write_ev);
event_add(ev->write_ev, NULL);
printf("end2\n");
}
recv(sock, ev->buffer, MEM_SIZE-2, 0);
这里为什么要 MEM_SIZE-2?
没有什么原因,就是原来用MEM_SIZE有问题,猜测是字符串结尾的问题,就-1,然后又-2,但是都不能解决问题
@dbproxy: 是按照字节流来处理,跟是否为以 0 结尾的字符串无关系。
size = recv(sock, ev->buffer, MEM_SIZE, 0);
你这里指定了接收缓冲区为 10 个字节,每次 recv 10 个字节,那么 size 的值应该是小于等于 10 。
应该循环调用 recv 类似:while(true){size = recv(sock, ev->buffer, MEM_SIZE, 0);}
recv 应该是在有一定长度的数据后就通知一次,也就是你客户端 send 了 n 字节数据,服务器可能通过多次将 n 字节数据分成 m 个部分,然后通知 m 次 recv 事件(这个过程也不是服务器在接收完所有 n 字节数据后才开始执行)。那么你就不能把客户端一次 send 操作同服务器的一次 recv 对应起来。
所以客户端需要同服务器商定协议,即设计你自己的报文,报文简单的包含:Header-Body-Footer 三个部分,recv 通过建立协议栈来分析 Header 和 Footer,以确定当前收到的数据是否为一次完成的报文。
最好的理解该模式的方法就是假定 recv 每次都只接收到 1 字节,而又不知道客户端的发送频率和发送时间。