gcc libserver.c -o libserver -levent
-
/*libevent server*/
-
#include <sys/socket.h>
-
#include <sys/types.h>
-
#include <netinet/in.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <event.h>
-
#include <stdlib.h>
-
#include<time.h>
-
#define PORT 5188
-
#define BACKLOG 5
-
#define BUFFSIZE 1024
-
/* 初始化libevent */
-
struct event_base* base;
-
/*sock_ev struct*/
-
struct sock_ev
-
{
-
struct event* read_ev;
-
struct event* write_ev;
-
unsigned char* buffer;
-
};
-
/* release event */
-
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);
-
}
-
-
void on_write(int sock, short event, void* arg)
-
{
-
char* buffer = (char*)arg;
-
send(sock, buffer, strlen(buffer), 0);
-
-
free(buffer);
-
}
-
void register_package(unsigned char *buffer){
-
int i;
-
printf("Receive Register Package\n");
-
printf("ID=%x%x%x%x\n",buffer[3],buffer[2],buffer[1],buffer[0]);
-
printf("PHONE_NUMBER=");
-
for(i=4;i<15;i++){
-
printf("%c",buffer[i]); //PHONE_NUMBER打印
-
}
-
printf("\n");
-
printf("IP address: %d.%d.%d.%d",buffer[16],buffer[17],buffer[18],buffer[19]);
-
printf("\n");
-
}
-
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(BUFFSIZE);
-
bzero(ev->buffer, BUFFSIZE);
-
size = recv(sock, ev->buffer, BUFFSIZE, 0);
-
register_package(ev->buffer);
-
if (size == 0)
-
{
-
release_sock_event(ev);
-
close(sock);
-
return;
-
}
-
bzero(ev->buffer, BUFFSIZE);
-
strcat(ev->buffer,"libevent i know you\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);
-
}
-
-
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);
-
/* accept */
-
newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
-
printf("ip=%s port=%d\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
-
event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, 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 = htonl(INADDR_ANY);
-
bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
-
listen(sock, BACKLOG);
-
/*libevent*/
-
/* 1 建立默认的 event_base event_base_new()函数声明在<event2/event.h>中*/
-
-
base = event_base_new();
-
struct event listen_ev;
-
/* 初始化事件event,设置回调函数和关注的事件 */
-
/*void event_set(struct event *ev, int fd, short event, void (*cb)(int, short, void *), void *arg)*/
-
event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);
-
/* 3)设置event从属的event_base */
-
/* event_base_set(base, &ev); */
-
/* 这一步相当于指明event要注册到哪个event_base实例上; */
-
event_base_set(base, &listen_ev);
-
-
/* 4)是正式的添加事件的时候了 */
-
event_add(&listen_ev, NULL);
-
/*event_add(&listen_ev, timeout);*/
-
/* 基本信息都已设置完成,只要简单的调用event_add()函数即可完成,其中timeout是定时值; */
-
/* 5)程序进入无限循环,等待就绪事件并执行事件处理 */
-
event_base_dispatch(base);
-
-
return 0;
-
}
gcc libclient.c -o libclient
-
/*libevent client*/
-
#include <unistd.h>
-
#include <stdio.h>
-
#include <fcntl.h>
-
#include <netdb.h>
-
#include <sys/socket.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#define cerror(str) do{perror(str); exit(EXIT_FAILURE); }while(0)
-
int main()
-
{
-
int fd;
-
struct sockaddr_in sin;
-
memset(&sin, 0, sizeof(sin));
-
sin.sin_family = AF_INET;
-
sin.sin_port = htons(5188);
-
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
-
fd=socket(AF_INET, SOCK_STREAM, 0);
-
connect(fd, (struct sockaddr*)&sin, sizeof(sin));
-
while(1){
-
char sendbuf[2048]={0x78,0x56,0x34,0x12,0x31,0x33,0x30,0x36,0x36,0x36,0x36,0x38,0x38,0x38,0x36,0x38,0xc0,0xa8,0x01,0x42};
-
char recvbuf[1024]={0};
-
write(fd,sendbuf,strlen(sendbuf));
-
sleep(5);
-
read(fd,recvbuf,sizeof(recvbuf));
-
fputs(recvbuf,stdout);
-
memset(sendbuf,0,sizeof(sendbuf));
-
memset(recvbuf,0,sizeof(recvbuf));
-
sleep(2);
-
-
}
-
close(fd);
-
return 0;
-
}
阅读(909) | 评论(0) | 转发(0) |