What kind of errors affect previous statements in 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_addre开发者_如何学Goss)) < 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.
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.
What happens if you fflush(STDOUT)
after each call to printf
? Maybe your program is dying before printing all buffered output.
精彩评论