客户端不接收消息表单服务器

时间:2013-03-23 04:49:06

标签: c sockets

我正在从服务器向客户端发送消息(问候语)。服务器发送数据但客户端没有收到数据,然后服务器跳转到读取,因为客户端必须回复用户的输入。你能告诉我为什么我从未收到问候语吗?

服务器:

int
main(int argc, char **argv)
{
    int                 listenfd, connfd;
    socklen_t           len;
    struct sockaddr_in  servaddr, cliaddr;
    char                buff[MAXLINE];
    time_t              ticks;
    char                message[50];
    char                temp_scale[2];
    char                recvdata[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("Listening on port 5678...Press Ctrl+C to end server\n");
    printf("Waiting for incoming connections...\n");

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

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

        snprintf(message, sizeof(message), "%s\r\n", "Welcome!");
        Writen(connfd, message, strlen(message));
        while ( (n = read(connfd, recvdata, MAXLINE)) > 0)
            {
             recvdata[n] = 0;   /* null terminate*/
             if (fputs(recvdata, stdout) == EOF)
                 err_sys("fputs error");
            }
        if (n < 0)
         err_sys("read error");

    Close(connfd);
    }
}

客户端:

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 (argc != 2)
        err_quit("usage: a.out <IPaddress>");*/

    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])); /*port passed through command line*/
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) /*The client translates the server address, passed on the command line*/
        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))); 

    while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
        recvline[n] = 0;    /* null terminate */
        if (fputs(recvline, stdout) == EOF)
            err_sys("fputs error");
    }
    if (n < 0)
     err_sys("read error");

    printf("Enter the thermal Unit ('C' Celsius, 'F' Farehnheit: ");
    send_data(sockfd);

    exit(0);
}

谢谢!

0 个答案:

没有答案