使客户端不断响应来自服务器的消息

发布于 2024-11-03 03:03:42 字数 2157 浏览 0 评论 0原文

我正在编写一个简单的客户端/服务器应用程序,但无法让客户端持续响应(目前是回显)发送给它的消息。

client.c

#define BUF_SIZE 1024

int ctrlsockfd;

void error(const char *message);
void closeStreams();
void writeResponse(const char *message);

int main (int argc, const char *argv[]) {
    printf("Client\n");

    // Connection code snipped

    // Handle responses
    int bytes_read;
    char buffer[BUF_SIZE];
    while (1) {
        // Fetch a message
        bytes_read = read(ctrlsockfd, buffer, BUF_SIZE);

        if (bytes_read == -1) {
            error("Failed to read");
        }

        // Send it back
        if (write(ctrlsockfd, buffer, strlen(buffer) + 1) == -1) {
            error("Failed to write");
        }
    }

    // Disconnect
    closeStreams();

    return 0;
}

host.c

#define BUF_SIZE 1024
#define LISTENPORT 9735

void closeStreams();
void error(const char *message);

int ctrlsockfd, clientsockfd;

int main (int argc, const char *argv[]) {
    printf("Server\n");

    // Connection code snipped

    // Accept a request (blocking) - we can only connect to one client at a time
    clientlen = sizeof(clientaddr);
    clientsockfd = accept(ctrlsockfd, (struct sockaddr *) &clientaddr, (socklen_t*) &clientlen);
    if (clientsockfd == -1) {
        error("Error accepting");
    }

    while (1) {
        // Read input string from stdin
        printf("> ");
        char message[BUF_SIZE];
        if (scanf("%s", message) == -1) {
            error("Failed to read from terminal");
        }

        // Send to client
        if (write(clientsockfd, message, strlen(message) + 1) == -1) {
            error("Failed to send message");
        } else {
            printf("Sent message %s\n", message);
        }

        // Read response from client
        char response[BUF_SIZE];
        if (read(clientsockfd, response, BUF_SIZE) == -1) {
            error("Error reading response");
        } else {
            printf("Response: %s\n", response);
        }

        // Close the connection
        closeStreams();
    }
}

这里有什么问题?

I'm writing a simple client/server application and am having trouble getting the client to continuously respond to (at the moment, echo) messages sent to it.

client.c

#define BUF_SIZE 1024

int ctrlsockfd;

void error(const char *message);
void closeStreams();
void writeResponse(const char *message);

int main (int argc, const char *argv[]) {
    printf("Client\n");

    // Connection code snipped

    // Handle responses
    int bytes_read;
    char buffer[BUF_SIZE];
    while (1) {
        // Fetch a message
        bytes_read = read(ctrlsockfd, buffer, BUF_SIZE);

        if (bytes_read == -1) {
            error("Failed to read");
        }

        // Send it back
        if (write(ctrlsockfd, buffer, strlen(buffer) + 1) == -1) {
            error("Failed to write");
        }
    }

    // Disconnect
    closeStreams();

    return 0;
}

host.c

#define BUF_SIZE 1024
#define LISTENPORT 9735

void closeStreams();
void error(const char *message);

int ctrlsockfd, clientsockfd;

int main (int argc, const char *argv[]) {
    printf("Server\n");

    // Connection code snipped

    // Accept a request (blocking) - we can only connect to one client at a time
    clientlen = sizeof(clientaddr);
    clientsockfd = accept(ctrlsockfd, (struct sockaddr *) &clientaddr, (socklen_t*) &clientlen);
    if (clientsockfd == -1) {
        error("Error accepting");
    }

    while (1) {
        // Read input string from stdin
        printf("> ");
        char message[BUF_SIZE];
        if (scanf("%s", message) == -1) {
            error("Failed to read from terminal");
        }

        // Send to client
        if (write(clientsockfd, message, strlen(message) + 1) == -1) {
            error("Failed to send message");
        } else {
            printf("Sent message %s\n", message);
        }

        // Read response from client
        char response[BUF_SIZE];
        if (read(clientsockfd, response, BUF_SIZE) == -1) {
            error("Error reading response");
        } else {
            printf("Response: %s\n", response);
        }

        // Close the connection
        closeStreams();
    }
}

What's the problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-11-10 03:03:42

我认为你在这里混淆了服务器和客户端。通常,服务器侦听端口并等待消息,然后响应它们。 此外,如果您在每次迭代中关闭连接,或者不关闭连接,则accept() 需要成为循环的一部分

Server       client
wait
             connect
             send data
read data
send data
             read data
<maybe iteration of send / receive here >
close        close
wait
...

I think you confuse server and client here. Usually the server listens to a port and waits for messages and then respond to them. Also, the accept() needs to be part of the loop if you close the connection in each iteration, or alternatively, don't close the connection.

Server       client
wait
             connect
             send data
read data
send data
             read data
<maybe iteration of send / receive here >
close        close
wait
...
梦过后 2024-11-10 03:03:42

这里还有很多错误,但我会立即这样做:

// Send it back
if (bytes_read > 0 && write(ctrlsockfd, buffer, strlen(buffer) + 1) == -1) {
     error("Failed to write");
} 

有点想知道那些在没有读取任何内容时所写的内容正在做什么......

There's quite a bit more wrong here, but right away I'd do this:

// Send it back
if (bytes_read > 0 && write(ctrlsockfd, buffer, strlen(buffer) + 1) == -1) {
     error("Failed to write");
} 

Kinda wonder what those writes when nothing was read were doing...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文