I'm working on a relatively simple program that is to allow two linux boxes to connect and send messages back and forth which will be processed at each end and made to play a game.....now i've done this sort of thing before and never had any trouble but this time i'm getting a Segmentation Fault and then an Address Out of Bounds error when I debug with gbd.
I've been trying to fix this all morning and i've narrowed down the problem to the variable 'servIP' and specifically whilst i'm sorting the sockaddr structure. It seems to me like servIP is somehow having it's memory location changed. Naturally, an oob address caused a segfault which stops the program. However, my level of knowledge does not extend down to individual address spaces and why this is happening and I cant seem to find any clarification on any of the existing threads.
Any ideas on how to solve this or even a nudge in the right direction would be greatly appreciated.
int hostSock; /* Socket descript for host */
int clntSock; /* Socket descript for client */
struct sockaddr_in hostAddr; /* local address */
struct sockaddr_in clntAddr; /* client address */
char *servIP; /* target servers IP (for clnt) */
i = 0;
printf("Would you like to connect(C) to existing game, host(H) a game or see\n");
/* (clnt) connect to IP */
printf("Enter the target <Server IP>.\n");
clntSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clntSock < 0)
printf("Could not create socket.");
memset(&clntAddr, 0, sizeof(clntAddr));
clntAddr.sin_family = AF_INET;
clntAddr.sin_addr.s_addr = inet_addr(servIP);
/* in the above line I get the seg fault, and gnb states that the address space of servIP is oob */
clntAddr.sin_port = 9876;
if(connect(clntSock, (struct sockaddr*) &clntAddr, sizeof(clntAddr)) <0)
printf("Could not connect to server");