Solved

Replacing CCeSocket with CSocket or CAsyncSocket

Posted on 2009-05-13
7
1,046 Views
Last Modified: 2013-12-21
I have replaced CCeSocket with CSocket in my Windows Mobile application and It seems to work well.

However I wish to use non-blocking sockets so tried using CAsyncSocket but I cannot conect to my server.

Why could this be?
0
Comment
Question by:Wanting2LearnMan
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
What error code do you get using 'GetLastError()'?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
>>>> However I wish to use non-blocking sockets
Don't think the CAsyncSocket supports non-blocking sockets. What it does is to create threads to make it asynchronous. But the thread would call select with timeout in order to not being blocked. So it is more a polling thing rather than setting the socket  non-blocking.

>>>> tried using CAsyncSocket but I cannot conect to my server.
I am currently on a VS2003 and the docs say

---------------
A CAsyncSocket object represents a Windows socket, which is an endpoint of network communication. The CAsyncSocket class encapsulates the Windows CE Sockets API, providing an object-oriented abstraction for programmers who want to use Windows CE Sockets in conjunction with MFC.

This class is based on the assumption that you understand network communications. You are responsible for handling blocking, byte-order differences, and conversions between Unicode and multibyte character set (MBCS) strings. If you want a more convenient interface that manages these issues for you, see class CSocket.

To use a CAsyncSocket object, call its constructor, and call the Create function to create the underlying socket handle (type SOCKET), except on accepted sockets. For a server socket, call the Listen method, and for a client socket call the Connect method. The server socket should call the Accept method upon receiving a connection request. Use the remaining CAsyncSocket methods to carry out communications between sockets. Upon completion, destroy the CAsyncSocket object if it was created on the heap; the destructor automatically calls the Close function.

Because Windows CE does not support asynchronous event notification, use the CCeSocket class instead of CAsyncSocket when writing an application for Windows CE. The CCeSocket class enables Windows CE applications to send and receive socket notifications for certain asynchronous events.

Remarks
MFC for Windows CE does not fully support asynchronous event notification, so it does not support the AsyncSelect method of the CAsyncSocket class. The new CCeSocket class does support asynchronous notification for certain socket events.

MFC for Windows CE does not support the GetLastError method of the CAsyncSocket class. Use the global GetLastError function instead.
-----------------

That means differentlly to using CSocket you have to
 
  - call the CAsyncSocket::Create  function to get a socket handle
  - call the CAsyncSocket::Connect (after Create was successful)

Did you already made these 2 calls?




0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 500 total points
Comment Utility
>>>> Don't think the CAsyncSocket supports non-blocking sockets.
I was wrong. You can set it to non-blocking by calling CAsyncSocket::IOCtl

    // switch to non-blocking
    DWORD on = TRUE;
    mySock.IOCtl(FIONBIO, &on);    

   // switch to blocking
    DWORD on = FALSE;
    mySock.IOCtl(FIONBIO, &on);

Note if setting to non-blocking almost all successive calls might return with error and GetLastError would give WSAEWOULDBLOCK.

In that case you either need to poll until there is no error (do a  Sleep(1); when polling to not heat-up your processor and give other threads some CPU as well) or in case of Connect() - which shouldn't be called more than once - you would need to overwrite the OnConnect handler function with your class derived from CAsyncSocket. Then, if the Connect was completed the framework would inform you by calling OnConnect member function.

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Wanting2LearnMan
Comment Utility
The error I get is WSAEWOULDBLOCK.  
0
 

Author Comment

by:Wanting2LearnMan
Comment Utility
>>That means differentlly to using CSocket you have to
  >>- call the CAsyncSocket::Create  function to get a socket handle
  >>- call the CAsyncSocket::Connect (after Create was successful)
>>Did you already made these 2 calls?

Yes my code is as follows

CAsyncSocket *sock;
sock = new CAsyncSocket;
sock->Create();

if (sock->Connect(sIpAddress, nPort) == FALSE)
{
       DWORD dwErr = GetLastError();    ////The error I get is WSAEWOULDBLOCK
       //blah blah
}
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 500 total points
Comment Utility
>>>>>      DWORD dwErr = GetLastError();    ////The error I get is WSAEWOULDBLOCK

As told, you would need to continue and wait until OnConnect would be called after the connect was completed

if (sock->Connect(sIpAddress, nPort) == FALSE)
{
      DWORD dwErr = GetLastError();    ////The error I get is WSAEWOULDBLOCK
      if (dwErr != WSAEWOULDBLOCK)
      {
            AfxMessageBox(CString("Connection failed ") + itoa(dwErr, CString().GetBuffer(32), 10));
            return FALSE;
      }
      return TRUE;   // go back to message loop
}

void MyAsyncSocket::OnConnect(UINT errCode)
{
     if (errCode == 0)
        myIsConnected = TRUE;
    else
    {
          AfxMessageBox(CString("Connection failed ") + itoa(errCode, CString().GetBuffer(32), 10));
    }
}

To actually read and write from socket you would check the BOOL member myIsConnected of yor class MyAsyncSocket derived from CAsyncSocket.

 

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 500 total points
Comment Utility
>>>> Replacing CCeSocket with CSocket or CAsyncSocket

The docs of Visual2005 say

>>>> CCeSocket functionality is encapsulated in CAsyncSocket
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now