Reputation: 11
Hello i am a beginner socket/c programmer and from this tutorial i have the connect function returns 10038 error. please help. what am i doing wrong?
also whats the difference between winsock and winsock2?
also in connect() function definition there is int PASCAL
what is pascal for?
#include <iostream>
#include <winsock.h>
using namespace std;
int main(){
WSADATA wsa;
cout<< "Iinitializing winsock....";
SOCKET sa;
struct sockaddr_in server;
if (WSAStartup(MAKEWORD(2,2), &wsa)!=0)
cout << "Failed";
cout << "initialized";
if ((sa = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) == INVALID_SOCKET))
cout << "Could not create socket " << WSAGetLastError();
cout << "Socket created";
server.sin_addr.s_addr = inet_addr ("213.165.64.44");
server.sin_family = AF_INET;
server.sin_port = htons(7);
//connect
if (connect(sa, (struct sockaddr *)&server, sizeof(server)) < 0){
cerr << "connect error" << WSAGetLastError();
return 1;
}
cout << "connected";
return 0;
}
Upvotes: 0
Views: 6407
Reputation: 5855
You should look at the documentation what 10038 means:
WSAENOTSOCK
10038 (0x2736)
An operation was attempted on something that is not a socket.
So sa
is not a socket. Printing out sa
to cerr
shows that it is zero, so something around the call to the socket()
function is bad. Looking at the line more closely reveals that there is a parentheses error in the line:
if ((sa = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) == INVALID_SOCKET))
the == is executed first, and as the return value of the socket()
function is not invalid socket, zero gets assigned to sa.
The correct expression would be:
if ((sa = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
For the other parts of the question:
PASCAL
is a macro for __stdcall
˙, Windows API functions generally use this calling convention.Upvotes: 3