CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2000
    Posts
    6

    Error when closing a socket and reconnecting

    Following problem:

    I created a Socket (CSocket instance), bound it and connected to host.
    After data exchange, I closed the Socket (x.close ())
    Now, I want to reuse this Socket to establish a new connection to another host - but the programm terminates with an assert.

    Is the socket still in use, even if call x.close() ?
    (I also tried x.ShutDown (2) - same problem!)
    How can I close/free the socket?



  2. #2
    Join Date
    Nov 1999
    Location
    NY
    Posts
    906

    Re: Error when closing a socket and reconnecting

    Do the following after you've called CSocket::Create() and before you call CSocket::Accept(). If you call CSocket::Bind()(doesn't look like it), do the following before Bind().


    CSocket mySocket;

    mySocket.Create(...);
    ....
    int nOptionValue = 1;
    int nOptionLen = sizeof(nOptionValue);
    mySocket.SetSockOpt(SO_REUSEADDR, (void *)&nOptionValue, &nOptionLen, SOL_SOCKET);
    ...
    mySocket.Accept(...);
    ....




    Kailash


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