tags:

views:

79

answers:

2

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.

+2  A: 

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.

Ninefingers
thanks for your comment. i've figured out which line was bad but can't quite figure out what i did wrong there.. gotta read more about accept() maybe..
Fantastic Fourier
+1  A: 

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

James McLeod
thanks, i realized that the buffer isn't flushed before it dies and i mistook that as not being executed.
Fantastic Fourier
i still don't know what's causing the error though, haha :/
Fantastic Fourier
Or terminate each line with a newline (\n) which also flushes the output. As it is, the text will run-on in an unreadable mess.
Clifford