Click to See Complete Forum and Search --> : Error Message WSAECONNABORTED 10053 problem


TheSheik
January 5th, 2005, 06:35 PM
Hello Everyone,

Ive been having a bit of trouble with this particular error message in my project. Any of your guru advice would be greatly appreciated The project I created is basically a mulithreaded server application to send/receive data via different connections. Initially a listening socket is setup in one thread, whenever a connection is received a temporary casyncsocket object inside the onAccept function is created to store the accepted connection, then inside that same function a new thread is created and the accepted connection passed to that new thread via the detach and attach member functions of the casynsocket.

The problem comes when data is sent to the newly accepted connection. As soon as I try to read the data via the receive function, receive returns the 10053 error (the receive function is called via an overriden OnReceive function of casyncsocket). I have tried everything to figure out why the socket is (I am guessing) being closed right away after the connection. I have disabled the firewall, etc. Anybody have any good pointers to follow to properly use casyncsockets across multiple threads or to point out something that I am doing wrong, any help would be appreciated. Thank you.

-David

TheSheik
January 5th, 2005, 07:44 PM
Another point of clarification, the connected socket can send data perfectly fine, it is only when data is sent to it, and the receive function called that the socket actually closes (I overrided the onClose notification function) So ...

Socket Accepted
Accepted Socket Sends Data fine
Data sent to Socket
OnReceive Called
receive called
receive function gives 10053 error
OnClose function of socket called

drewdaman
January 5th, 2005, 10:45 PM
don't really know.. but i was getting error 10054 a little while ago and someone suggested that i try another computer. i'm not saying that doing that will solve your problem.. but i found wiht my code that when i run the client on one computer and the server on another it works, but when i try to test stuff locally, it just doesn't work! i was also having trouble at recv. just give it a shot and hope for hte best i guess.

MikeAThon
January 6th, 2005, 08:15 PM
Please post some code, particularly code for the OnAccept handler in your main server's thread, and the thread's InitInstance code.

I think that you created a UI thread (and not a worker thread), since otherwise it would be difficult to get your OnReceive notifications. Correct?

Also, you mentioned that the newly-accepted connection can send data fine (but can't receive it). Does it send the data before it's handed off to the thread, while it's still in the main server's OnAccept handler, or afterwards?

Mike

TheSheik
January 7th, 2005, 03:59 PM
Hi Mike, thanks for your response

I think that you created a UI thread (and not a worker thread), since otherwise it would be difficult to get your OnReceive notifications. Correct?

Yes you are correct, I created a UI thread class derived from CWinThread. Inside this class is a CMySocket Object (my class derived from CasyncSocket) which handles the communications. The code for the UI thread initinstance is below.

BOOL CSockThread::InitInstance()
{
AfxMessageBox ("InitInstance");
// TODO: perform and per-thread initialization here
//Attach the connected socket to our socket and say that we want events for Read
//Write and Close
m_theSocket.Attach(m_acceptSockHandle, FD_READ | FD_WRITE | FD_CLOSE | FD_ACCEPT);
m_theSocket.Send ("hello", 6);
m_theSocket.setParentPtr(m_parentPtr);

return TRUE;
}

Please post some code, particularly code for the OnAccept handler in your main server's thread, and the thread's InitInstance code.
Here is the code from the OnAccept function in the CMySocket class (my class derived from the casyncsocket class).

void MySocket::OnAccept(int nErrorCode)
{
AfxMessageBox ("receiving: OnAccept");
if(nErrorCode){
CString errorString("ERROR - Listening Socket cannot accept connection, error number = ");
errorString.AppendFormat(_T("%d"),nErrorCode);
AfxMessageBox(errorString,MB_OK);
CAsyncSocket::OnAccept(nErrorCode);
}

CAsyncSocket soc;
// Accept the connection using a temp CSocket object.
Accept(soc);

// Create a thread to handle the connection.
// The thread is created suspended so that we can
// set variables in CConnectThread before it starts executing.
CSockThread * pThread =
(CSockThread *)AfxBeginThread(
RUNTIME_CLASS(CSockThread),
THREAD_PRIORITY_NORMAL,
0,
0);

// Pass the socket to the thread by passing the socket handle.
// You cannot pass a CSocket object across threads.
pThread->m_acceptSockHandle = soc.Detach();
pThread->m_parentPtr = parentPtr;

//Add the thread to the parentPtrs thread list of connected sockets
parentPtr->m_addConnSockThreadPtrToList(pThread);

// Now start the thread.
//pThread->ResumeThread();

CAsyncSocket::OnAccept(nErrorCode);
}



And finally the OnReceive function of the CMySocket class



void MySocket::OnReceive(int nErrorCode)
{
// for testing purposes only
char buffer[1024];
char buffer2[1024];

CString thisString("OnReceive - nErrorCode value is = ");
thisString.AppendFormat(_T("%d"),nErrorCode);
AfxMessageBox(thisString,MB_OK);

Send (" hello2 ", 9);
nRead = Receive (message, 1024);
//AfxMessageBox ("after receiving: OnReceive");
//sprintf (buffer, "%ld", (*((long *)message)));
//sprintf (buffer2, "$d", msgChunkNo);
//AfxMessageBox (buffer);

if (nRead == SOCKET_ERROR)
{
int lastError = GetLastError();
if (lastError != WSAEWOULDBLOCK)
{
CString errorString("ERROR - Receiving Socket cannot accept connection, error number = ");
errorString.AppendFormat(_T("%d"),lastError);
AfxMessageBox(errorString,MB_OK);

//AfxMessageBox ("Error occurred when receiving");
return;
}
}


So basically it sends hello, wait for data to be received, sends hello2 after data has been sent to it, then calls the onclose() function closes the connection then finishes the OnReceive function. Any help would be greatly appreciated I am thinking it might have something to do with the receive buffers not setup properly maybe because we are passing the SOCKET handle badly across the thread boundaries?? Thanks again.

-David

TheSheik
January 7th, 2005, 06:09 PM
Ok, problem solved. The moral of the story is check your partners code, and make sure that a buffer given to receive is actually allocated before trying to use it!!

-David

MikeAThon
January 7th, 2005, 06:24 PM
I'm glad your problem is solved!

Incidentally, in your thread start-up, I would indeed create the thread suspended, as your comments indicate (but which the flags to AfxBeginThread do not implement).

Mike