如何比较客户端在服务器C编程中发送的数据

时间:2013-03-23 18:40:18

标签: c strcmp

我想从客户端向服务器发送一个选项。服务器必须进行一些比较并将字符串发送回客户端。我可以从客户端接收数据,但我的服务器在比较时崩溃(它通过“错误输入”到达else并结束连接)。有人能告诉我我做错了吗?

  

错误的Inputclose错误:错误的文件描述符

     

程序退出,代码为01。

服务器:

void
result(int sockfd)
{
    ssize_t     n;
    char        buf[MAXLINE];
    int         temp;
    time_t      ticks;
again:
    while ((n =read(sockfd, buf, MAXLINE)> 0))
    {
     temp = rand() % 22;
     if (buf=="A\n" || buf=="a\n")
     {
      snprintf(buf, sizeof(buf), "You option is number is -%i, time is %.24s\r\n", temp,ctime(&ticks));
      Writen(sockfd, buf, n);
     }
     if (buf=="B\n" || buf=="f\")
     {
      snprintf(buf, sizeof(buf), "You option is number is -%i, time is %.24s\r\n", temp,ctime(&ticks));
      Writen(sockfd, buf, n);
     }
     else
     {
       printf("Incorrect Input");
       Close(sockfd);
     }  
    }
    if (n < 0 && errno == EINTR)
    goto again;
    else if (n < 0)
        err_sys("read error");
}

int
main(int argc, char **argv)
{
    int         listenfd, connfd;
    socklen_t       len;
    struct sockaddr_in  servaddr, cliaddr;
    char            buff[MAXLINE];
        char            recvline[MAXLINE + 1];

    listenfd = Socket(AF_INET, SOCK_STREAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family      = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);/*----------------------------------------------------*/
    servaddr.sin_port        = htons(5678); 

    Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
    Listen(listenfd, LISTENQ);
    printf("EDMTS is running on 129.128.4.80, listening on port 5678\n");
    printf("\n");
    printf("Waiting for incoming connections...Press Ctrl+C to end server\n");

    for ( ; ; )
         {
        len = sizeof(cliaddr);
        connfd = Accept(listenfd, (SA *) &cliaddr, &len);

        /*Client connects to server*/
        printf("\n");
        printf("Connection from %s, port %d\n",
               Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
               ntohs(cliaddr.sin_port));

                result(connfd);
                Close(connfd);
        printf("Conexion cerrada...Esperando siguiente cliente\n");
        }
}

客户端

void
get_temp(FILE *fp, int sock)
{
    char    sendline[MAXLINE], recvline[MAXLINE];

    while (Fgets(sendline, MAXLINE, fp) != NULL) {

        Writen(sock, sendline, strlen(sendline));

        if (Readline(sock, recvline, MAXLINE) == 0)
            err_quit("Server Terminated Prematurely");

        Fputs(recvline, stdout);
    }
}


int
main(int argc, char **argv)
{
    int                 sockfd, n;
    socklen_t           len;
    char                recvline[MAXLINE + 1];
    struct sockaddr_in  servaddr, cliaddr;
    char  scale[2];

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        err_sys("socket error");

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port   = htons(atoi(argv[2])); 
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) 
        err_quit("inet_pton error for %s", argv[1]);

    printf("Connect...\n");
    if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
        err_sys("connect error");

    len = sizeof(cliaddr);
    Getsockname(sockfd, (SA *) &cliaddr, &len);
    printf("Local Address is: %s\n",
           Sock_ntop((SA *) &cliaddr, sizeof(cliaddr))); 

    printf("Enter option A or B): ");
    get_temp(stdin, sockfd);

    exit(0);
}

当我运行调试器找出我的变量时,我发现:

  

断点1,结果(sockfd = 7)在servertest.c:236 236
  printf(“Random es%i \ n”,temp);   (gdb)p buf   $ 1 =“A \ n”,'\ 0',“\ 203 \217 ”,'\ 0',   “H \ 206H \ 206 \ 204 \ 210#\ 000 \ 000 \ 000 \ 000 \ 206߷\ 020 \ 207# ”   '\ 0',   “ \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \000 \017 ”,'\ 0',“h \206 ”,' \ 0',   “\ 001 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \000 \207 ”,'\ 0',   “ \ t \ 000 \ 000 \ 000# \030 \ 000 \ 000 \ 000 \ 000h \206 \ 003”,'\ 0'   ,“\ n \ 000 \ 000 \ 000g \206 ”,'\ 0',   “”......   (gdb)p temp   $ 2 = 17

非常感谢

2 个答案:

答案 0 :(得分:1)

首先,您无法使用==比较C字符串(您将比较指针地址,并且buf [0]的地址和静态字符串“a \ n”的地址肯定不会相等。要比较字符串,请使用strncmp()

发生崩溃是因为在调用Close()之后,在套接字文件描述符上再次调用read()函数。要删除崩溃,请在Close()之后放置break;,如下所示:

else
 {
   printf("Incorrect Input");
   Close(sockfd);
   break;
 }  

答案 1 :(得分:0)

此外,您正在关闭套接字两次。首先来到这里:

// void result(int sockfd)
else
{
    printf("Incorrect Input");
    Close(sockfd); // <<<
} 

然后在这里:

// int main(int argc, char **argv)
Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));

result(connfd);
Close(connfd); // <<<
printf("Conexion cerrada...Esperando siguiente cliente\n");