address out of bounds in sockets program

This is a discussion on address out of bounds in sockets program within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I'm working on a relatively simple program that is to allow two linux boxes to connect and send messages ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    address out of bounds in sockets program

    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.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    There are a few things wrong with this program. Here are a few quickies.

    Change
    Code:
    scanf("%s", &i);
    to
    Code:
    scanf("%c", &i);
    You need to allocate memory for your char *servIP. That's the problem causing your seg fault. Try using
    Code:
    char servIP[256];
    instead.

    This line is continuously assigning 1 to the variable loop. Use == to compare.
    Code:
    while(loop=1)
    Same with the if (i='C') line.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    Also you should fix your indentation and avoid using global vars
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find program code's address at runtime
    By wakandan in forum C Programming
    Replies: 1
    Last Post: 07-01-2009, 11:26 AM
  2. IP Sockets Program in c++ for University Project
    By saqureshi in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2008, 05:21 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  4. Address program
    By Siggy in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2004, 01:03 AM
  5. how to get memory address applied in other program?
    By wu7up in forum C Programming
    Replies: 1
    Last Post: 03-01-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21