Chat Server

I had recently been in a computer/business class and decided to have some fun since I was way ahead of the class(JAVA). So I first started off by creating a VB Chat Server and Client I had many features like kick, admin login, bans, bad word filters, private messages, etc. Anyway I noticed a bit of lag in the server so I decided to remake the server in C and keep the client in VB. I have been making the server and I seem to be going fine until I hit something that would compromise the whole project, a function called sendtoall, that sends to all the open sockets.

#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>


int socks[500], ind;
void dostuff(int);
void error(char *msg){
perror(msg);
exit(1);
}

int main(int argc, char *argv[]){
int sockfd, newsockfd, portno, clilen, pid;
struct sockaddr_in serv_addr, cli_addr;

if (argc <2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
ind = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
   error("ERROR opening socket");
bzero((char * ) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)
   error("ERROR on binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
while(1){
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd < 0) 
   error("ERROR on accept");
socks[ind]=newsockfd;
ind = ind + 1;
pid = fork();
if(pid<0)
   error("ERROR on fork");
if (pid==0) {
close(sockfd);
dostuff(newsockfd);
exit(0);
}
else close(newsockfd);
}
return 0;
}



void dostuff (int sock)
{
   int n;
   char buffer[256];   
   bzero(buffer,256);
   n = read(sock,buffer,255);
   if (n < 0) error("ERROR reading from socket");
   sendtoall(buffer);
   printf("Here is the message: %s\n",buffer);
   n = write(sock,"I got your message",18);
   if (n < 0) error("ERROR writing to socket");
}


void sendtoall(char* msg){
int i, n;
i = 0;
printf("Message:%s \n", msg);
printf("Sent to all..or not\n");
printf("I:%d", i);
for(i=0;500<=i;i++){
printf("I:%d", i);
n = write(socks,msg,sizeof(msg));
if (n < 0) error("ERROR writing to socket"); 
}
}


void parsemsg(char *msg){
}

This is my code so far, yet the problem is a logical error. When it runs sendtoall if displays everything outside of the for loop, yet it never executes any commands inside it. Please help me.

Thanks in advance.

, Lazyshot

When i = 0, 500 <= i is false, so the loop will break without running anything inside.

oops, stupid mistake

i now have another problem being that it won't send the message to all the sockets.....