CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Invalid socket

  1. #1
    Join Date
    Aug 2012
    Posts
    4

    Question 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

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Aug 2012
    Posts
    4

    Question Re: Invalid socket

    Quote Originally Posted by S_M_A View Post
    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...

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    Join Date
    Aug 2012
    Posts
    4

    Re: Invalid socket

    Quote Originally Posted by Skizmo View Post
    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.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Invalid socket

    Quote Originally Posted by wberoli View Post
    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

  7. #7
    Join Date
    Aug 2012
    Posts
    4

    Re: Invalid socket

    Quote Originally Posted by VictorN View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured