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

结果:

相关推荐
jrrz08289 分钟前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
二十雨辰18 分钟前
[linux]docker基础
linux·运维·docker
咖啡里的茶i24 分钟前
Vehicle友元Date多态Sedan和Truck
c++
城南vision28 分钟前
计算机网络——TCP篇
网络·tcp/ip·计算机网络
海绵波波10730 分钟前
Webserver(4.9)本地套接字的通信
c++
@小博的博客36 分钟前
C++初阶学习第十弹——深入讲解vector的迭代器失效
数据结构·c++·学习
饮浊酒1 小时前
Linux操作系统 ------(3.文本编译器Vim)
linux·vim
lihuhelihu1 小时前
第3章 CentOS系统管理
linux·运维·服务器·计算机网络·ubuntu·centos·云计算
矛取矛求1 小时前
Linux系统性能调优技巧
linux
One_Blanks2 小时前
渗透测试-Linux基础(1)
linux·运维·安全