If my server is to support multiple connections on the same port, is there an elegant way to pick which socket to listen on when a connection is incoming?
If you don't understand me here's some pseudo-code of what I mean
Code:
int socket[10];
int i;
while (still listening)
{
for (i = 0; i < 10; i++)
{
if (socket[i] == invalid_socket)
{
socket[i] = accept();
break;
}
}
}
Also, I am implementing a system when I accept an http header, I limit the size of the allowed http header, to say 2048 bytes. If it's larger, I send back "Bad Request" or something similar. Once again, is there a better way of how I am doing this at the moment? This snippet takes into consideration packet fragmentation.
Code:
char buffer[2048];
int numbytes = 0;
int result;
do
{
result = recv(sck, buffer, 2048, 0);
if (result > 0)
{
if ((numbytes + result) > 2048)
{
//Send Bad Request
break;
}
numbytes += result;
if (last 4 bytes are \r\n\r\n)
{
sendtoparser(buffer, numbytes);
break;
}
}
}
while (result > 0);
I am looking to move a lot of my programming into C++, so it there is a way to easily manage this type of stuff, please point me to any functions or anything to make life easier!
Thanks!