回显客户端-服务器发送消息无法正常工作

问题描述

我已经从unix网络编程的书中实现了echo客户服务器,我感兴趣的是从client-> server发送msg的client_msg函数,还有将消息回显给客户端的str_echo很好,这是有代码

但在我使用read的升级功能之前:

ssize_t
readLine(int fd,void* str,size_t len)
{
    size_t nleft = len ;
    ssize_t nread;
    char *ptr = (char*)str,c;
    int i;
    for( i = 1 ; i <= len ; i++ )       // for(char* ptr ; *ptr ; ptr++) for pointers and tables .
    {
        again :
        if((nread = read(fd,&c,1)) == 1)
        {

            *ptr++ = c ;
            if( c == '\n')
            {
                break;      // end of line .
            }

        }
        else if(nread == 0)
        {
            *ptr = 0 ;
            return i-1;             // EOF .
        }
        else
        {
            if(errno == EINTR)
                goto again ;
            return (-1);
        }
    }

    *ptr = 0;
    return i;
}

/ ============================================== =============== /

void
cli_msg(int fd)
{
    char msg[MAXLINE],msgTmp[MAXLINE*2];
    ssize_t n;
    const char EOS[5] = "exit";


    while( fgets(msg,MAXLINE,stdin) != NULL)
    {
        if(printf("test exit word %s->%s|\n",EOS,msg) && !strncmp(EOS,msg,sizeof EOS -1))
        {
            printf("in the if statement strncmp\n");
            break;
        }
        
        if( (n = writen(fd,strlen(msg)) > 0))
            printf("data is sent --------------------------------------->\n");
        else
        {
            printf("WRITE ERROR : %s\n",strerror(errno) );
            exit(EXIT_FAILURE);
        }




        if((n = readLine(fd,MAXLINE)) <= 0 )    // the error is that readn doesn't work .
        {
            printf("READLINE ERROR : %s ",strerror(errno));
            exit(0);
        }

        puts(msg);

    }

    return ;
}

/ ============================================ ========= / str_echo:

void str_echo(int fd)
{
    char buffer[MAXLINE];
    int nread;
        again:
        while((nread = readLine(fd,buffer,MAXLINE)) > 0 )
        {
            printf("data is caught  -> %s\n",buffer);
            writen(fd,strlen(buffer));
        }

        if(!nread)
            printf("client exit prematurly ...\n");
        
        /*else if(nread < 0 && errno == EINTR)  // read process interrupted
            goto again;*/
        else if(nread < 0)
        {
            printf("READ ERROR : %s\n",strerror(errno));
            exit(0);
        }

    return;
}

问题是当我写的字符串的大小大于MAXLINE(MAXLINE = 10)时,客户端和服务器块(我想当服务器捕获消息时,它会寻找\ 0,但找不到它阻塞,并且客户端阻塞了对read的调用) 我该如何解决这个问题,并使程序在stdin中分配缓冲区,并根据MAXLINE值tnx世界进行调整:

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)