-
Bind();
I've started using WinSock recently and now that I have it working, I am getting an error in bind() when I run it. Here is the code for bind():
Code:
if( bind( socket, ( SOCKADDR* )( &sockAddr ), sizeof( sockAddr ) ) == SOCKET_ERROR )
{
std::cout << "Failed to bind!" << std::endl;
}
Whenever I run the program it is fine until it gets there, and of course prints Failed to bind! and I cannot figure out what is wrong. If it isn't this, the full code is attached.
- SirCrono6
-
you are filling your sockaddr structure incorrectly. The port needs to be converted to network byte order. Change:
Code:
sockAddr.sin_port = 50;
to
Code:
sockAddr.sin_port = htons(50);
-
For the client, should I also have htons( 50 ) or something different (I've seen others)? Oh, and that worked, thanks :)
- SirCrono6
-
yes, you should. Whenever you fill a sockaddr or sockaddr_in structure, the data must be in network byte order.
-
Hmm
Okay, I've done the same thing to the client program, and I am getting an error at the client's connect() function. I'm guessing it's not the call to connect() itself, so I'll just post all the code.
Code:
#include <iostream>
#include <windows.h>
#include <WINSOCK2.h>
int main( void )
{
WSADATA bWsaDat;
SOCKET bSocket;
SOCKADDR_IN bSockAddr;
HOSTENT *bHost;
char bSendMe[] = "Awesome, I sent!", bServer[] = "localhost";
char bRecieveMe[50];
int bRetVal = SOCKET_ERROR;
if( WSAStartup( MAKEWORD( 2, 0 ), &bWsaDat ) != 0 )
{
std::cout << "WSA initialization failed!" << std::endl;
}
bSocket = ( AF_INET, SOCK_STREAM, 0 );
if( bSocket == INVALID_SOCKET )
{
std::cout << "Failed to create socket!" << std::endl;
}
std::cout << "Waiting for host..." << std::endl;
while( bHost == NULL )
{
bHost = gethostbyname( bServer );
if( bHost != NULL )
{
break;
}
}
bSockAddr.sin_port = htons( 50 ); //I've added it here...
bSockAddr.sin_family = AF_INET;
bSockAddr.sin_addr.s_addr = *( unsigned long* ) bHost->h_addr;
if( connect( bSocket, ( SOCKADDR* )( &bSockAddr ), sizeof( bSockAddr ) ) != 0 )
{
std::cout << "Failed to establish connection with server!" << std::endl;
}
while( bRetVal == SOCKET_ERROR )
{
bRetVal = send( bSocket, bSendMe, strlen( bSendMe ) + 1, 0 );
if( bRetVal == 0 || bRetVal == WSAECONNRESET )
{
std::cout << "Connection closed at other end!" << std::endl;
break;
}
}
bRetVal = SOCKET_ERROR;
while( bRetVal == SOCKET_ERROR )
{
bRetVal = recv( bSocket, bRecieveMe, 50, 0 );
if( bRetVal == 0 || bRetVal == WSAECONNRESET )
{
std::cout << "Connection closed at other end!" << std::endl;
break;
}
}
std::cout << bRecieveMe << std::endl;
closesocket( bSocket );
WSACleanup();
std::cin.get();
return 0;
}
Thanks, and all the variables have b infront of them because I thought it caused the first error :rolleyes:
- SirCrono6
Edit: Maybe it's not that, because now the server is failing to bind() again :mad:
-
Code:
bSocket = ( AF_INET, SOCK_STREAM, 0 );
should be
Code:
bSocket = socket( AF_INET, SOCK_STREAM, 0 );
Code:
while( bHost == NULL )
{
bHost = gethostbyname( bServer );
if( bHost != NULL )
{
break;
}
}
The above code is bad for a couple reasons. First, if gethostbyname() fails once, it's just going to put you in an endless loop of failure. Second, since bHost is never initialized, what happens if it starts out as something other than 0? Your gethostbyname() function would never get called.