Advertisement

Mysterious Socket Error

Started by June 09, 2005 10:08 PM
3 comments, last by hplus0603 19 years, 8 months ago
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[])
{
    struct sockaddr_in scAdr; 
    int sockfd, sError;
    
    scAdr.sin_family = AF_INET;        
    scAdr.sin_port = htons(5432);
    scAdr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(&(scAdr.sin_zero), '\0', 8);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    printf("Socket Result: %d\n",sockfd);
    printf("Errno: %d\n",errno);
    perror("socket");
    
    sError = bind(sockfd, (struct sockaddr*)&scAdr, sizeof(struct sockaddr));
    
    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 :)
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


Also make sure that the first header your program includes (or the first header your precompiled header includes) is <winsock2.h>, and link with ws2_32.lib.
enum Bool { True, False, FileNotFound };
Advertisement
*smacks forehead*

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
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.
-Mike
The MAKEDWORD() argument is the version of winsock you want. Trust me: you want version 2.2 :-)
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement