Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 服务器无法接受来自多个客户端的()消息?_C_Sockets - Fatal编程技术网

C 服务器无法接受来自多个客户端的()消息?

C 服务器无法接受来自多个客户端的()消息?,c,sockets,C,Sockets,两个客户端可以连接到服务器,但它只接受并显示第一个客户端的输入流消息,而不显示第二个客户端的输入流消息,尽管另一个客户端也已连接 下面是我接受流的代码,我试着在每次连接时关闭套接字,但没有成功 int main(int argc,char *argv[]) { fd_set ready; struct sockaddr_in msgfrom; int msgsize; union { uint32_t addr; char bytes[4]; } fromaddr; if ((

两个客户端可以连接到服务器,但它只接受并显示第一个客户端的输入流消息,而不显示第二个客户端的输入流消息,尽管另一个客户端也已连接

下面是我接受流的代码,我试着在每次连接时关闭套接字,但没有成功

int main(int argc,char *argv[])
{
fd_set ready;
struct sockaddr_in msgfrom;
int msgsize;
union {
    uint32_t addr;
    char bytes[4];
} fromaddr;

if ((progname = rindex(argv[0], '/')) == NULL)
    progname = argv[0];
else
    progname++;
while ((ch = getopt(argc, argv, "adsp:h:")) != -1)
    switch(ch) {
        case 'a':
            aflg++;     /* print address in output */
            break;
        case 'd':
            soctype = SOCK_DGRAM;
            break;
        case 's':
            server = 1;
            break;
        case 'p':
            port = optarg;
            break;
        case 'h':
            host = optarg;
            break;
        case '?':
        default:
            usage();
    }
argc -= optind;
if (argc != 0)
    usage();
if (!server && (host == NULL || port == NULL))
    usage();
if (server && host != NULL)
    usage();
/*
 * Create socket on local host.
 */
if ((s = socket(AF_INET, soctype, 0)) < 0) {
    perror("socket");
    exit(1);
}
sock = setup_server();  
while (!done) {
    FD_ZERO(&ready);
    FD_SET(sock, &ready);
    FD_SET(fileno(stdin), &ready);
    if (select((sock + 1), &ready, 0, 0, 0) < 0) {
        perror("select");
        exit(1);
    }
    if (FD_ISSET(fileno(stdin), &ready)) {
        if ((bytes = read(fileno(stdin), buf, BUF_LEN)) <= 0)
            done++;
        send(sock, buf, bytes, 0);
    }
    msgsize = sizeof(msgfrom);
    if (FD_ISSET(sock, &ready)) {
        if ((bytes = recvfrom(sock, buf, BUF_LEN, 0, (struct sockaddr *)&msgfrom, &msgsize)) <= 0) {
            done++;
        } else if (aflg) {
            fromaddr.addr = ntohl(msgfrom.sin_addr.s_addr);
            fprintf(stderr, "%d.%d.%d.%d: ", 0xff & (unsigned int)fromaddr.bytes[0],
                0xff & (unsigned int)fromaddr.bytes[1],
                0xff & (unsigned int)fromaddr.bytes[2],
                0xff & (unsigned int)fromaddr.bytes[3]);
        }
        write(fileno(stdout), buf, bytes);
    }
}
以下是我设置服务器的代码,以供参考:

int setup_server() {
struct sockaddr_in serv, remote;
struct servent *se;
int newsock, len;

len = sizeof(remote);
memset((void *)&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
if (port == NULL)
    serv.sin_port = htons(9990);
else if (isdigit(*port))
    serv.sin_port = htons(atoi(port));
if (bind(s, (struct sockaddr *)&serv, sizeof(serv)) < 0) {
    perror("bind");
    exit(1);
}
if (getsockname(s, (struct sockaddr *) &remote, &len) < 0) {
    perror("getsockname");
    exit(1);
}
fprintf(stderr, "Port number is %d\n", ntohs(remote.sin_port));
listen(s, 1);
newsock = s;
if (soctype == SOCK_STREAM) {
    fprintf(stderr, "Entering accept() waiting for connection.\n");
    newsock = accept(s, (struct sockaddr *) &remote, &len);
}
return(newsock);
}
客户端代码:

sock = setup_client();
/*
 * Set up select(2) on both socket and terminal, anything that comes
 * in on socket goes to terminal, anything that gets typed on terminal
 * goes out socket...
 */
while (!done) {
    FD_ZERO(&ready);
    FD_SET(sock, &ready);
    FD_SET(fileno(stdin), &ready);
    if (select((sock + 1), &ready, 0, 0, 0) < 0) {
        perror("select");
        exit(1);
    }
    if (FD_ISSET(fileno(stdin), &ready)) {
        if ((bytes = read(fileno(stdin), buf, BUF_LEN)) <= 0)
            done++;
        send(sock, buf, bytes, 0);
    }
    msgsize = sizeof(msgfrom);
    if (FD_ISSET(sock, &ready)) {
        if ((bytes = recvfrom(sock, buf, BUF_LEN, 0, (struct sockaddr *)&msgfrom, &msgsize)) <= 0) {
            done++;
        } else if (aflg) {
            fromaddr.addr = ntohl(msgfrom.sin_addr.s_addr);
            fprintf(stderr, "%d.%d.%d.%d: ", 0xff & (unsigned int)fromaddr.bytes[0],    0xff & (unsigned int)fromaddr.bytes[1],
                0xff & (unsigned int)fromaddr.bytes[2],
                0xff & (unsigned int)fromaddr.bytes[3]);
        }
        write(fileno(stdout), buf, bytes);
    }
    //close(sock);
}
return(0);
}

/*
 * setup_client() - set up socket for the mode of soc running as a
 *      client connecting to a port on a remote machine.
 */

int setup_client() {

struct hostent *hp, *gethostbyname();
struct sockaddr_in serv;
struct servent *se;

/*
 * Look up name of remote machine, getting its address.
 */
if ((hp = gethostbyname(host)) == NULL) {
    fprintf(stderr, "%s: %s unknown host\n", progname, host);
    exit(1);
}
/*
 * Set up the information needed for the socket to be bound to a socket on
 * a remote host.  Needs address family to use, the address of the remote
 * host (obtained above), and the port on the remote host to connect to.
 */
serv.sin_family = AF_INET;
memcpy(&serv.sin_addr, hp->h_addr, hp->h_length);
if (isdigit(*port))
    serv.sin_port = htons(atoi(port));
else {
    if ((se = getservbyname(port, (char *)NULL)) < (struct servent *) 0) {
        perror(port);
        exit(1);
    }
    serv.sin_port = se->s_port;
}
/*
 * Try to connect the sockets...
 */ 
if (connect(s, (struct sockaddr *) &serv, sizeof(serv)) < 0) {
    perror("connect");
    exit(1);
} else
    fprintf(stderr, "Connected...\n");
return(s);
}

UDP还可以,您可以像已经做过的那样重新接收vFrom

TCP是不同的,但你几乎做到了:你需要一个调用来接受你想要处理的每个连接,即你应该在循环中选择服务器套接字,根据需要调用accept来获得一个新的套接字,处理它并在最后关闭它


在客户端,由于您在服务器的挂起连接队列中,因此看起来您已连接-请参阅listen2手册页。

从您发布的代码中,您的accept调用位于服务器设置中,因此,虽然您没有显示调用setup\u server的位置,但我只能假设您正在调用accept一次?如果是这样,那就是你的问题。accept是获取要处理的客户端连接的对象。如果只调用一次,您将只处理一个客户端连接。您的接受调用在哪里?@spdaley我已将main方法包含在代码段中。我已尝试过,但这次即使在我关闭套接字后,它也无法绑定该套接字。为了供您参考,我还提供了客户端代码,请告知我到底哪里出错了。您不应该关闭服务器套接字或重新绑定它;只需在同一个服务器套接字上再次调用accept,就可以关闭accept返回的套接字句柄。在准备停止接受客户端连接之前,不要关闭服务器套接字句柄。