随手写了一个linux服务端与window客户端的epoll程序,当做练习把。

时间:2023-03-08 16:48:49
随手写了一个linux服务端与window客户端的epoll程序,当做练习把。

linux服务端:监听链接,处理消息

#include <sys/socket.h>    
#include <sys/epoll.h>    
#include <netinet/in.h>    
#include <arpa/inet.h>    
#include <fcntl.h>    
#include <unistd.h>    
#include <stdio.h>    
#include <errno.h>

#define MAX_EVENTS 500

int main()
{
int epfd = -1;
int listensocket;
struct sockaddr_in serveraddr;
char buf[MAX_EVENTS][100] = {0};

int nCount = 0;

struct epoll_event ev, evs[MAX_EVENTS];

listensocket = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listensocket)
{
perror("socket fail\n");
return -1;
}

printf("listensocket:0x%x\n", listensocket);

memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = INADDR_ANY;
serveraddr.sin_port = htons(7778);

int bReuse = 1;
if (-1 == setsockopt(listensocket, SOL_SOCKET, SO_REUSEADDR, (char*)&bReuse, sizeof(bReuse)))
{
perror("setsockopt fail\n");
return -1;
}

if (-1 == bind(listensocket, (struct sockaddr *)&serveraddr, sizeof(serveraddr)))
{
perror("bind fail\n");
return -1;
}

if (-1 == listen(listensocket, 3))
{
perror("listen fail\n");
return -1;
}

epfd = epoll_create(MAX_EVENTS);
if (-1 == epfd)
{
perror("epoll_create fail\n");
return -1;
}

printf("epfd:0x%x\n", epfd);

ev.events = EPOLLIN;
ev.data.fd = listensocket;
    if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, listensocket, &ev))
{
perror("epoll_create fail\n");
return -1;
}

while (1)
{
int i = 0;
struct sockaddr_in peeraddr;
int addrlen = sizeof(peeraddr);
int fd;
int rcvnum = 0;
int r = 9;

memset(&peeraddr, 0, sizeof(peeraddr));

//printf("waiting...\n");
nCount = epoll_wait(epfd, evs, MAX_EVENTS, -1);
if (0 > nCount)
{
perror("epoll_create fail\n");
continue;
}

for(i = 0; i < nCount; i++)
{
if (evs[i].data.fd == listensocket)
{
fd = accept(listensocket, (struct sockaddr *)&peeraddr, &addrlen);
printf("peeraddr %s:%d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));

ev.events = EPOLLIN;
ev.data.fd = fd;
if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev))
{
perror("epoll_ctl listen fail\n");
return -1;
}
}
else
{
rcvnum = recv(evs[i].data.fd, buf[i], 100, 0);
if (0 == rcvnum)
{
close(evs[i].data.fd);
continue;
}
buf[i][99] = 0;
printf("recv:%s\n", buf);
memset(buf[i], 0, 100);
}
}
}

return 0;
}

window客户端:主要发送消息至liunx服务端:

#include <stdio.h>   
#include <winsock2.h> 
 
int main(int argc, char **arg)
{
SOCKET fd;
struct sockaddr_in serveraddr;
int nCount = 0;
int ret;

WSADATA wsaData;
    short version = MAKEWORD(2,2);
WSAStartup(version,&wsaData);//初始化socket

if (argc != 2)
{
printf("param fail\n");
return -1;
}

fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == fd)
{
perror("socket fail\n");
return -1;
}

printf("fd:0x%x\n", fd);

memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr("172.16.192.92");
serveraddr.sin_port = htons(7778);

if (-1 == connect(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)))
{
printf("connect fail\n");
return -1;
}

while (1)
{
ret = send(fd, arg[1], strlen(arg[1]), 0);
printf("send:%d ret:%d\n", strlen(arg[1]), ret);
if (ret < 0)
{
closesocket(fd);
break;
}
Sleep(1000);
}

return 0;
}