# Mysterious Socket Error

This topic is 4735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

First of all, allow me to say this is by no means an expert problem, so if this is not the place to post it forgive me, but it seemed much more relevant here than in the beginners section. What I am trying to do is simple enough, I'm trying to get the hang of socket programming in C, with the end goal is being able to send a single bit of information from one computer to another. I've read through Beej's guide to sockets about half a dozen times (trying to make sure I understand as much of it as I can, the rest time will do) up to the point where the definitions are put aside and the actual functions begin, and that's where my problems begin aswell. Now with the introduction out of the day, here's my problem. Whenever I call socket, it returns an error (-1), however errno and perror state there is no error. I am thinking it's one of those head-wall cases where a single misplaced character puts you off for hours, but that's just my non-expert analysis. Either way, here is the code (not much, but getting there):
#include <winsock.h>
#include <dos.h>

int main( int argc, char* argv[])
{
int sockfd, sError;

sockfd = socket(AF_INET, SOCK_STREAM, 0);

printf("Socket Result: %d\n",sockfd);
printf("Errno: %d\n",errno);
perror("socket");

printf("Bind Result: %d\n",sError);
printf("Errno: %d\n",errno);
perror("socket");

system("PAUSE");
return 0;
}

For which the output would be:
Socket Result: -1
Errno: 0
socket: No error
Bind Result: -1
Errno: 0
socket: No error

Obviously something is wrong here, most likely due to my error. Either that or I just descovered the mythical -1 port. Anyhow, thanks in advance :)

##### Share on other sites
Here's my guess:

1) You're using some flavor of windows.
2) You're not calling WSAStartup before trying to create a socket.

// Add this at the beginning of main()#if defined(WIN32)WSADATA wsaData;WSAStartup( MAKEWORD(2,2), &wsaData );#endif

##### Share on other sites

You're so right is frightening. I took note to initialize WSA but complete forgot abou that. I feel a little embarassed right now, typical error on my part :p

And a little off, but while on the subject, could you possibly educate me on what is the word that is passed to WSAStartup? I've seen several variations of it, 0,1; 1,1; and now 2,2; Just trying to make some sense in it :)

Thanks again

##### Share on other sites
Since this is winsock you should also be checking the error via WSAGetLastError() instead of errno. perror doesn't work either.

There are a few other minor difference between socket programming on Windows vs Linux/Unix. They can mostly be #defined away. For example don't try to use close() or ioctl() on a socket under winsock - you have to use closesocket and ioctlsocket.

##### Share on other sites
The MAKEDWORD() argument is the version of winsock you want. Trust me: you want version 2.2 :-)

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631067
• Total Posts
2997740
×