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

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值