UDP Server. It won't bind()

Hello. I'm trying to make an UDP server which will do certain things on one's computer based on the messages received.

That's how far i got:

[code]#include "windows.h"
#include "winsock2.h"

int main()
{
WSADATA wsaData;
if(WSAStartup(0x0202, &wsaData))
{
MessageBox(NULL,"Call to WSAStartup() failed", "Error!", MB_ICONSTOP);
WSACleanup();
return 0;
}
else
{
if(wsaData.wVersion != 0x0202)
{
MessageBox(NULL,"Wrong socket version!", "Error!", MB_ICONSTOP);
WSACleanup();
return 0;
}
}

sockaddr_in serverSockAddrIn;
memset(&serverSockAddrIn, 0, sizeof(serverSockAddrIn));
serverSockAddrIn.sin_family = AF_INET;
serverSockAddrIn.sin_port = htons(14564);
serverSockAddrIn.sin_addr.s_addr = htonl(INADDR_ANY);

SOCKET serverSocket;
serverSocket = (AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if(serverSocket == INVALID_SOCKET)
{
MessageBox(NULL, "Socket initialization failed!", "Error!", MB_ICONSTOP);
WSACleanup();
return 0;
}

if(bind(serverSocket,(sockaddr*)&serverSockAddrIn, sizeof(serverSockAddrIn)) == SOCKET_ERROR)
{
MessageBox(NULL, "Binding failed!", "Error!", MB_ICONSTOP);
WSACleanup();
return 0;
}

WSACleanup();
}

[/code]

I compiled the code just to test and see if there are any spelling errors and for my surprise o noticed that it won't bind(). I've checked MSDN and several examples. Everything seems to be fine according to those examples.

Why do you think this is happening? I've checked the port i am bind(ing)() to and it's free.

Comments

  • Whoa, I've no idea how this compiled, but you have:
    serverSocket = (AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    instead of:
    serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  • Aww..how couldn't i see that? >< Damn...I'm really sorry for posting this minor problem here.

    Thanks for opening my eyes.
  • No problem, anyone can make that mistake, especially when the compiler actually accepts it. It took me a while to figure it out too :P
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories