1. 服务器主程序

#include "../apue.h"

void handler(int sig){
        printf("recv sig=%d\n", sig);
        exit(EXIT_SUCCESS);
}

int main(void){
        int listenfd;
        listenfd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(listenfd < 0)
                err_exit("listen error");

        struct sockaddr_in servaddr;
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(5188);
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        //servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        //inet_aton("127.0.0.1", $servaddr.sin_addr);

        int on=1;
        if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,&on, sizeof(on))<0)
                err_exit("setsockopt error");

        if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr))<0)
                err_exit("bind error");

        if(listen(listenfd, SOMAXCONN)<0)
                err_exit("listen error");

        struct sockaddr_in peeraddr;
        socklen_t peerlen = sizeof(peeraddr);
        int conn;
        if((conn = accept(listenfd, (struct sockaddr*)&peeraddr, &peerlen))<0)
                err_exit("accept error");
        printf("ip=%s,port=%d\n", inet_ntoa(peeraddr.sin_addr), peeraddr.sin_port);

        pid_t pid;
        pid = fork();
    char recvbuf[1024];
    char sendbuf[1024] = {0};
    int ret;
        if(pid==-1)
                err_exit("fork error");
        else if(pid==0){
                while(fgets(sendbuf, sizeof(sendbuf), stdin) != NULL){
                        write(conn, sendbuf, sizeof(sendbuf));
                        memset(sendbuf, 0, sizeof(sendbuf));
                }
                kill(pid, SIGUSR1);
                exit(EXIT_SUCCESS);

        }else{
                signal(SIGUSR1, handler);
        while (1){
            memset(recvbuf, 0, sizeof(recvbuf));
            ret = read(conn, recvbuf, sizeof(recvbuf));
                        if(ret==-1)
                                err_exit("read error");
                        else if(ret==0){
                                printf("peer close\n");
                                break;
                        }
            fputs(recvbuf, stdout);
        }
                close(listenfd);
        }
        return 0;
}

 

2. 客户端程序

#include "../apue.h"

int main(void){
        int socketfd;
        socketfd = socket(AF_INET, SOCK_STREAM, 0);
        if(socketfd == -1)
                err_exit("socket error");

        struct sockaddr_in clientaddr;
        memset(&clientaddr, 0, sizeof(clientaddr));
        clientaddr.sin_family = AF_INET;
        clientaddr.sin_port = htons(5188);
        clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

        int conn;
        conn = connect(socketfd, (struct sockaddr*)&clientaddr, sizeof(clientaddr));
        if(conn == -1)
                err_exit("connect error");
        char recvbuf[1024] = {0};
    char sendbuf[1024] = {0};
        int ret;
        pid_t pid=fork();
        if(pid==-1)
                err_exit("fork error");
        else if(pid==0){
                while(fgets(sendbuf, sizeof(sendbuf), stdin) != NULL){
                        write(socketfd, sendbuf, sizeof(sendbuf));
                        memset(sendbuf, 0, sizeof(sendbuf));
                }
                exit(EXIT_SUCCESS);

        }else{
        while (1){
                        ret=read(socketfd, recvbuf, sizeof(recvbuf));
                        if(ret==-1)
                                err_exit("read error");
                        else if(ret==0){
                                printf("server close\n");
                                break;
                        }
                        fputs(recvbuf,stdout);

        }
                close(socketfd);
        }
        return 0;
}