-
August 15th, 2012, 09:27 AM
#1
Invalid socket
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
-
August 15th, 2012, 12:50 PM
#2
Re: Invalid socket
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?
-
August 15th, 2012, 02:04 PM
#3
Re: Invalid socket
Originally Posted by S_M_A
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...
-
August 15th, 2012, 02:09 PM
#4
Re: Invalid socket
What can be wrong ?
I´m not using WSAStartup...
You just answered your own question.
MSDN:
The WSAStartup function initiates use of the Winsock DLL by a process.
Without the WSAStartup, there are no sockets.
-
August 15th, 2012, 02:29 PM
#5
Re: Invalid socket
Originally Posted by Skizmo
You just answered your own question.
MSDN:
Without the WSAStartup, there are no sockets.
Ok. I got it.
I´m newbie in sokets world.
Thanks guys. Problem solved.
-
August 15th, 2012, 02:46 PM
#6
Re: Invalid socket
Originally Posted by wberoli
Ok. I got it.
I´m newbie in sokets world.
Thanks guys. Problem solved.
Note that you must call WSAGetLastError (or GetLastError) every time your socket function fails (or is about to fail). Otherwise it won't be possible to know the reason of the failure.
Victor Nijegorodov
-
August 15th, 2012, 02:51 PM
#7
Re: Invalid socket
Originally Posted by VictorN
Note that you must call WSAGetLastError (or GetLastError) every time your socket function fails (or is about to fail). Otherwise it won't be possible to know the reason of the failure.
Thanks for the advice Victor.
I´m steping up the learning curve.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|