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

Threaded View

  1. #1
    Join Date
    Mar 2004
    Posts
    382

    Problem with winsock

    I have a class which handels TCP/IP communication via a winsock, and I've got it to working for the most part ( I'm able to connect to the server and communicate with it). My application is a client application. First let me show you a very basic idea of what my socket class looks like:

    Code:
    // Header file
    #include <stdio.h>
    #include "winsock2.h"
    class MYSOCKET
    {
    private:
    	static char m_DestIP[16];		
    	static SOCKET m_socket;	
    public:
                    MYSOCKET();
                    bool Connect();
    }
    
    // Implementation File
    #include "MYSOCKET.h"
    
    bool MYSOCKET::Connect()
    {
                   // Initialize Winsock.
        WSADATA wsaData;
        int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
        if ( iResult != NO_ERROR )
            printf("Error at WSAStartup()\n");
    
        // Create a socket.
        m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    
        if ( m_socket == INVALID_SOCKET ) {
            printf( "Error at socket(): %ld\n", WSAGetLastError() );
            WSACleanup();
            return false;
        }
    
        // Connect to a server.
        sockaddr_in clientService;
    
        clientService.sin_family = AF_INET;
        clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
        clientService.sin_port = htons( 27015 );
    
        if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
            printf( "Failed to connect.\n" );
            WSACleanup();
            return false;
        }
        return true;
    }
    There are more methods in the class, but they are not important in this case. Anyway, I got this code straight from microsfts websiste:
    Microsoft code and it works fine when the server is on. But, once I turn the server off, when I call the connect method twice or three times back to back, I get an error message and the application shuts down. I get the error message when I'm running the release version of the application. It's the same message you get when any windows application hangs up and the error message pops up asking you to send an error report to microsoft. But, when I run the debug version of the code, nothing happens. I can call connect as many times as I want back to back, and it will just keep on trying to re-connect, which is the behavior that I want.

    By the way, the constructor and destructor don't do anything, and before I call connect(), I just creat an object
    Code:
      MYSOCKET mySocket
    and that's it, nothing else. Does anyone know a solution to this problem?

    As for the project settings, I havn't changed anything to the default settings other then adding the library WS2_32.lib both to the release and debug build, which is necessary for winsock2.

    As for the error message, when I click on debug, I get the following message "Unhandled exception at 0x73dd1351 (MFC42.DLL) in MyApp.exe: 0xC0000005: Access violation reading location 0x00000429." and the part of the Disassembly code where the error occurs is the following:

    --- map_pp.cpp -----------------------------------------------------------------

    CMapPtrToPtr::CAssoc*
    CMapPtrToPtr::GetAssocAt(void* key, UINT& nHashBucket, UINT& nHashValue) const
    // find association (or return NULL)
    {
    nHashValue = HashKey(key);
    nHashBucket = nHashValue % m_nHashTableSize;

    if (m_pHashTable == NULL)
    return NULL;

    // see if it exists
    CAssoc* pAssoc;
    for (pAssoc = m_pHashTable[nHashBucket]; pAssoc != NULL; pAssoc = pAssoc->pNext)
    {

    if (pAssoc->key == key)
    return pAssoc;

    }
    return NULL;
    }


    73DD1350 push esi
    73DD1351 mov esi,dword ptr [ecx+4] <<<<<<<< Error here >>>>>>>>>>>
    73DD1354 test esi,esi
    73DD1356 push edi
    void* CMapPtrToPtr::GetValueAt(void* key) const
    // find value (or return NULL -- NULL values not different as a result)
    73DD1357 je CMapPtrToPtr::GetValueAt+2Ch (73DD137Fh)
    {
    if (m_pHashTable == NULL)
    73DD1359 mov edi,dword ptr [esp+0Ch]
    73DD135D xor edx,edx
    73DD135F mov eax,edi
    73DD1361 shr eax,4
    73DD1364 div eax,dword ptr [ecx+8]
    return NULL;

    Thanks in advance.
    Last edited by kodeguruguy; June 22nd, 2004 at 05:39 PM.

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