自学内容网 自学内容网

2024.02.29作业

1. TCP模型

server

#include "test.h"

#define SER_IP "192.168.191.128"
#define SER_PORT 9999

int main(int argc, char const *argv[])
{
    int sfd = -1;
    sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sfd)
    {
        perror("socket error");
        return -1;
    }
    printf("sfd = %d\n", sfd);

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

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

    struct sockaddr_in cin;
    socklen_t socklen = sizeof(cin);

    int newfd = accept(sfd, (struct sockaddr*)&cin, &socklen);
    if (newfd == -1)
    {
        perror("accept error");
        return -1;
    }

    printf("newfd = %d\n", newfd);
    printf("IP: %s, port: %d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));

    char rbuf[128] = "";
    while (1)
    {
        bzero(rbuf, sizeof(rbuf));

        int res = read(newfd, rbuf, sizeof(rbuf));
        if (res == 0)
        {
            printf("客户端下线\n");
            break;
        }

        printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), rbuf);

        strcat(rbuf, "*_*");
        write(newfd, rbuf, strlen(rbuf));
    }

    close(sfd);
    close(newfd);

    return 0;
}

client

#include "test.h"

#define SER_IP "192.168.191.128"
#define SER_PORT 9999

int main(int argc, char const *argv[])
{
    int sfd = -1;
    sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sfd)
    {
        perror("socket error");
        return -1;
    }
    printf("sfd = %d\n", sfd);

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

    char wbuf[128] = "";
    while (1)
    {
        bzero(wbuf, sizeof(wbuf));

        printf("请输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf) - 1] = 0;

        send(sfd, wbuf, strlen(wbuf), 0);
        printf("发送成功\n");

        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }

        bzero(wbuf, sizeof(wbuf));
        recv(sfd, wbuf, sizeof(wbuf), 0);
        printf("收到的消息: %s\n", wbuf);
    }

    close(sfd);

    return 0;
}

2. UDP模型

server

#include "test.h"

#define SER_IP "192.168.191.128"
#define SER_PORT 9999

int main(int argc, char const *argv[])
{
    int sfd = -1;
    sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == sfd)
    {
        perror("socket error");
        return -1;
    }
    printf("sfd = %d\n", sfd);

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(SER_PORT);
    sin.sin_addr.s_addr = inet_addr(SER_IP);
    if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");

    struct sockaddr_in cin;
    socklen_t socklen = sizeof(cin);
    char rbuf[128] = "";
    while (1)
    {
        bzero(rbuf, sizeof(rbuf));

        recvfrom(sfd, rbuf, sizeof(rbuf), 0, (struct sockaddr*)&cin, &socklen);

        printf("收到消息: %s\n", rbuf);

        strcat(rbuf, "*_*");
        if (sendto(sfd, rbuf, sizeof(rbuf), 0, (struct sockaddr*)&cin, socklen) == -1)
        {
            perror("sendto error");
            return -1;
        }
    }

    close(sfd);

    return 0;
}

client

#include "test.h"

#define SER_IP "192.168.191.128"
#define SER_PORT 9999

int main(int argc, char const *argv[])
{
    int sfd = -1;
    sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == sfd)
    {
        perror("socket error");
        return -1;
    }
    printf("sfd = %d\n", sfd);

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(SER_PORT);
    sin.sin_addr.s_addr = inet_addr(SER_IP);

    char wbuf[128] = "";
    while (1)
    {
        bzero(wbuf, sizeof(wbuf));

        printf("请输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf) - 1] = 0;

        sendto(sfd, wbuf, strlen(wbuf), 0, (struct sockaddr*)&sin, sizeof(sin));
        printf("发送成功\n");

        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }

        bzero(wbuf, sizeof(wbuf));
        recvfrom(sfd, wbuf, sizeof(wbuf), 0, NULL, NULL);
        printf("收到的消息: %s\n", wbuf);
    }

    close(sfd);

    return 0;
}

思维导图


原文地址:https://blog.csdn.net/lkjhxx/article/details/136384121

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!