Winsock2.h

This is a discussion on Winsock2.h within the Windows Programming forums, part of the Platform Specific Boards category; I am new to Winsock programming. I am more used to pure POSIX style network programming (like in Stevens' books). ...

  1. #1
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Question Winsock2.h

    I am new to Winsock programming. I am more used to pure POSIX style network programming (like in Stevens' books).

    I need some help with this code, which for some Microsoft reason doesn't work with Winsock 2.2 but work fine under any UNIX platform. Could someone please explain to me what the difference is? Here is the code:

    #include <winsock2.h>
    ...

    SOCKET sockfd;

    ...

    // get the socket
    sockfd = socket(PF_INET, SOCK_DGRAM, 0);
    if( sockfd != 0 ) {
    Bail("socket()");
    exit(-1);
    }
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  2. #2
    Out on Bail
    Guest
    Be sure to call WSAStartup before using any Windows Socket functions.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Also make sure the proper winsock2 lib is used (WS2_32.lib).


    >>Bail("socket()");

    I'm not familiar with this...what are you trying to do?

  4. #4
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    yeah WSAStartup is missing, but you also have to know if your network card has datagram capabilities(It's just a suggestion), I would rather use SOCK_STREAM
    by the way, I'm not sure you should check if it's different from 0, try
    if(sock==INVALID_SOCKET)
    {
    return -1;
    }
    else
    {
    //do anything here...
    }

  5. #5
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Thanks. I am going to try all those things when I have some free time. Too busy at work right now.

    As far as "Bail()" goes - what it does is display a related error message, where the header is whatever string I pass as the argument. I like doing it this way because it save a lot of time trying to display the proper error message.

    Anyways, Thank You, Guys.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2009, 02:57 PM
  2. Winsock.h and Winsock2.h
    By azteched in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-24-2004, 07:42 PM
  3. winsock2.h
    By Carp in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 02:42 PM
  4. Errors when including winsock2.h
    By skiingwiz in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2002, 07:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21