消息发送,接收者没有收到消息。套接字编程

时间:2013-03-23 23:25:59

标签: c sockets

我正在接收来自客户端的消息,比较它并根据比较结果发送消息。服务器(显然)发送消息,但客户端仍处于读取过程中。

服务器:

  

等待传入连接...按Ctrl + C结束服务器

     

从127.0.0.1连接,端口54918收到的消息:c随机数5   进入IFs 1er IF

void
result(int sockfd)
{
    ssize_t     n;
    char        buf[MAXLINE];
    int         temp;
    time_t      ticks;
    int         i;
again:
    while ((n =read(sockfd, buf, MAXLINE)> 0))
    {
     buf[n] = '\0';
     printf("Message Recieved:%s\n",buf);
     srand (time(NULL));
     temp = rand() % 15+1;
     printf("Ramdom es %i\n",temp);
     printf("Into IF\n");    
     if ((strncmp (buf,"A",1) == 0) || (strncmp (buf,"a",1) == 0))
     {
      snprintf(buf, sizeof(buf), "Option A chosen times %i on on %.24s\r\n", temp,ctime(&ticks));
      Writen(sockfd, buf, n);
      printf("1st IF\n");
     }
     else if ((strncmp (buf,"B",1) == 0) || (strncmp (buf,"b",1) == 0))
     {
      snprintf(buf, sizeof(buf), "Option B chosen times %i on on %.24s\r\n", temp,ctime(&ticks));
      Writen(sockfd, buf, n);
      printf("2nd IF\n");
     }
     else
     {
       printf("Incorrect Input");
       Close(sockfd);
       break;
     }  
    }
    if (n < 0 && errno == EINTR)
    goto again;
    else if (n < 0)
        err_sys("read error");
    printf("salio de funcion result\n");    
}

int
main(int argc, char **argv)
{
    int                 listenfd, connfd;
    socklen_t           len;
    struct sockaddr_in  servaddr, cliaddr;
    char                buff[MAXLINE];
    /*char                message[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("Server is running on 127.0.0.1, 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);

    }
}

客户端:

  

连接...本地地址是:127.0.0.1:54918   开始阅读......   输入选项A或B:b

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 (argc != 3)
        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])); 
    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("Starting read...\n");
    printf("Enter option A or B:  ");
        get_temp(stdin, sockfd);
    exit(0);
}

感谢。

1 个答案:

答案 0 :(得分:0)

停止编码并编写协议规范。规范应在字节级指定交换的信息和方式。你现在的方式是,你的代码不可能是“正确的”或“错误的”,因为它没有规定它是否符合或不符合。

协议规范中最重要的一点是你是否有“消息”,如果有的话,每一方如何找到它们的开头和结尾。您可以使用关闭连接作为消息边界。您可以使用分隔符(例如零字节或换行符)作为消息边界。您可以为每条消息添加长度前缀。

但是如果没有规范,您的服务器和客户端将仅就谁做什么,何时以及如何运气达成一致。