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(¤t_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, ¤t); // 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, "%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, ¤t) && 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.)