I am writing a code to connect to a socket.
But when I use the function MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); it always returns invalid socket.
I don´t know why this happens.
Any light would be very appreciated.
Thanks
Printable View
I am writing a code to connect to a socket.
But when I use the function MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); it always returns invalid socket.
I don´t know why this happens.
Any light would be very appreciated.
Thanks
Try the example here http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx (also read the text carefully)
If that works you do something wrong, maybe not calling WSAStartup?
I´m doing:
SOCKET sock;
sock = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET)
return -1;
SOCKADDR_IN serveraddr;
memset(&serveraddr,0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(3000);
serveraddr.sin_addr.s_addr = IpAddr.val;
if (connect(sock, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR_IN)) < 0) {
closesocket( sock );
}
I have did this before in another program and worked fine, but now it doesn´t work.
What can be wrong ?
I´m not using WSAStartup...
You just answered your own question.Quote:
What can be wrong ?
I´m not using WSAStartup...
MSDN:
Without the WSAStartup, there are no sockets.Quote:
The WSAStartup function initiates use of the Winsock DLL by a process.