Hi.
I just started learning networking and I managed to make this code:
Code:
#include <winsock.h>
#include <iostream>
using namespace std;
int RequestedVersion = 2;
void DoStuff()
{
SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSocket == INVALID_SOCKET) { cout << "Unable to create a socket" << endl; return; }
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(80);
sockAddr.sin_addr.S_un.S_addr = inet_addr("72.14.221.104");
// Connect to the server
if (connect(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr))!=0)
{
// error handling code
cout << "Connection failed!" << endl;
}
else { cout << "Socket is connected" << endl; }
if (bind(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr))!=0)
{
cout << "Binding failed (" << WSAGetLastError() << ")" << endl;
}
}
int main()
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(RequestedVersion, 0), &wsadata) == 0)
{
cout << "Startup was successful!" << endl;
if (RequestedVersion >= wsadata.wVersion)
{
cout << "Version " << wsadata.wVersion << endl;
}
DoStuff();
system("pause");
if (WSACleanup())
{
cout << "Cleanup failed!" << endl;
}
}
else
{
cout << "Startup failed!" << endl;
}
}
it gives the following output when run:
Startup was successful!
Version 2
Socket is connected
Binding failed (10022)
Press any key to continue . . .
So I can't figure out what's wrong. Please help or direct me. Thank you!