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

运行结果:

客户端:

服务器

相关推荐
狐凄几秒前
Python实例题:基于 Flask 的在线聊天系统
开发语言·python
狐凄1 分钟前
Python实例题:基于 Flask 的任务管理系统
开发语言·python
学习编程的gas5 分钟前
Linux开发工具——gcc/g++
linux·运维·服务器
大大。8 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
嵌入式成长家10 分钟前
ubuntu rules 使用规则
linux·ubuntu·rules 使用规则
_可乐无糖17 分钟前
AWS WebRTC: 判断viewer端拉流是否稳定的算法
linux·服务器·webrtc·aws
shootero@126.com18 分钟前
R语言开发记录,一
开发语言·r语言
勤奋的知更鸟21 分钟前
Java 编程之状态模式
java·开发语言·状态模式
沐知全栈开发30 分钟前
R 列表:深入解析与高效应用
开发语言
数据智能老司机31 分钟前
Linux内核编程——Linux设备模型
linux·架构·操作系统