I made a little socket serverthat works (tested with telnet) and now I am starting to code myself a client to interact with the server.
The problem:
multiple pc can host multiple servers with diffirent ports. The client will have to scan the network to search for servers and build a list of server where one is choosen at random.
Now scanning the network (10.0.0.1 - 10.0.0.254) takes forever.
Code:
for(i = 1 ; i < 255 ; i++){
bzero(address,16);
sprintf(address,"%s%d",NETWORK,i);
serv_addr.sin_addr.s_addr = inet_addr(address);
for (y = MINPORT ; y <= MAXPORT ;y++){
serv_addr.sin_port = htons(y);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) < 0){
if (DEBUG){printf("%s:%d no connection\n",address,y);}/*No server found*/
}
else{
found++;
if (DEBUG){printf("w0000000000t %s:%d got connection\n",address,y);}
close(sockfd);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
}
}
}
There has to be a better way to do this I hope. Please help.