网络编程-day5

IO复用select实现TCP服务端

c 复制代码
#define IP "192.168.125.196"
#define PORT 8888
int main(int argc, const char *argv[])
{
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sfd == -1){
        perror("socket");
        return -1;
    }

    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
        perror("setsockopt");
        return -1;
    }

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    if(bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
        perror("bind");
        return -1;
    }

    if(listen(sfd, 128) == -1){
        perror("listen");
        return -1;
    }

    fd_set readfds, tempfds;
    FD_ZERO(&readfds);
    FD_SET(sfd, &readfds);
    int maxfd = sfd;
    int newfd = -1;
    struct sockaddr_in cin_arr[1024] = {0};
    struct sockaddr_in cin;
    socklen_t cinlen = sizeof(cin);
    char rbuf[128] = {0};
    while(1){
        tempfds = readfds;
        int res = select(maxfd+1, &tempfds, NULL, NULL, NULL);
        if(res == -1){
            perror("select");
            return -1;
        }else if(res == 0){
            printf("timeout\n");
            return -1;
        }
        for(int i=0; i<=maxfd; i++){
            if(!FD_ISSET(i, &tempfds)){
                continue;
            }
            if(i == sfd){
                newfd = accept(sfd, (struct sockaddr *)&cin, &cinlen);
                if(newfd == -1){
                    perror("accept");
                    return -1;
                }
                printf("[%s:%d]:online\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));
                cin_arr[newfd] = cin;
                FD_SET(newfd, &readfds);
                if(newfd > maxfd){
                    maxfd = newfd;
                }
            }else{
                bzero(rbuf, sizeof(rbuf));
                int res = recv(i, rbuf, sizeof(rbuf), 0);
                if(res == 0){                                                                                 
                    printf("offline\n");
                    close(i);
                    FD_CLR(i, &readfds);
                    for(int k=maxfd; k>=sfd; k--){
                        if(FD_ISSET(k, &readfds)){
                            maxfd = k;
                            break;
                        }
                    }
                    continue;
                }
                printf("[%s:%d]:%s\n", inet_ntoa(cin_arr[i].sin_addr), ntohs(cin_arr[i].sin_port), rbuf);
                for(int j=4; j<=maxfd; j++){
                    if(j != i){
                        send(j, rbuf, strlen(rbuf), 0);
                    }
                }
            }
        }
    }
    close(sfd);
    return 0;
}

IO复用poll实现TCP客户端

c 复制代码
#include <myhead.h>
#define S_IP "192.168.125.196"
#define S_PORT 8888
#define C_IP "192.168.125.196"
#define C_PORT 7777
int main(int argc, const char *argv[])
{
    int cfd = socket(AF_INET, SOCK_STREAM, 0);
    if(cfd == -1){
        perror("socket");
        return -1;
    }

    int reuse = 1;
    if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
        perror("setsockopt");
        return -1;
    }

    struct sockaddr_in cin;
    cin.sin_family = AF_INET;
    cin.sin_port = htons(C_PORT);
    cin.sin_addr.s_addr = inet_addr(C_IP);
    if(bind(cfd, (struct sockaddr *)&cin, sizeof(cin)) == -1){
        perror("bind");
        return -1;
    }

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(S_PORT);
    sin.sin_addr.s_addr = inet_addr(S_IP);
    if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
        perror("connect");
        return -1;
    }
    printf("connect success\n");

    struct pollfd pfd[2];
    pfd[0].fd = 0;
    pfd[0].events = POLLIN;
    pfd[1].fd = cfd;
    pfd[1].events = POLLIN;

    char rbuf[128] = {0};
    char wbuf[128] = {0};
    while(1){
        int ret = poll(pfd, 2, -1);
        if(ret == -1){
            perror("poll");
            return -1;
        }else if(ret == 0){
            printf("timeout\n");
            return -1;
        }
        if(pfd[0].revents == POLLIN){
            bzero(wbuf, sizeof(wbuf));
            fgets(wbuf, sizeof(wbuf), stdin);
            int len = strlen(wbuf);
            if(wbuf[len-1] == '\n'){
                wbuf[len-1] = 0;
            }
            send(cfd, wbuf, len, 0);
        }
        if(pfd[1].revents == POLLIN){
            bzero(rbuf, sizeof(rbuf));
            int res = recv(cfd, rbuf, sizeof(rbuf), 0);
            if(res == 0){
                printf("offline\n");
                break;
            }
            printf("[%s:%d]:%s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), rbuf);
        }
    }
    return 0;
}                                                                                                  
相关推荐
brhhh_sehe11 分钟前
重生之我在异世界学编程之C语言:深入文件操作篇(下)
android·c语言·网络
李先静12 分钟前
用 gdbserver 调试 arm-linux 上的 AWTK 应用程序
linux·arm开发·awtk
地球空间-技术小鱼13 分钟前
YUM(Yellowdog Updater, Modified)和DNF(Dandified YUM)简介
linux·运维·服务器·笔记·学习
忆源14 分钟前
Linux高级--2.4.2 linux TCP 系列操作函数 -- 深层理解
linux·网络·tcp/ip
Bucai_不才15 分钟前
【C++】初识C++之C语言加入光荣的进化(上)
c语言·c++·面向对象
木向17 分钟前
leetcode22:括号问题
开发语言·c++·leetcode
comli_cn19 分钟前
使用清华源安装python包
开发语言·python
筑基.25 分钟前
basic_ios及其衍生库(附 GCC libstdc++源代码)
开发语言·c++
哈利巴多先生25 分钟前
HTTP,续~
网络·网络协议·http
白了个白i28 分钟前
http的访问过程或者访问页面会发生什么
网络·网络协议·http