0

I had used Windows for socket programming recently, but my assignment requires students to use Linux Socket header(sys/socket.h) rather than Windows one. So I'm starting from ground zero but the program freezes even though it is a very simple program that does nothing but only prints a message when a connection is made.

Server.cpp

#include <sys/socket.h>
#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <errno.h>
#include <string>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 3490
#define BACKLOG 10

int main(void){
    int sockfd, new_fd;
    struct sockaddr_in my_addr;
    struct sockaddr_in their_addr;
    socklen_t sin_size;
    if((sockfd = socket(PF_INET, SOCK_STREAM, 0))==-1){
        perror("Socket creation failed");
        exit(1);
    }
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if(bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr))==-1){
        perror("Socket binding failed");
        exit(1);
    }
    if(listen(sockfd, BACKLOG) == -1){
        perror("Socket listening failed");
        exit(1);
    }
    sin_size = (socklen_t)sizeof(struct sockaddr_in);
    while(true){
        printf("Loop Test"); // This is not displayed at all
        if((new_fd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size))==-1){
            perror("Connetion accepting failed");
            exit(1);
        }
        printf("Server got connection from %s\n", inet_ntoa(their_addr.sin_addr));
    }
    return 0;
}

Client.cpp

#include <sys/socket.h>
#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <errno.h>
#include <string>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 3490

int main(void){
    int sockfd;
    struct sockaddr_in their_addr;
    if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1){
        perror("Socket generating failed");
        exit(1);
    }
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(PORT);
    their_addr.sin_addr.s_addr = INADDR_LOOPBACK;
    if(connect(sockfd, (struct sockaddr*)&their_addr, sizeof(struct sockaddr)) == -1){
        perror("Connection failed");
        exit(1);
    }
    printf("Loop test"); // This is not displayed no matter if server is on or not
    return 0;
}

As I mentioned in the code, it just stops and both perror and printf(for debugging) are not displayed. Why is this happened? (I'm using Ubuntu)

James
  • 103
  • 7

1 Answers1

1

There are two issues with your program. The first one is relatively minor, it is that you do not end your printf statements with "\n". Because of that the output is buffered and you do not see it immediately.

However, this would alone would not be visible (although it does make debugging harder as you are unsure what is happening) if you would not have a simple yet annoying bug - you fail to convert INADDR_LOOPBACK to big endian with htonl. Ironically, you do this for INADDR_ANY (which although good style is not doing anything real, since 0 is always 0), but do not do it where it really matters.

SergeyA
  • 56,524
  • 5
  • 61
  • 116
  • I forgot to use htonl, thanks. But I can't understand the last sentence. What do you mean by 'good style is not doing anything real' ? – James Mar 22 '18 at 13:56
  • @James INADDR_ANY is for all intents and purposes always 0. Endiannes for 0 does not matter, so `htonl` does nothing. – SergeyA Mar 22 '18 at 14:03