2012-04-11 133 views
1

我有一个监听TCP unix域/本地套接字的服务器守护进程。运行在同一台计算机上的多个客户端连接到它。守护进程也绑定到UDP Internet套接字。只要守护进程从本地客户端接收到任何数据,它就会向除连接客户端以外的所有连接客户端发送相同的数据。如果守护进程接收到UDP因特网套接字上的数据,则需要将该数据发送给所有本地连接的客户端。当守护程序在本地套接字上接收到数据时,数据的发送/接收工作就完美了。但是,当服务器发送它们在UDP互联网套接字上接收到的数据时,客户端不会收到任何数据。客户端在退出服务器守护程序并关闭连接后,或者在任何客户端将数据本地发送到服务器时接收该互联网数据。互联网数据与客户端一起接收本地数据。我使用fcntl()将本地和inet套接字设置为阻塞。下面是我(我已删除所有不必要的代码)的守护进程代码:套接字recv()没有收到数据

while(1) 
{ 
    FD_SET(sockfd, &read_fds); 
    FD_SET(inet_sock, &read_fds); 
    for (i = 0; i < nclients; i++) 
    { 
    FD_SET(clients[i], &read_fds); 
    } 

    select(maxfd + 1, &read_fds, &write_fds, &except_fds, NULL); 

    /* Check for events on inet sock */ 
    if (FD_ISSET(inet_sock, &read_fds)) 
    { 
    /* Read from inet sock */ 
    socklen = sizeof(dest_sin); 
    rval = recvfrom(inet_sock, buf, BUFLEN-1, MSG_DONTWAIT, 
        (struct sockaddr *) &dest_sin, &socklen);   
    buf[rval]=0; 
    fprintf(stderr, "Received: %d (%d) bytes containing %s", rval, strlen(buf), buf); 

    /* Send the message to every other client */ 
    for(j=0; j < nclients; j++) 
    { 
     send(clients[j], buf, strlen(buf), MSG_DONTWAIT); 
    } 
    } 

    /* A read event on the local socket is a new connection */ 
    if (FD_ISSET(sockfd, &read_fds)) 
    { 
    socklen = sizeof(dest_sun); 
    /* Accept the new connection */ 
    rval = accept(sockfd, (struct sockaddr *) &dest_sun, &socklen); 

    /* Add client to list of clients */ 
    clients[nclients++] = rval; 
    if (rval > maxfd) maxfd = rval; 
    snprintf(s, BUFLEN, "You are client %d [%d]. You are now connected.\n\0", 
     nclients, rval); 
    send(rval, s, strnlen(s, BUFLEN), MSG_DONTWAIT); 
    } 

    /* Check for events from each client */ 
    for (i = 0; i < nclients; i++) 
    { 
    fprintf(stderr,"Checking client %d [%d] for read indicator.\n",i, clients[i]); 

    /* Client read events */ 
    if (FD_ISSET(clients[i], &read_fds)) 
    { 
     fprintf(stderr, "Client %d [%d] marked for read.\n", i, clients[i]); 

     /* Read from client */ 
     rval=recv(clients[i], buf, BUFLEN-1, MSG_DONTWAIT); 

     buf[rval]=0; 
     fprintf(stderr, "Received: %d (%d) bytes containing %s", rval, strlen(buf), buf); 

     /* Send the message to every other client */ 
     for(j=0; j < nclients; j++) 
     { 
     /* Skip the sender */ 
     if (j == i) continue; 
     /* Send the message */ 
     send(clients[j], s, strlen(s, BUFLEN), MSG_DONTWAIT); 
     } 
    } 
    } 
} 

这里是我的客户端代码:

while(1) 
{ 
    FD_SET(fileno(stdin), &read_fds); 
    FD_SET(sockfd, &read_fds); 
    select(fileno(stdin) > sockfd ? fileno(stdin)+1 : sockfd+1, 
&read_fds, &write_fds, &except_fds, NULL); 

    if (FD_ISSET(sockfd, &read_fds)) 
    { 
    /* Read from socket and display to user */ 
    mlen = recv(sockfd, (void *)buf, BUFLEN-1, MSG_DONTWAIT); 
    buf[mlen]=0; 
    printf("Received %d bytes: %s", mlen, buf); 
    } 

    if (FD_ISSET(fileno(stdin), &read_fds)) 
    { 
    fgets(buf, BUFLEN, stdin); 
    fprintf(stderr, "Sent %d octets to server.", 
    send(sockfd, (void *)buf, (size_t) strnlen(buf, BUFLEN), 0)); 
    } 
} 

的目标是让客户端接收数据守护进程立即发送它们(守护进程在其inet套接字上收到的数据)。编辑:我已经计算出,当守护进程发送数据时,客户端的select()返回该套接字可读,但recv()阻塞,这就是我没有获取数据的原因客户端。对于如何解决这个问题,有任何的建议吗?

+0

虽然没有东西跳出来,但请不要在formf()语句中发送send()。另外,你说你删除了代码。实际工作示例是否检查返回码/ errno? – Duck 2012-04-11 01:06:47

+0

是的,实际的程序会进行错误检查。 – ddd 2012-04-11 01:43:49

+0

跳到我身上的是你永远不会检查send和recv调用的返回值。另外,如何在服务器和客户端创建UDP套接字? – 2012-04-11 07:11:51

回答

1

下面是你的代码,提取和比对的send()电话:

send(clients[j], buf, strlen(buf),  MSG_DONTWAIT); 
send(rval,  s, strnlen(s, BUFLEN), MSG_DONTWAIT); 
send(clients[j], s, strlen(s, BUFLEN), MSG_DONTWAIT); 

我在这里看到了一些不一致的地方。有时你会拨打strlen(),有时strnlen(),有时候会有strlen()两个参数(我甚至不知道该怎么办)。

您所看到的问题可能与以下事实有关:您未在套接字上发送任何显示消息之间边界的信息。在流套接字上,消息边界是而不是被保留,您应该注意在协议中包含适当的帧信息,以便接收方可以提取单个消息。您不能完全通过recv()呼叫来获得完全相同的字节数,因为呼叫中存在send()。您将得到相同顺序的合计字节数(这是流套接字的一点),但是这些消息可能会整合或分裂,并且您无法控制该数据。

相关问题