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

运行结果:

客户端:

服务器

相关推荐
曦樂~10 小时前
Cpp面筋
c语言·c++
道尔柯南11 小时前
C++ string 类详解:从常用接口到深拷贝,彻底掌握字符串处理
android·java·c++
来让爷抱一个12 小时前
Python 异步里混用同步库,事件循环被卡住的全过程
开发语言·python
Linux-lucky12 小时前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu
禁默12 小时前
启元 Q1 二次开发实践:Ubuntu 部署与仿真验证,从 PrimeConsole 到 ROS 2 Humble + RViz
linux·运维·ubuntu
skywalk816312 小时前
把 去也网站 从 Ubuntu 搬到 FreeBSD:一次 Service Jail + nginx 的部署实录
linux·运维·nginx·ubuntu·freebsd
十二14 小时前
【洛谷题解】P1083 [NOIP 2012 提高组] 借教室(二分答案 + 差分)
数据结构·c++·算法·蓝桥杯
心祭丫丫14 小时前
【每天一个 Python 小案例17】Python 实现通讯录
开发语言·人工智能·笔记·python·学习
爱莉希雅&&&14 小时前
GitLab 完整学习笔记
linux·运维·服务器
风笙97815 小时前
[文件系统]文件系统挂载与卸载详解
linux