glibc detected.

This is a discussion on glibc detected. within the C Programming forums, part of the General Programming Boards category; Some thoughts: Code: #define MAXCONNECTIONS 5 userInfo users[ MAXCONNECTIONS + 10 ]; /* ... */ char *usernamebyip(userInfo *users, struct sockaddr_in ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Some thoughts:
    Code:
    #define MAXCONNECTIONS 5
    
    userInfo users[MAXCONNECTIONS + 10];
    
    /* ... */
    
    char *usernamebyip(userInfo *users, struct sockaddr_in *client)
    {
    	char *seekAddress = inet_ntoa(client->sin_addr);
    	int i = 0;
    	for(; i < MAXCONNECTIONS * 2; i++)
    		if(strcmp(users[i].ipaddress, seekAddress) == 0)
    			return users[i].name;
    	return "Unknown";
    }
    That happens to work, but what if you changed MAXCONNECTIONS to, say, 20? You should use the same expression in both of those places. (Or better yet, use sizeof() to determine the number of elements in the array at the point where usernamebyip() is called, and pass that into the function.)

    Code:
    				time_t current_time = time(0);
    				char *time = malloc(10);
    				getcurrenttime(ctime(&current_time), time);
    Okay, that works, but it's a little confusing, don't you think? Calling a variable the same thing as a standard function usually is.

    getcurrenttime() itself is rather unusual. It's very specific. Perhaps it would be better to have something like copy_string(char *to, const char *from, int startpos, int endpos) . . . .

    Code:
    						bzero(&data, PACKETSIZE + (PACKETSIZE / 2));
    						strcat(data, talker);
    						strcat(data, buffer); // appending talker and buffer to data
    Why not use strcpy() instead of bzero()+strcat(), assuming you don't care about the string beyond the NULL terminator?

    Code:
    						FD_CLR(i, &current); // we want to inform them that i left
    						char userLeftMsg[PACKETSIZE];
    						bzero(&userLeftMsg, PACKETSIZE);
    						size_t peer_size_left = sizeof(struct sockaddr);
    						getpeername(i, (struct sockaddr *)&client, &peer_size_left);
    						sprintf(userLeftMsg, "&#37;s <%s, %s> has left the server.\n", usernamebyip((userInfo *)&users, &client), inet_ntoa(client.sin_addr), time);
    						printf("%s", userLeftMsg); // informing the server
    						for(; k <= loopbetweenDesc; k++) // informing the others that i has left
    							if(FD_ISSET(k, &current) && k != sockfd)
    								if(send(k, userLeftMsg, strlen(userLeftMsg), 0) == -1)
    									perror("send");
    If i has left, don't you think it would be a good idea not to send a message to i that they have left? (Perhaps "&& k != i" would do it.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    About the first issue:
    Yep, you are right, I must have changed it in the loop and forgot to change the size of the array, thanks!
    ---
    It doesn't matter to me actually.
    ---
    I tought strcpy doesn't add the null terminator - I'm gonna use it now, thanks!
    ---
    You're wrong.
    As you see, I'm deleting i from my list:
    Code:
    						FD_CLR(i, &current); // we want to inform them that i left
    						for(; k <= loopbetweenDesc; k++) // informing the others that i has left
    							if(FD_ISSET(k, &current) && k != sockfd) // i isn't set in our list since I deleted it from the list (FD_CLR)
    								if(send(k, userLeftMsg, strlen(userLeftMsg), 0) == -1)
    									perror("send");
    Thanks from the comments.

    BTW, why can't I send messages to sockfd? what's the reason?

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by eXeCuTeR View Post
    You're wrong.
    As you see, I'm deleting i from my list:
    Code:
    						FD_CLR(i, &current); // we want to inform them that i left
    						for(; k <= loopbetweenDesc; k++) // informing the others that i has left
    							if(FD_ISSET(k, &current) && k != sockfd) // i isn't set in our list since I deleted it from the list (FD_CLR)
    								if(send(k, userLeftMsg, strlen(userLeftMsg), 0) == -1)
    									perror("send");
    Ah, I did miss that. Oh well.

    BTW, why can't I send messages to sockfd? what's the reason?
    At a guess? I think sockfd is you, the server. (I say "I think" because I've never really done any programming with BSD sockets.)

    Another thing I'm not sure about: do you need to send a NULL here?
    Code:
    									char greeting[STRING/2];
    									sprintf(greeting, "Hello %s and welcome to the server.\n", users[userIndexer].name);
    									if(send(h, greeting, strlen(greeting), 0) == -1)
    										perror("send");
    As it is, you're leaving off the NULL. I'm not sure if that's okay or not . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    The biggest problem I see is a bloated main doing all the work and a few tiny satellite functions.

    Modularise!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by dwks View Post
    Ah, I did miss that. Oh well.


    At a guess? I think sockfd is you, the server. (I say "I think" because I've never really done any programming with BSD sockets.)

    Another thing I'm not sure about: do you need to send a NULL here?
    Code:
    									char greeting[STRING/2];
    									sprintf(greeting, "Hello &#37;s and welcome to the server.\n", users[userIndexer].name);
    									if(send(h, greeting, strlen(greeting), 0) == -1)
    										perror("send");
    As it is, you're leaving off the NULL. I'm not sure if that's okay or not . . . .
    So the server is supposed to receive the message, doesn't it?
    ---
    That's totally fine...well, it works fine basically..
    ---
    One more REALLY important thing..
    Sorry for the triple-posting but I really need the answer as soon as possible.
    Well,
    1.
    METHOD 1:
    inet_aton("999.999.999.999", &server.sin_addr);
    METHOD 2:
    server.sin_addr.s_addr = inet_addr("999.999.999.999")

    both of these cannot assign my address and when I use:
    server.sin_addr.s_addr = INADDR_ANY;
    I can connect only with 127.0.0.1 (telnet 127.0.0.1 PORT) and not with any other IP address, for example, telnet 999.999.999.999 PORT won't work (if 999.999.999.999 is my IP address of course)
    I've opened the port I'm binding to in my router's configuration and the firewall is not blocking the traffic through this port (set this too)
    Any suggestions?


    And Salem, I'm gonna divide some operations in main into functions soon.
    Last edited by eXeCuTeR; 07-09-2008 at 04:20 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. glibc detected - error
    By mr_m4x in forum C Programming
    Replies: 2
    Last Post: 03-15-2009, 10:29 AM
  2. *** glibc detected *** a.out: realloc()
    By msshapira in forum C Programming
    Replies: 9
    Last Post: 01-27-2009, 08:49 AM
  3. Replies: 1
    Last Post: 09-19-2008, 01:21 AM
  4. Replies: 7
    Last Post: 11-26-2007, 12:11 PM
  5. *** glibc detected *** free(): invalid next size
    By icebabe in forum C Programming
    Replies: 2
    Last Post: 05-24-2006, 12:09 PM

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