tylerfb11
tylerfb11

Reputation: 43

C++ winsock gives 10038 error on bind()

um, first post here, this place seems to be all over google and i can usally find my solution with having to acually ask a question my self in any site/forums; but if i sweat any more bullets over this ima hunt down whoever developed winsock and shoot them (sorry for the anger i think ive turned over every rock in every corrner of the net with no luck.... breeaatheee.... wheew)

Im new to network programming, but have been working with C++ for the last three years on a hobby level, and also been playing with AS3 recently.

Im trying to write a server (for the client with is the AS3 project im also working on) and as far as i can tell this SOCKET is perfectly fine. im not re-creating it, multi-threading with it, no re-assignment or anything. no funny bisuness. simply trying to set it all up and bind() is spitting out that nasty 10038 right in my face.

Ive looked on MSDN, and i know very well that 10038 means "attempted operation on an invalid socket"; for the life of me i cant see where its invalid.

but enough of my rambling, heres the code: (functions.h is empty, havnt got that far along yet)

 //Server for Project7 - Client written in AS3 under FlashDevelop. Developed under and for the Windows Operating System Enviroment
//All connections handled under TCP/IP on port 3011
//Client is URL locked to www.cutdev.com
//Copyright Tyler Buchinski 2012 All Rights Reserved
#include <iostream>
#include "functions.h"

#define WIN32_MEAN_AND_LEAN

#include <winsock2.h>
#include <windows.h>


        using namespace std;

int main()
{

    const int iReqWinsockVer = 2;   // Minimum winsock version required

WSADATA wsaData;

  if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
  {
    // Check if major version is at least iReqWinsockVer
    if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
    {

        SOCKET SocketListen;
        SocketListen = (AF_INET,SOCK_STREAM,IPPROTO_TCP);


            if(SocketListen == INVALID_SOCKET)
                {
                    cout << "ERROR - could not creaate listening socket." << endl;
                    system("pause");

                    return 4;
                }

            sockaddr_in Listener, Channel1;

            Listener.sin_family = AF_INET;
            Listener.sin_port = htons(3011);
            Listener.sin_addr.S_un.S_addr = INADDR_ANY;

            int err = bind(SocketListen,(sockaddr*)(&Listener),sizeof(Listener));
            if (!err == 0)
                {
                    cout << "Listener binding failed!" << endl;
                    cout << err << endl;
                    cout << WSAGetLastError();

                    return 3;
                }



    }
    else
    {
        // Required version not available
        cout <<"Required version of Winsock not installed." << endl;
    }

    // Cleanup winsock
    if (!WSACleanup() == 0)
    {
        // cleanup failed
        cout << "WSACleanup Failed!!" << endl;
        system("pause");
    }
  }
  else
  {
    cout << "WSA Startup failed!" << endl;
  }
    return 0;
}

Thanks in advance for any help! -Tyler

Upvotes: 1

Views: 4943

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74355

Error 10038 is WSAENOTSOCK:

An operation was attempted on something that is not a socket.

This error is returned if the descriptor in the s parameter is not a socket.

This happens since you omitted the call to socket() and SocketListen contains the value of the IPPROTO_TCP constant instead of a socket descriptor:

SocketListen = (AF_INET,SOCK_STREAM,IPPROTO_TCP);

should become:

SocketListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

Upvotes: 3

Related Questions