什么样的错误会影响 C 中先前的语句?

发布于 2024-08-22 20:34:05 字数 2158 浏览 3 评论 0原文

很抱歉问了一个听起来很笼统的问题。

假设

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define SERVER_ADDRESS  "123.456.789.012"
#define CLIENT_ADDRESS  "123.456.789.013"

#define SERVER_TCP_PORT "1234"
#define CLIENT_TCP_PORT "1235"


int main()
{
    printf("o halo thar");


    int sockfd, new_sockfd, msg_len;
    void * got_msg = "got ur msg!";
    void * message;
    struct sockaddr_in server_address, client_address;


    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        exit(1);
    printf("socket is opened");

    bzero((char * ) &server_address, sizeof(server_address)); 
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(SERVER_ADDRESS);
    server_address.sin_port = htons(SERVER_TCP_PORT);


    if (bind(sockfd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
        exit(1);
    printf("socket is bound");

    listen(sockfd,11);
    printf("listening");

    if (accept(sockfd, (struct sockaddr *) &client_address, sizeof(client_address)) < 0) // THE BAD LINE 
        exit(1);
    printf("accepted"); 

    int i;
    for( i = 0; i < 11; i++)
    {
        msg_len = recv(sockfd, (void *) message, 10000, 0);
        if (msg_len < 1)
            exit(1);
        printf("receiving msg");

        if (send(sockfd, (void *) got_msg, 10000, 0) < 0);
            exit(1);
        printf("sending msg");
    }

    close(sockfd);

}

如果一切正常运行,它应该打印 abc 。当然,我的代码没有。但我已将问题定位到我称之为 somecode() 的这行代码。当我注释掉 somecode() 时,程序打印出 ab (而不是 c)。然而,当没有注释掉时,它什么也不打印。那么我遇到了什么样的问题会影响之前的陈述呢?抱歉含糊不清。我只是想知道如何看到 somecode() 编译得很好,但是在运行时,它会影响在到达 somecode() 之前应该完成的代码的执行?谢谢你们。

编辑:somecode() 是

   if (accept(sockfd, (struct sockaddr *) &client_address, sizeof(client_address)) < 0)
      exit(1);

编辑2: 抱歉说得太模糊了。我什至忘记描述该程序发生了什么。它不会打印任何内容,我必须按 ctrl+c 才能摆脱它。

Sorry for a very generic sounding question.

let's say

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define SERVER_ADDRESS  "123.456.789.012"
#define CLIENT_ADDRESS  "123.456.789.013"

#define SERVER_TCP_PORT "1234"
#define CLIENT_TCP_PORT "1235"


int main()
{
    printf("o halo thar");


    int sockfd, new_sockfd, msg_len;
    void * got_msg = "got ur msg!";
    void * message;
    struct sockaddr_in server_address, client_address;


    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        exit(1);
    printf("socket is opened");

    bzero((char * ) &server_address, sizeof(server_address)); 
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(SERVER_ADDRESS);
    server_address.sin_port = htons(SERVER_TCP_PORT);


    if (bind(sockfd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
        exit(1);
    printf("socket is bound");

    listen(sockfd,11);
    printf("listening");

    if (accept(sockfd, (struct sockaddr *) &client_address, sizeof(client_address)) < 0) // THE BAD LINE 
        exit(1);
    printf("accepted"); 

    int i;
    for( i = 0; i < 11; i++)
    {
        msg_len = recv(sockfd, (void *) message, 10000, 0);
        if (msg_len < 1)
            exit(1);
        printf("receiving msg");

        if (send(sockfd, (void *) got_msg, 10000, 0) < 0);
            exit(1);
        printf("sending msg");
    }

    close(sockfd);

}

it should print abc if everything runs correctly. of course, my code doesn't. but i have localized the problem to this certain line of code which i'm calling somecode(). when i comment out somecode(), the program prints out ab (not c). however when not commented out, it prints nothing. so what kind of problem am i running into that affects previous statements? sorry for the vagueness. i'm just wondering how seeing somecode() is compiled fine, but when running, it influences executions of code that should be done before it reaches somecode()? Thanks guys.

EDIT:somecode() being

   if (accept(sockfd, (struct sockaddr *) &client_address, sizeof(client_address)) < 0)
      exit(1);

EDIT2:
sorry for being too vague. i even forgot to describe what happens to the program. it doesn't print anything out and i have to ctrl+c in order to get out of it.

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

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

发布评论

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

评论(2

愛放△進行李 2024-08-29 20:34:05

在没有看到更多代码的情况下,任何导致程序意外退出(而不是被操作系统杀死)的情况,例如分段错误或进入某种形式的代码结构,不允许它返回到执行流,从而需要通过 ctrl+c 杀死。否则,程序应该可以毫无问题地继续进行。

Without seeing more code, anything that causes the program to exit unexpectedly (rather, be killed by the operating system) such as a segmentation fault or entering some form of code structure that does not allow it to return to your flow of execution, necessitating a kill via ctrl+c. Otherwise, the program should carry on without any issue.

南街女流氓 2024-08-29 20:34:05

如果在每次调用 printffflush(STDOUT) 会发生什么?也许你的程序在打印所有缓冲输出之前就死了。

What happens if you fflush(STDOUT) after each call to printf? Maybe your program is dying before printing all buffered output.

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