Solved

Replacing CCeSocket with CSocket or CAsyncSocket

Posted on 2009-05-13
7
1,118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
A short article about problems I had with the new location API and permissions in Marshmallow
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

691 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