Solved

Replacing CCeSocket with CSocket or CAsyncSocket

Posted on 2009-05-13
7
1,053 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 128
how to split multiple lines delimiter : 8 77
Droid 4 15 102
Excel file not created as expected 7 46
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

932 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

11 Experts available now in Live!

Get 1:1 Help Now