• 简单unix 局域网的TCP会话


    client.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <time.h>
    
    #define maxn 101
    /**
    服务器端
    1>建立一个基于TCP的流套接字描述符
    2>将套接字描述符绑定到本地地址和本地端口上
    3>监听客户端的请求
    4>通过Accept接收客户端的请求
    5>发送数据/接收数据
    6>关闭套接字
    客户端
    1>与服务器端建立链接
    2> 发送数据/接收数据
    3>关闭套接字
    **/
    int main(int  argc, char** argv)
    {
        int s_socket ;
        //1>建立一个基于TCP的流套接字描述符
        if((s_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
            herror("socket");
            exit(errno);
        }
        else
        {
            printf("socket create success!
    ");
        }
        //端口号
        unsigned int port;
        if(argv[2]) {
            port = atoi(argv[2]);
        }
        else {
            port = 1111;
        }
        //2>将套接字描述符绑定到本地地址和本地端口上
        struct sockaddr_in  addport;
        bzero(&addport, sizeof(addport));
        addport.sin_family = AF_INET;
        //addport.sin_addr.s_addr = inet_addr(argv[1]);
        addport.sin_port = htons(port);
        if(inet_aton(argv[1], (struct in_addr*)&addport.sin_addr.s_addr) == -1)
        {
            herror("port");
            exit(errno);
        }
        else
        {
            printf("IP and port  create success!
    ");
        }
        //建立链接
        if(connect(s_socket, (struct sockaddr*)&addport, sizeof(addport)) == -1)
        {
            herror("connect");
            exit(errno);
        }
        else
        {
            printf("connect   success
    ");
        }
        //3>监听客户端的请求
        char buf[maxn];
        char  buf1[maxn];
        int flag = 1;
        while(1)
        {
            //接收信息
            flag = 1;
            bzero(buf, maxn);
            int len;
            len = recv(s_socket, buf, maxn, 0);
            if(len > 0)
            {
                printf("服务器发来的信息是:  %s
    ", buf);
            }
            else
            {
                if(len == 0)
                {
                    printf("server is closed!
    ");
                    flag = 0;
                    break;
                }
                else if(len == -1)
                {
                    printf("receive message failure!");
                }
            }
        loop:
            if(flag == 0) break;
            bzero(buf, maxn);
            printf("请输入要发送的信息:");
            fgets(buf, maxn, stdin);
            if(strncasecmp(buf, "exit", 4) == 0)
            {
                printf("聊天中止
    ");
                break;
            }
            if(strncmp(buf, "
    ", 1) == 0)
            {
                printf("空字符
    ");
                continue;
            }
            int L_len = strlen(buf);
            if(buf[L_len - 1] == '
    ')
            {
                len = send(s_socket, buf, L_len - 1, 0);
            }
            else
            {
                len = send(s_socket, buf, L_len, 0);
            }
            if(len > 0)
            {
                printf("send message success!
    ");
            }
            else
            {
                printf("send message failure!
    ");
                break;
            }
        }
        close(s_socket);
        return 0;
    }
    View Code

    server.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <time.h>
    
    #define maxn 101
    /**
    服务器端
    1>建立一个基于TCP的流套接字描述符
    2>将套接字描述符绑定到本地地址和本地端口上
    3>监听客户端的请求
    4>通过Accept接收客户端的请求
    5>发送数据/接收数据
    6>关闭套接字
    客户端
    1>与服务器端建立链接
    2> 发送数据/接收数据
    3>关闭套接字
    **/
    int main(int  argc, char** argv)
    {
        int s_socket, sock_port ;
        //1>建立一个基于TCP的流套接字描述符
        if((s_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
            herror("socket");
            exit(errno);
        }
        else
        {
            printf("socket create success!
    ");
        }
        //端口号
        unsigned int port;
        if(argv[2]) {
            port = atoi(argv[2]);
        }
        else {
            port = 1111;
        }
        if(argv[3])
        {
            sock_port = atoi(argv[3]);
        }
        else
        {
            sock_port = 3;
        }
        //2>将套接字描述符绑定到本地地址和本地端口上
        struct sockaddr_in  c_addport, s_addport;
        bzero(&s_addport, sizeof(s_addport));
        s_addport.sin_family = AF_INET;
        //s_addport.sin_addr.s_addr = inet_addr(argv[1]);
        s_addport.sin_port = htons(port);
        if(argv[1])
        {
            s_addport.sin_addr.s_addr = inet_addr(argv[1]);
        }
        else
        {
            s_addport.sin_addr.s_addr = INADDR_ANY;
        }
        if((bind(s_socket, (struct sockaddr*) &s_addport, sizeof(struct sockaddr))) == -1) {
            perror("bind");
            exit(errno);
        }
        else {
            printf("bind success!
    ");
        }
        //3>监听客户端的请求
        if(listen(s_socket, sock_port) == -1)
        {
            herror("listen");
            exit(errno);
        }
        else
        {
            printf("server is listening!
    ");
        }
        while(1)
        {
            int  flag;
            socklen_t len;
            len = sizeof(struct sockaddr);
            flag = accept(s_socket, (struct sockaddr*)&c_addport, &len);
            if(flag == -1)
            {
                herror("accept");
                exit(errno);
            }
            else
            {
                printf("Accept success!
    ");
                printf("现在监听的客户端IP是 :%s端口是%d
    ", inet_ntoa(c_addport.sin_addr), ntohl(c_addport.sin_port));
            }
            char buf[maxn];
            while(1)
            {
                //发送消息
            loop:
                bzero(buf, maxn);
                printf("请输入要发送的信息:");
                fgets(buf, maxn, stdin);
                int L_len = strlen(buf);
                int Index ;
                if(strncasecmp(buf, "exit", 4) == 0)
                {
                    printf("chatting  be ended !
    ");
                    break;
                }
                if(strcmp(buf, "
    ") == 0)
                {
                    printf("空字符
    ");
                    goto loop;
                }
                if(buf[L_len - 1] == '
    ')
                {
                    Index = send(flag, buf, L_len - 1, 0);
                }
                else
                {
                    Index = send(flag, buf, L_len, 0);
                }
                if(Index == -1)
                {
                    printf("send message failure!");
                }
                else
                {
                    printf("send message success
    ");
                }
                //接收消息
                bzero(buf, maxn);
                int LL;
                LL = recv(flag, buf, maxn, 0);
                if(LL > 0)
                {
                    printf("从客户端接收的信息是%s
    ", buf);
                }
                else
                {
                    if(LL < 0)
                    {
                        printf("Receive message failure!
    ");
                    }
                    else
                    {
                        printf("客户端退出,聊天中止
    ");
                        break;
                    }
                }
            }
            close(flag);
            printf("是否结束进程?Y->是:N->否");
            bzero(buf, maxn);
            fgets(buf, maxn, stdin);
            if(strncasecmp(buf, "Y", 1) == 0)
            {
                printf("结束进程
    ");
                break;
            }
            else {
                printf("waiting ..........
    ");
            }
        }
        close(s_socket);
        return 0;
    }
    View Code

     

    应用少.....更新中。。。。。。

  • 相关阅读:
    mysql用 法like concat()
    redis系列之数据库与缓存数据一致性解决方案
    day33:进程锁&事件&进程队列&进程间共享数据
    day32:进程&进程join&守护进程deamon
    day31:socketserver&hashlib&hmac&TCP登录
    day30:TCP&UDP:socket
    day29:计算机网络概念
    小程序3:ATM小程序
    hdu 6867 Tree 2020 Multi-University Training Contest 9 dfs+思维
    Codeforces Round #660 (Div. 2) Captain Flint and Treasure 拓扑排序(按照出度、入读两边拓扑排序)
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4980141.html
Copyright © 2020-2023  润新知