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

运行结果:

客户端:

服务器

相关推荐
烂蜻蜓20 分钟前
前端已死?什么是前端
开发语言·前端·javascript·vue.js·uni-app
老猿讲编程23 分钟前
安全C语言编码规范概述
c语言·开发语言·安全
OrangeJiuce26 分钟前
【QT中的一些高级数据结构,持续更新中...】
数据结构·c++·qt
人工干智能2 小时前
科普:你的笔记本电脑中有三个IP:127.0.0.1、无线网 IP 和局域网 IP;两个域名:localhost和host.docker.internal
网络协议·tcp/ip·电脑
程序员-King.3 小时前
【接口封装】——13、登录窗口的标题栏内容设置
c++·qt
萌の鱼3 小时前
leetcode 2826. 将三个组排序
数据结构·c++·算法·leetcode
Biomamba生信基地4 小时前
两天入门R语言,周末开讲
开发语言·r语言·生信
RAN_PAND4 小时前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
qwy7152292581634 小时前
13-R数据重塑
服务器·数据库·r语言
Bio Coder4 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言