Linux c++开发-11-Socket TCP编程简单案例

服务端:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>

#include <errno.h>


int main(void)
{
       
        //1.socket
        int server_sock = socket(AF_INET, SOCK_STREAM, 0);

        // address
        struct sockaddr_in server_addr;
        server_addr.sin_addr.s_addr = INADDR_ANY;
        server_addr.sin_port = htons(9001);
        server_addr.sin_family = AF_INET; 

        //2.bind
        if ( (bind(server_sock, (struct sockaddr*) &server_addr, sizeof(server_addr))) != 0 )
        {
                fprintf(stderr, "couldn't bind: %s\n", strerror(errno));
                exit(EXIT_FAILURE);

        }

        //3.listen
        if ((listen(server_sock, 10)) != 0){
                fprintf(stderr, "couldn't listen: %s\n", strerror(errno));
                exit(EXIT_FAILURE);
        }

        int client_sock;
        //4.accept
        if( (client_sock = accept(server_sock, NULL, NULL)) == -1 ){
                fprintf(stderr, "couldn't accept: %s\n", strerror(errno));
                exit(EXIT_FAILURE);
        }
        char send_message[255] = "hello i am server";
        send(client_sock, send_message, sizeof(send_message), 0);

        char buf[256];
        recv(client_sock,buf,sizeof(buf),0);

        printf("Server recv:%s\n",buf);

        close(server_sock);
        close(client_sock);

        return 0;
}

客户端:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#include <netinet/in.h>

#include <errno.h>

int main(void){
        //1.create sock
        int sock = socket(AF_INET, SOCK_STREAM, 0);
        if(sock < 0){
                fprintf(stderr, "can't create socket because of %s\n", strerror(errno));
                exit(EXIT_FAILURE);
        }

        //create address
        struct sockaddr_in server_addr;
        server_addr.sin_addr.s_addr = INADDR_ANY;
        server_addr.sin_port = htons(9001);
        server_addr.sin_family = AF_INET;

        //connect
        if( (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr))) == -1  ){
                perror("connect error because of: ");
                close(sock);
                exit(EXIT_FAILURE);

        }

        //create recv buffer and recv
        char recv_buffer[255];
        recv(sock, recv_buffer, sizeof(recv_buffer), 0);

        //print
        printf("printf recv: %s\n", recv_buffer);

        char send_message[255] = "I Love China!";
        send(sock, send_message, sizeof(send_message), 0);

        close(sock);
        return 0;
}

结果:

相关推荐
VVVVWeiYee1 小时前
IP v 6
网络·网络协议·tcp/ip
‍。。。1 小时前
ubuntu 安装 vllm
linux·运维·ubuntu
xiao-xiang2 小时前
zookeeper-集群扩缩容
linux·分布式·zookeeper·中间件·注册中心·zk
zzz100663 小时前
Shell 编程基础(续):流程控制与实践
linux·运维·服务器
蒲公英的孩子3 小时前
Linux下OpenRadioss源码编译安装及使用
linux·分布式·开源软件
三坛海会大神5554 小时前
《Shell 大道:筑基篇(下)—— 流控筑根基,数据任驱驰》
linux·运维·服务器·bash
蟾宫曲5 小时前
网络编程 04:TCP连接,客户端与服务器的区别,实现 TCP 聊天及文件上传,Tomcat 的简单使用
java·服务器·网络·tcp/ip·tomcat·端口
闻缺陷则喜何志丹5 小时前
【分治法 BFS 质因数分解】P12255 [蓝桥杯 2024 国 Java B] 园丁|普及+
c++·算法·蓝桥杯·宽度优先·质因数分解·分治法
寒冬没有雪5 小时前
按对角线进行矩阵排序
c++·算法
Huangichin5 小时前
C++基础算法——贪心算法
c++·算法·贪心算法