tcp socket 通信

服务端

//基于tcp的服务器
#include<stdio.h>
#include<ctype.h> // toupper()
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>

int main(void){
    printf("服务器:创建套接字\n");
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1){
        perror("socket");
        return -1;
    }
    printf("服务器:准备地址结构\n");
    struct sockaddr_in ser;
    ser.sin_family = AF_INET;
    ser.sin_port = htons(8080);
    ser.sin_addr.s_addr = inet_addr("192.168.101.132"); // ip 自己设置
    printf("服务器:绑定套接字和地址结构\n");
    if(bind(sockfd,(struct sockaddr*)&ser,sizeof(ser)) == -1){
        perror("bind");
        return -1;
    }
    printf("服务器:启动侦听\n");
    if(listen(sockfd,1024) == -1){
        perror("listen");
        return -1;
    }
    
    printf("服务器:等待客户端的连接请求\n");
    struct sockaddr_in cli;//用来输出客户端的地址结构信息
    socklen_t len = sizeof(cli);//用来输出地址结构的大小
    int conn = accept(sockfd,(struct sockaddr*)&cli,&len);
    if(conn == -1){
            perror("accept");
            return -1;
    }
    printf("服务器:接收到%s:%hu的客户端的连接\n",
        inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));

    printf("服务器:业务处理\n");
    for(;;){
        //接收客户端发来的小写的串
        char buf[64] = {};
        ssize_t size = read(conn,buf,sizeof(buf)-1);
        if(size == -1){
            perror("read");
            return -1;
        }
        if(size == 0){
            // 客户端关闭套接字
            break;
        }
        //转大写
        for(int i = 0;i < strlen(buf);i++){
            buf[i] = toupper(buf[i]);
        }
        //将大写的串回传个客户端
        if(write(conn,buf,strlen(buf)) == -1){
            perror("write");
            return -1;
        }
    }
    //关闭通信套接字
    close(conn);
    close(sockfd);
    return 0;
}

客户端

//基于tcp的服务器
#include<stdio.h>
#include<ctype.h> // toupper()
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>

int main(void){
    printf("服务器:创建套接字\n");
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1){
        perror("socket");
        return -1;
    }
    printf("服务器:准备地址结构\n");
    struct sockaddr_in ser;
    ser.sin_family = AF_INET;
    ser.sin_port = htons(8080);
    ser.sin_addr.s_addr = inet_addr("192.168.101.132");
    printf("服务器:绑定套接字和地址结构\n");
    if(bind(sockfd,(struct sockaddr*)&ser,sizeof(ser)) == -1){
        perror("bind");
        return -1;
    }
    printf("服务器:启动侦听\n");
    if(listen(sockfd,1024) == -1){
        perror("listen");
        return -1;
    }
    
    printf("服务器:等待客户端的连接请求\n");
    struct sockaddr_in cli;//用来输出客户端的地址结构信息
    socklen_t len = sizeof(cli);//用来输出地址结构的大小
    int conn = accept(sockfd,(struct sockaddr*)&cli,&len);
    if(conn == -1){
            perror("accept");
            return -1;
    }
    printf("服务器:接收到%s:%hu的客户端的连接\n",
        inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));

    printf("服务器:业务处理\n");
    for(;;){
        //接收客户端发来的小写的串
        char buf[64] = {};
        ssize_t size = read(conn,buf,sizeof(buf)-1);
        if(size == -1){
            perror("read");
            return -1;
        }
        if(size == 0){
            // 客户端关闭套接字
            break;
        }
        //转大写
        for(int i = 0;i < strlen(buf);i++){
            buf[i] = toupper(buf[i]);
        }
        //将大写的串回传个客户端
        if(write(conn,buf,strlen(buf)) == -1){
            perror("write");
            return -1;
        }
    }
    //关闭通信套接字
    close(conn);
    close(sockfd);
    return 0;
}

 

posted @ 2025-03-13 22:21  靖意风  Views(16)  Comments(0)    收藏  举报