why Bind( ) function returns the -1

Status
Not open for further replies.

scorrpeio

Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
I am currently doing the socket programming for UDP in VC++ using Visual Studio 2005 where, I used the Bind() function to bind the socket. But I everytime gets the result..........Bind failed.
When I checked out, the function Bind() returns -1.
I dont get why this happen?? Please let me know.


// Create A Socket

int ServerSocket;
ServerSocket = socket( PF_INET,SOCK_DGRAM, 0 );

//Check for existance

if ( ServerSocket < 0 )
{
MessageBox ("The Socket is not created!!!" );
}

//Bind the Socket

int MySock;
struct sockaddr_in MyAddr;

MySock = socket ( PF_INET,SOCK_DGRAM, 0 );
MyAddr.sin_family = AF_INET;
MyAddr.sin_addr.s_addr = inet_addr( m_IPAdd );
MyAddr.sin_port = htonl( m_iPortNo );

bind ( ServerSocket, (SOCKADDR*) &MyAddr, sizeof( MyAddr ) );

if ( bind ( ServerSocket, (SOCKADDR*) &MyAddr, sizeof( MyAddr ) ) == SOCKET_ERROR )
{
MessageBox( "bind( ) failed." );
closesocket( ServerSocket );
return;
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…