Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CAsycnSocket issues

Status
Not open for further replies.

minifiredragon

Programmer
Jun 29, 2003
68
0
0
US
The program I have built works about 95% of the time without crashing. I have placed error codes on the OnConnect, OnRecieve, OnSend and OnAccept and there are no errors. It appears the problem occurs between:

m_strCardNumber = dlgInsertCard.m_strCardNumber;
if (m_strCardNumber != "0")
{//2
m_strCardNumber = "1" + m_strCardNumber;
//start code for socket
//create a default socket
m_sConnectSocket.Create();
//open connection to server


//////Problem between here -->
m_sConnectSocket.Connect(m_strIpAddy, m_iPort);
//send Card number
int iLen;
int iSent;

iLen = m_strCardNumber.GetLength();
///// and here -->


iSent = m_sConnectSocket.Send(LPCTSTR(m_strCardNumber), iLen);

//check for errors
if (iSent == SOCKET_ERROR)

When debugging I get this error message:

Unhandled exception at 0x7c2f09c5 (mfc71d.dll) in ICServer.exe: User breakpoint.

And breaks into this code at:

BOOL CAsyncSocket::Accept(CAsyncSocket& rConnectedSocket,
SOCKADDR* lpSockAddr, int* lpSockAddrLen)
{


/// BREAK POINT HERE ---->
ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET);



ASSERT(CAsyncSocket::FromHandle(INVALID_SOCKET) == NULL);

CAsyncSocket::AttachHandle(INVALID_SOCKET, &rConnectedSocket);
if (CAsyncSocket::FromHandle(INVALID_SOCKET) == NULL)
{
// AttachHandle Call has failed
return FALSE;
}

I just caught an OnClose error on the server end with giving a code 10053.


Right now I am not sure where to look to find the starting point of the error. Like I said it all works well 95% of the time.
 
One other issue is, if I step through it slow, I don't crash, but at regular speed there is a problem, but like I said, not all the time.
 
Don't use MFC, personally (thank god!). But I did notice this:

>>m_sConnectSocket.Send(LPCTSTR(m_strCardNumber), iLen);

If m_strCardNumber is an std::string, you shouldn't be casting it to a char*, since the object doesn't define an operator char*() function (implicit conversion operator).
 
it is a CString. Everything works accept for a little bug that crashes the program every so often.
 
>> Everything works accept for a little bug that crashes the program every so often.

Most of the time that type of behavior indicates a memory corruption bug somewhere in the project.

-pete
 
It doesn't crash the release version of the application. In debug mode I laid out where it breaks to in the application in my 1st post.

...
/// BREAK POINT HERE ---->
ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET);
...

I did considerer data corruption, but in stepping through the code line by line I can never produce the error, only when it runs at full speed does the error occur, and all the returns are as they should. I will look closer though to see if perhaps something is corrupting something else.
 
You can create a breakpoint that will fire with the value of m_hSocket changes. That might help you locate the event that is triggering the change.

-pete
 
I wanted to bring this back up because I found a solution. I actually had 1 error in the code, at one of the points I never closed the connection. After I fixed it, I still had the same problem with the program only working 95% of the time, the rest it never seems to either send of recieve, so here is what I did, and it did fix it:

snip--->

m_sConnectSocket.Create();
//open connection to server
m_sConnectSocket.Connect(m_strIpAddy, m_iPort);


//I added this
PauseTimer();
//end of new

//send Card number
int iLen;
int iSent;

iLen = m_strCardNumber.GetLength();
iSent = m_sConnectSocket.Send(LPCTSTR(m_strCardNumber), iLen);


// here is the pausetimer:

void CICClientDlg::pauseTimer(void)
{
pausecount = 0;
while (pausecount < 1000 )
{
pausecount++;
}
return;
}


quite simple know?? It seems the connection and send was happening faster then something on my computer could process it (doesn't make sense I know because it is not truly multi tasking). Just adding that slight delay let the program function as it should.

Any clue as to why I needed it??
 
CAsyncSocket is asynchronous. The Connect() function returns immediately not after the connection is established. You need to override the OnConnect() function and perform the Send() in there. You also need to check the return value and if it is false use GetLastError() to retrieve error information.

They hide all these facts in the documentation
-pete
 
lets try that again:
Code:
[URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_casyncsocket.3a3a.connect.asp[/URL]

-pete
 
I see...

But I read through that section and there was nothing about what u mention above.
 
Hu?

Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. If this indicates an error code of WSAEWOULDBLOCK, and your application is using the overridable callbacks, [red]your application will receive an OnConnect message when the connect operation is complete.[/red] The following errors apply to this member function:


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top