Solved

Replacing CCeSocket with CSocket or CAsyncSocket

Posted on 2009-05-13
7
1,082 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
ID: 24377020
What error code do you get using 'GetLastError()'?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24377322
>>>> 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
ID: 24377496
>>>> 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:Wanting2LearnMan
ID: 24379174
The error I get is WSAEWOULDBLOCK.  
0
 

Author Comment

by:Wanting2LearnMan
ID: 24379234
>>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
ID: 24379824
>>>>>      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
ID: 24380454
>>>> Replacing CCeSocket with CSocket or CAsyncSocket

The docs of Visual2005 say

>>>> CCeSocket functionality is encapsulated in CAsyncSocket
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

838 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