C语言中的套接字程序,发送消息阻止进一步的代码

时间:2018-09-26 00:47:01

标签: c sockets client-server

我是套接字编程的新手,目前在客户端和服务器之间发送/接收信息时遇到问题。目前,我正在向客户端发送“ TRN”给客户端,并在客户端进行处理,但是程序被卡住了。控制台将打印[DEBUG] Sent start message.,但是它永远不会显示到MADE IT HERE 1消息中。在客户端,当它接收到“ TRN”时,将对其进行处理,然后将一个int发送回我想接收的服务器,但是它并没有那么做。这是我的代码细分:

/* Send the start message. */
write_client_msg(cli_sockfd, "TRN");
printf("\n[DEBUG] Sent start message.\n");
int place_reveal = 0;
printf("\nMADE IT HERE 1");
place_reveal = recv_int(cli_sockfd);
printf("\nThis is the users results: %d", place_reveal);

/* Writes a message to a client socket. */
void write_client_msg(int cli_sockfd, char * msg)
{
    int n = write(cli_sockfd, msg, strlen(msg));
    if (n < 0)
        error("ERROR writing msg to client socket");

}

/* Reads an int from a client socket. */
int recv_int(int cli_sockfd)
{
    printf(" MADE IT HERE 2");

    int msg = 0;
    int n = read(cli_sockfd, &msg, sizeof(int));

    if (n < 0 || n != sizeof(int)) /* Not what we were expecting. Client likely disconnected. */
        return -1;

    printf("[DEBUG] Received int: %d\n", msg);

    return msg;
}

我发现,虽然当我按Control C退出程序时,控制台输出是:

^C
MADE IT HERE 1 MADE IT HERE 2
OUCH you hit Ctrl-C?!
Goodbye!

0 个答案:

没有答案