美文网首页
echo-server-client

echo-server-client

作者: Jesson3264 | 来源:发表于2021-06-13 09:06 被阅读0次
    /* 
     * @file   client.c
     * @author: jesson3264@163.com
     *
     * @time 2021年6月13日
    */
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <string.h>
    #include <errno.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
     
    #define SERVERIP "192.168.8.103"
    #define SERVERPORT 12345
    #define MAXBUFFER 256
     
    int main(int argc, char** argv)
    {  
        int clientFd,ret;
        struct sockaddr_in serveraddr;
        char buf[MAXBUFFER];
        clientFd=socket(AF_INET,SOCK_STREAM,0);//创建socket
        if (clientFd < 0)
        {
            printf("socket error:%s\n",strerror(errno));
            exit(-1);
        }
    
        bzero(&serveraddr, sizeof(serveraddr));
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_port = htons(SERVERPORT);
        inet_pton(AF_INET, SERVERIP, &serveraddr.sin_addr);
        
        ret = connect(clientFd, (struct sockaddr *)&serveraddr,sizeof(serveraddr));//连接到服务器
        if (ret != 0)
        {
            close(clientFd);
            printf("connect error:%s\n",strerror(errno));
            exit(-1);
        }
    
        while(1)
        {
                bzero(buf,sizeof(buf));
                scanf("%s",buf);
                write(clientFd,buf,sizeof(buf));//写数据
                bzero(buf,sizeof(buf));
                read(clientFd,buf,sizeof(buf));//读数据
                printf("echo:%s\n",buf);
            }
        close(clientFd);
        return (EXIT_SUCCESS);
    }
    
    
    // server
    /* 
     * @file:   Server.c
     * @author: jesson3264@163.com
     *
     * 
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <string.h>
    #include <errno.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    #define SERVERIP "192.168.8.103"
    #define SERVERPORT 12345
    #define MAXBUFFER 256
    
    int main(int argc, char **argv)
    {
        char readBuf[MAXBUFFER] = {0};
        int serverFd, connfd, ret;
        socklen_t len;
        struct sockaddr_in serveraddr, clientaddr;
        
        char ip[40] = {0};
        serverFd = socket(AF_INET, SOCK_STREAM, 0); //创建socket
        if (serverFd < 0)
        {
            printf("socket error:%s\n", strerror(errno));
            exit(-1);
        }
    
        bzero(&serveraddr, sizeof(serveraddr));
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_port = htons(SERVERPORT);
        //serveraddr.sin_addr.s_addr = INADDR_ANY;
    
        inet_pton(AF_INET, SERVERIP, &serveraddr.sin_addr);
        //将c语言字节序转换为网络字节序
        ret = bind(serverFd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); //绑定IP和端口
        if (ret != 0)
        {
            close(serverFd);
            printf("bind error:%s\n", strerror(errno));
            exit(-1);
        }
    
        ret = listen(serverFd, 5); //监听
        if (ret != 0)
        {
            close(serverFd);
            printf("listen error:%s\n", strerror(errno));
            exit(-1);
        }
    
        len = sizeof(clientaddr);
        bzero(&clientaddr, sizeof(clientaddr));
        while (1)
        {
            connfd = accept(serverFd, (struct sockaddr *)&clientaddr, &len); //接受客户端的连接
            printf("%s 连接到服务器 \n", inet_ntop(AF_INET, &clientaddr.sin_addr, ip, sizeof(ip)));
            if (serverFd < 0)
            {
                printf("accept error : %s\n", strerror(errno));
                continue;
            }
    
            while ((ret = read(connfd, (void *)readBuf, MAXBUFFER))) //读客户端发送的数据
            {
                fprintf(stdout, "recv:%s\n", readBuf);
                write(connfd, readBuf, MAXBUFFER); //写回客户端;
                bzero(readBuf, MAXBUFFER);
            }
    
            if (ret == 0)
            {
                printf("客户端关闭连接\n");
            }
            else
            {
                printf("read error:%s\n", strerror(errno));
            }
            close(connfd);
        }
        close(serverFd);
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:echo-server-client

          本文链接:https://www.haomeiwen.com/subject/rmbweltx.html