unp_day04

作业1

题目

多进程服务器

代码

#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define ERR_MSG(msg)                            \
    do {                                        \
        fprintf(stderr, "line:%d\n", __LINE__); \
        perror(msg);                            \
    } while (0)

#define IP "192.168.31.65" 

int cli_msg(int newfd, struct sockaddr_in cin);

void handler(int sig)
{
    while (waitpid(-1, NULL, WNOHANG) > 0)
        ;
}

int main(int argc, const char *argv[])
{
    // 接受子进程结束时的SIGCHLD信号,完成收尸
    __sighandler_t s = signal(SIGCHLD, handler);
    if (SIG_ERR == s) {
        ERR_MSG("signal");
        return -1;
    }
    // 创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0) {
        ERR_MSG("socket");
        return -1;
    }

    // 填充服务器的地址信息结构体
    // 该真实结构体根据地址制定:AF_INET : man 7 ip
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;             // 必须填充AF_INET
    sin.sin_port = htons(8888);           // 端口的网路字节序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 = 0;
    pid_t pid = 0;

    while (1) {
        // 父进程只完成连接
        // 从已完成连接的队列头中获取一个客户端信息,生成一个新的文件描述符
        newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
        if (newfd < 0) {
            ERR_MSG("accept");
            return -1;
        }
        printf("[%s : %d] newfd = %d 连接成功__%d__\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);

        pid = fork();
        if (pid < 0) {
            ERR_MSG("fork");
            return -1;
        } else if (pid > 0) {
            close(newfd);
        } else if (0 == pid) {
            // 子进程只完成传输
            close(sfd);
            cli_msg(newfd, cin);
            exit(0);
        }
    }
    // 关闭文件描述符
    close(sfd);
    return 0;
}

int cli_msg(int newfd, struct sockaddr_in cin)
{
    char buf[1024] = "";
    ssize_t res = 0;
    while (1) {
        // 接收
        memset(buf, 0, sizeof(buf));
        res = recv(newfd, buf, sizeof(buf), 0);
        if (res < 0) {
            ERR_MSG("recv");
            return -1;
        } else if (0 == res) {
            printf("[%s : %d] newfd = %d 客户端下线__%d__\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd,
                   __LINE__);
            break;
        }
        printf("[%s : %d] newfd = %d : %s__%d__\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf, __LINE__);

        // 发送
        strcat(buf, "*_*");
        if (send(newfd, buf, sizeof(buf), 0) < 0) {
            ERR_MSG("send");
            return -1;
        }
        printf("发送成功\n");
    }
}

作业2

题目

多线程服务器

代码

#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#define ERR_MSG(msg)                            \
    do {                                        \
        fprintf(stderr, "line:%d\n", __LINE__); \
        perror(msg);                            \
    } while (0)

#define IP "192.168.1.74" 

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;
    }

    // 填充服务器的地址信息结构体
    // 该真实结构体根据地址制定:AF_INET : man 7 ip
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;             // 必须填充AF_INET
    sin.sin_port = htons(8888);           // 端口的网路字节序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 = 0;
    pthread_t tid;
    struct cli_msg info;

    // 从已完成连接的队列头中获取一个客户端信息,生成一个新的文件描述符
    while (1) {
        newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
        if (newfd < 0) {
            ERR_MSG("accept");
            return -1;
        }
        printf("[%s : %d] newfd = %d 连接成功__%d__\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
        info.newfd = newfd;
        info.cin = cin;

        if (pthread_create(&tid, NULL, deal_cli_msg, (void *)&info) != 0) {
            fprintf(stderr, "line:%d pthread_create failed\n", __LINE__);
            return -1;
        }
        pthread_detach(tid);
    }

    // 关闭文件描述符
    close(newfd);
    close(sfd);
    return 0;
}

void *deal_cli_msg(void *arg)
{
    int newfd = ((struct cli_msg *)arg)->newfd;
    struct sockaddr_in cin = ((struct cli_msg *)arg)->cin;
    char buf[1024] = "";
    ssize_t res = 0;
    while (1) {
        // 接收
        memset(buf, 0, sizeof(buf));
        res = recv(newfd, buf, sizeof(buf), 0);
        if (res < 0) {
            ERR_MSG("recv");
            break;
        } else if (0 == res) {
            printf("[%s : %d] newfd = %d 客户端下线__%d__\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd,
                   __LINE__);
            break;
        }
        printf("[%s : %d] newfd = %d : %s__%d__\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf, __LINE__);

        // 发送
        strcat(buf, "*_*");
        if (send(newfd, buf, sizeof(buf), 0) < 0) {
            ERR_MSG("send");
            break;
        }
        printf("发送成功\n");
    }
    close(newfd);
    pthread_exit(NULL);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值