Hi,
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.
Code:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int loop;
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) */
char i;
int main()
{
loop=1;
while(loop=1)
{
i = 0;
printf("Would you like to connect(C) to existing game, host(H) a game or see\n");
printf("the rules(R)?\n");
scanf("%s", &i);
if(i='C')
{
/* (clnt) connect to IP */
printf("Enter the target <Server IP>.\n");
scanf("%s", &servIP);
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");
}
}
Any ideas on how to solve this or even a nudge in the right direction would be greatly appreciated.
Many Thanks.