Question

Basically i'm trying to do a server-client program that communicates with sockets. I find it strange that the server, once started it doesn't even print the first line. Why is this? There must be something that's sliping from me and I really need to know what.

Server.c

int rvsock;

void stop(int sig){
    close(rvsock);
}

void* worker(void* p){
        struct mymsg m;
        int err;
        int sock = (int)p;
    err = recv(sock,&m,sizeof(m),0);
    if(err < 0){
        printf("Failed to receive");
        exit(1);
    }

    m.a = ntohl(m.a);
    m.b = ntohl(m.b);
    m.c = ntohl(m.c);
    m.ip = ntohl(m.ip);

    printf("Received numbers: %d %d %d from IP:%d", m.a,m.b,m.c,m.ip);

    if(m.a < m.b && m.b <= m.c)
        m.a = m.c;
    else if(m.a < m.b && m.b >= m.c)
            m.a = m.b;
    else if(m.a > m.b && m.b <= m.c)
            m.a = m.a;

    m.a = htonl(m.a);
    m.b = htonl(m.b);
    m.c = htonl(m.c);
    err = send(sock, &m,sizeof(m),0);
    if(err < 0){
        printf("Failed to send!");
        close(sock);
        return NULL;
    }

    close(sock);
    return NULL;
}

int main(int argc, char** argv) {
        printf("DAFUQ");   //It doesn't even print this. Why?
        int port;
        int sock;
        int err;
        unsigned int len;
        struct sockaddr_in saddr;
        struct sockaddr_in caddr;
        pthread_t w[100];
        int wn = 0;
        int i;
        sscanf(argv[1], "%d", &port);

        signal(SIGINT, stop);

        rvsock = socket(AF_INET, SOCK_STREAM, 0);
        if(rvsock < 0) {
            perror("Failed to create socket");
            exit(1);
        }

        memset(&saddr, 0, sizeof(saddr));
        saddr.sin_family = AF_INET;
        saddr.sin_addr.s_addr = INADDR_ANY;
        saddr.sin_port = htons(port);

        err = bind(rvsock, (struct sockaddr*)&saddr,
        sizeof(struct sockaddr_in));
        if(err < 0) {
            perror("Failed to bind");
            exit(1);
        }

        err = listen(rvsock, 5);
        if(err < 0) {
            perror("Failed to listen");
            close(rvsock);
            exit(1);
        }

       while(1) {
           len = sizeof(struct sockaddr_in);
           sock = accept(rvsock, (struct sockaddr*)&caddr, &len);
           if(sock < 0) {
               perror("Failed to accept");
               break;
           }

           pthread_create(&w[wn], 0, worker, (int*)sock);
           wn++;
       }

      for(i=0; i<wn; i++) {
          pthread_join(w[i], 0);
      }

      return 0;
}

client.c

int main(int argc, char** argv){
    int sock;
    int err;
    int port;
    struct sockaddr_in saddr;
    struct mymsg m;

sscanf(argv[1], "%d", &port);

sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0){
    printf("failed to create");
    exit(1);
}

memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
saddr.sin_port = htons(port);

err = connect(sock, (struct sockaddr*)&saddr, sizeof(struct sockaddr_in));
if(err < 0){
    perror("Failed to connect!");
    exit(1);
}

printf("give a:");scanf("%d",&m.a);
printf("give b:");scanf("%d",&m.b);
printf("give c:");scanf("%d",&m.c);

m.a = htonl(m.a);
m.b = htonl(m.b);
m.c = htonl(m.c);

send(sock,&m,sizeof(m),0);

return 0;
}

message.h

struct mymsg{
    int a;
    int b;
    int c;
    int ip;
};
Was it helpful?

Solution

In your server you have this line. printf prints to the stdout FILE which is opened for you when you start your program.

printf("DAFUQ");   //It doesn't even print this. Why?

man stdout provides the following information about stdout:

The stream  stdout  is  line-buffered
when  it  points  to  a  terminal.  Partial lines will not appear until
fflush(3) or exit(3) is called, or a newline is printed.

You don't print a newline, you don't flush stdout and your program doesn't exit, therefore none of the conditions required to print your output to the terminal are met.

Therefore in order to print the line you have 3 options:

1

printf("DAFUQ");   //It doesn't even print this. Why?
fflush(stdout);

2

printf("DAFUQ\n"); /*notice added '\n'*/   

3 call exit() after you print ( not very helpful probably).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top