epoll服务器

客户端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/input.h>
 
#define ERR_MSG(msg) do{\
        fprintf(stderr,"line:%d\n",__LINE__);\
        perror(msg);\
}while(0)
 
#define IP "192.168.250.100"  //本机IP ifconfig
#define PORT 8888            //1024-49151
 
int main(int argc, const char *argv[])
{
        //创建流式套接字
        int cfd = socket(AF_INET,SOCK_STREAM,0);
        if(cfd < 0){
                ERR_MSG("socket");
                return -1;
        }
        printf("cfd = %d\n",cfd);
 
        //允许端口快速重用
        int reuse = 1;
        if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
        {
                ERR_MSG("setsockopt");
                return -1;
        }
        printf("允许端口快速重用成功\n");
 
        //填充服务器地址信息结构体,供给connect函数使用
        struct sockaddr_in sin;
        sin.sin_family = AF_INET;                       //必须填AF_INET
        sin.sin_port = htons(PORT);                     //端口号,1024-49151
        sin.sin_addr.s_addr = inet_addr(IP);    //本机IP
 
        if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
        {
                ERR_MSG("connect");
                return -1;
        }
        printf("*************连接成功**************\n");
        printf("cfd=%d  __%d__\n",cfd,__LINE__);
 
 
        char buf[128] = "";
        ssize_t res = 0;
        while(1)
        {
                bzero(buf,sizeof(buf));
                //发送
                printf("请输入>>> ");
                fgets(buf,sizeof(buf),stdin);
                buf[strlen(buf)-1] = 0;
 
                if(write(cfd,buf,sizeof(buf)) < 0)
 
                {
                        ERR_MSG("sned");
                        return -1;
                }
                printf("发送成功\n");
 
                //接收
                bzero(buf,sizeof(buf));
                res = read(cfd,buf,sizeof(buf));
                if(res < 0)
                {
                        ERR_MSG("recv");
                        return -1;
                }
                else if(0 == res)
                {
                        printf("*************服务器下线**************\n");
                        printf("cfd=%d  __%d__\n",cfd,__LINE__);
                        break;
                }
                printf("cfd=%d: %s  __%d__\n",cfd,buf,__LINE__);
        }
 
        close(cfd);
 
        return 0;
}

服务器代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <pthread.h>
 
#define ERR_MSG(msg)                            \
    do                                          \
    {                                           \
        fprintf(stderr, "line:%d\n", __LINE__); \
        perror(msg);                            \
    } while (0)
#define IP "192.168.250.100" // 本机IP ifconfig
#define PORT 8888            // 1024-49151
 
// 需要传给线程处理的参数
struct cli_msg
{
    int newfd;
    struct sockaddr_in cin;
};
 
void *deal_cli_msg(void *arg);
 
int main(int argc, const char *argv[])
{
    // 创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("sfd = %d\n", sfd);
 
    // 允许端口快速重用
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用成功\n");
 
    // 填充地址信息结构体
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;            // 必须填AF_INET
    sin.sin_port = htons(PORT);          // 端口号,1024-49151
    sin.sin_addr.s_addr = inet_addr(IP); // 本机IP
 
    // 将IP和端口绑定到套接字上
    if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind success  __%d__\n", __LINE__);
 
    // 将套接字设置为被动监听状态,监听是否有客户端连接成功
    if (listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("listen success  __%d__\n", __LINE__);
 
    struct sockaddr_in cin; // 存储连接成功的客户信息
    socklen_t addrlen = sizeof(cin);
    int newfd = -1;
    pthread_t tid;
 
    char buf[128] = {0};
    ssize_t res = 0;
    int ret, i;
    struct cli_msg info;
    struct epoll_event event;
    struct epoll_event events[10]; // 存放发生事件的描述符的表
 
    // 创建一个新的epoll
    int epfd = epoll_create(1);
    if (epfd < 0)
    {
        printf("epoll_create err\n");
        exit(-1);
    }
    event.events = EPOLLIN;
    event.data.fd = sfd;
    ret = epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event);
    if (ret)
    {
        printf("epoll_ctl err\n");
        exit(-1);
    }
 
    while (1)
    {
        ret = epoll_wait(epfd, events, 10, -1);
        if (ret < 0)
        {
            printf("epoll_wait err\n");
            exit(-1);
        }
 
        for (int i = 0; i < ret; i++)
        {
            if (events[i].data.fd == sfd)
            {
                struct sockaddr_in cliaddr;
                int len = sizeof(cliaddr);
                int cfd = accept(sfd, (struct sockaddr *)&cliaddr, &len);
 
                event.events = EPOLLIN;
                event.data.fd = cfd;
                epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &event);
            }
            else
            {
                char buf[1024] = {0};
                int len = read(events[i].data.fd, buf, sizeof(buf));
                if (len == -1)
                {
                    ERR_MSG("read");
                    exit(-1);
                }
                else if (len == 0)
                {
                    printf("*************客户端下线**************\n");
                    epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
                    close(events[i].data.fd);
                }
                else if (len > 0)
                {
                    printf("read buf = %s\n", buf);
                    write(events[i].data.fd, buf, strlen(buf) + 1);
                }
            }
        }
    }
    close(sfd);
    close(epfd);
 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值