Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

UDP Server. It won't bind()

DoggySorDoggySor Posts: 4Member
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

Sign In or Register to comment.