Thread: WinSock Defined Function Error

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    15

    WinSock Defined Function Error

    Im having an error when returning an integer variable to a defined function that seems to be specified to return void. This is in the winsock.h header file for version 1.1. This problem lies in the function WSAGetLastError - which simply stores -1 in int nret. But, WSAGetLastError seems to return void in the included header file.

    Code:
    int nret;
    
    //...
    
      if(nret == SOCKET_ERROR) {
               
                        nret = WSAGetLastError;
                        ReportError = WSAGetLastError(nret, "bind()");
                        WSACleanup();
                        return NETWORK_ERROR; }
    
    //In winsock.h
    
    int PASCAL WSAGetLastError(void);
    Should I just edit the header file so that WSAGetLastError returns int? I.E. WSAGetLastError(int) instead of (void)?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148

    Re: WinSock Defined Function Error

    Originally posted by CPP-Null

    Should I just edit the header file so that WSAGetLastError returns int? I.E. WSAGetLastError(int) instead of (void)?
    Certainly not.

    Your code is wrong.Call WSAGetLastError with no parameters and then you get an integer (error status)
    Code:
    nret = WSAGetLastError();

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    15
    Oaf! Bad looking on me - I forgot to pass it in, your right ^^

    ::EDIT:: - running into some compiler errors. Aparently #define NETWORK_ERROR -1, and #define NETWORK_OK 0 are both out of scope later down in the coding. - I'm still having problems passing an integer to a void. I followed the tutorial i'm learning from to a tee. For the out of scope definitions since I was trying to located where they go out of scope which I didn't (Shows what an amateur I am ^^) - I just replaced them with -1, 0 respectavily. I'm also having linkage issues and issues with WinMain(HINSTANCE hInst, HINSTANCE hPrevInsnt, LPSTR lpCmd, int nShow) {.

    Code:
    #include <winsock.h>
    #include <windows.h>
    #include <stdio.h>
    
    #define Network_Error -1
    #define Network_OK 0
    
    void ReportError(int, const char *);
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
    
        WORD sockVersion;
        WSADATA wsaData;
        int nret;
        
         sockVersion = MAKEWORD(1, 1);
         
         WSAStartup(sockVersion, &wsaData);
         
              SOCKET listeningSocket;
              
                  listeningSocket = socket(AF_INET, 
                                           SOCK_STREAM,
                                           IPPROTO_TCP);
                                           
         if(listeningSocket == INVALID_SOCKET) {
              
              nret = WSAGetLastError();
              ReportError(nret, "socket()");
              
              WSACleanup();
              return Network_Error; }
              
              SOCKADDR_IN serverInfo;
              
                        serverInfo.sin_family = AF_INET;
                        serverInfo.sin_addr.s_addr = INADDR_ANY;
                        serverInfo.sin_port = htons(8888);
                        
         nret = bind(listeningSocket, (LPSOCKADDR) &serverInfo, sizeof(struct sockaddr));
         
               if(nret == SOCKET_ERROR) {
               
                        nret = WSAGetLastError();
                        ReportError(nret, "bind()");
                        WSACleanup();
                        return -1; }
                        
         nret = listen(listeningSocket, 10);
         
              if(nret == SOCKET_ERROR) {
              
                        nret = WSAGetLastError();
                        ReportError(nret, "listen()");
                        WSACleanup();
                        return -1; }
                        
         SOCKET theClient;
         
              theClient = accept(listeningSocket, NULL, NULL);
              
                   if(theClient = SOCKET_ERROR) {
                   
                        nret = WSAGetLastError();
                        ReportError(nret, "accept()");
                        WSACleanup();
                        return -1; }
                        
         closesocket(theClient);
         closesocket(listeningSocket);
         
         WSACleanup();
         return 0;
    }
    
        void ReportError(int errorCode, const char *whichFunc) {
            
             char ErrorMSG[92];
             ZeroMemory(ErrorMSG, 92);
             sprintf(ErrorMSG, "Call %s returned error %d", (char*)whichFunc, errorCode);
             MessageBox(NULL, ErrorMSG, "Socket Indication", MB_OK); 
                        
                         
    }
    I'm just a hobbiest - this isn't a project or anything of the sort.
    Last edited by CPP-Null; 05-25-2003 at 10:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  2. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM