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

结果:

相关推荐
止观止3 小时前
C++20 Concepts:让模板错误信息不再“天书”
c++·c++20·编程技巧·模板编程·concepts
n***s9093 小时前
IP地址、子网掩码(NETMASK)和网关(Gateway)
tcp/ip·gateway·智能路由器
java_logo3 小时前
MySQL Server Docker 容器化部署指南
linux·运维·数据库·docker·容器
FL16238631293 小时前
ONNX RuntimeC++ 静态库下载安装和使用教程
开发语言·c++
誰能久伴不乏3 小时前
Linux文件套接字AF_UNIX
linux·服务器·c语言·c++·unix
a41324473 小时前
如何解决centos上oracle连接问题
linux·oracle·centos
h***34633 小时前
在linux(Centos)中Mysql的端口修改保姆级教程
linux·mysql·centos
星释3 小时前
Rust 练习册 97:Run-Length Encoding 压缩算法
java·linux·rust
2509_940880223 小时前
Linux(CentOS)安装 MySQL
linux·mysql·centos