Linux下的简单TCP客户端和服务器

客户端

cpp 复制代码
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>

int main()
{
    struct sockaddr_in* caddr=(struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
    caddr->sin_port = htons(8999);
    inet_pton(AF_INET, "192.168.101.231", &caddr->sin_addr.s_addr);
    caddr->sin_family = AF_INET;

    int cfd = socket(AF_INET, SOCK_STREAM, 0);
    if (cfd == -1)
    {
        perror("socket error");
        return 1;
    }

    int res = connect(cfd, (struct sockaddr*)caddr, sizeof(struct sockaddr));
    if (res == -1)
    {
        perror("connect error");
        return 1;
    }

    while (1)
    {
        char buf[1024] = {'\0'};
        int res = send(cfd, "this is client", strlen("this is client"), 0);
        if (res != -1)
        {
            printf("send:%s\n", "this is client");
        }
        else
        {
            perror("send error");
        }
        res = recv(cfd, buf, 1024, 0);
        if (res == -1)
        {
            perror("recv error");
        }
        else
        {
            printf("client recv:%s\n", buf);
        }
        memset(buf, 0, 1024);
    }

    close(cfd);
    return 0;
}

服务器

cpp 复制代码
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>

int main()
{
    struct sockaddr_in saddr;
    saddr.sin_port = htons(8999);
    saddr.sin_addr.s_addr = INADDR_ANY;
    saddr.sin_family = AF_INET;
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd == -1)
    {
        perror("socket error");
        return 1;
    }
    int bind_res = bind(sfd, (struct sockaddr*)&saddr, sizeof(struct sockaddr_in));
    if (bind_res == -1)
    {
        perror("bind error");
        return 1;
    }
    listen(sfd, 100);
    int size = sizeof(struct sockaddr_in);
    int cfd = accept(sfd, (struct sockaddr*)&saddr, (socklen_t*)&size);
    while (1)
    {


        if (cfd == -1)
        {
            perror("accept error");
            return 1;
        }

        char buf[1024] = {'\0'};
        int res = recv(cfd, buf, 1024, 0);
        if (res != -1)
        {
            printf("recv:%s\n", buf);
        }
        strcat(buf, ": is recved by server");
        res = send(cfd, buf, strlen(buf), 0);
        if (res == -1)
        {
            perror("send error");
        }
        memset(buf, 0, 1024);

    }
    close(cfd);
    close(sfd);
    return 0;
}

运行结果:

客户端:

服务器

相关推荐
肆忆_2 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
chlk1235 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑5 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件6 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
不想写代码的星星6 小时前
虚函数表:C++ 多态背后的那个男人
c++
深紫色的三北六号15 小时前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash19 小时前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI1 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行2 天前
Linux和window共享文件夹
linux
端平入洛2 天前
delete又未完全delete
c++