I've got this code in my socket class:
bool GSocket::Listen(int Port)
{
d->Socket = socket(AF_INET, SOCK_STREAM, 0);
if (d->Socket >= 0)
{
sockaddr Addr;
sockaddr_in *a = (sockaddr_in*) &Addr;
ZeroObj(Addr);
a->sin_family = AF_INET;
a->sin_port = htons(Port);
a->sin_addr.OsAddr = INADDR_ANY;
if (bind(d->Socket, &Addr, sizeof(Addr)) >= 0)
{
if (listen(d->Socket, SOMAXCONN) != SOCKET_ERROR)
{
return true;
}
else
{
Error();
}
}
else
{
Error();
}
}
else
{
Error();
}
return false;
}
The "Error()" method just calls WSAGetLastError and passes the error and it's description up to the app. Anyway it works fine on my machine (xp sp2) but fails on my friends xp sp3 machine. In particular the bind call fails and WSAGetLastError returns "2", which isn't even a valid socket error code. The value of "Port" passed in is 80, I'm running a simple HTTP server as UI for a service. I'm not entirely sure why I check for >= 0, but it could be related to non-windows platforms I also use this code on. In any case according to the MSDN the return code on error for bind is SOCKET_ERROR which is -1 so that check should be ok.
Have I missed something simple?
Update: We just tried a different port number '8888' and everything works as expected. So it seems that the low port number is the issue. There is nothing actively listening on that port before we run my service, so I'm thinking that it's some sort of new permissions issue in SP3 that stops processes listening on ports < 1024 unless they have certain permissions, similar to the linux/unix way of doing things. Still I'd like to be able to sort it out anyway.