Solved

Reading from a socket

Posted on 1998-07-06
5
157 Views
Last Modified: 2013-11-20
How do I use OnRecieve to read from a socket so that I can execute a block of code whenever
some data comes in on the socket?

Please be very detailed and specific, as I have little experience.
0
Comment
Question by:zorker
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
zingo earned 50 total points
ID: 1318745
Just call

 int Receive( void* lpBuf, int nBufLen);

in your OnRecieve() method. lpBuf should be a pointer to a
buffer where you want the incoming message to be stored. nBufLen is the length ot the buffer (lpBuf) that you supplies.

Example:

void pBuf[16];

if(Receive(pBuf, 16) > 0)
{
    // Handle the received message here
}

Be aware that if the incoming message in this case is shorter than 16 bytes, you probably had to wait until more messages arrives to fill out up the buffer before it returns.

To avoid this problem, I recommend that you use a message format where you always have a minimum size. This minimum size could contain the length of the message and for example a message number. If this minimum size is 4 bytes, then you always start with reading 4 bytes, find out the length of the message, and call Receive once more to get the rest of the message. Then you can make a call to your block of code to process the incoming message.
0
 

Author Comment

by:zorker
ID: 1318746
I have no control over the message format.  Basically what I want to happen is whenever
anything comes in on the IP socket, I want to write it to a com port (which I already
have all set up).  Should I just do it one byte at a time?

Also, regarding OnReceive:  Where do I call it or whatever?
Should I extend CAsyncSocket or can I just override an existing version
or what?  Also, will it automatically be called when data comes in, or do
I have to do something to the message map?

0
 

Author Comment

by:zorker
ID: 1318747
What happens if I read less than the amount of data on the buffer?
Does the rest get discarded, or does it move to the front of the buffer?
0
 

Expert Comment

by:zingo
ID: 1318748
Unfortunately, I don't know a way to find out how many bytes is in the in-buffer. So, if you don't know anything about the size of the messages you will receive, I guess the safest way is to read one at a time.

Make your own socket class, which inherits from CAsyncSocket. Then override the OnReceive method with your own. It will be called automatically.

If you read less data than is in the in-buffer, the rest will remain in the in-buffer. You will receive the following bytes with your next call to Receive.
0
 

Expert Comment

by:acanito
ID: 1318749
use the function AsyncSelect whit the flag FD_READ. The code is

void CChatClientDoc::Conecta()
{
      m_pSocket = new CChatSocket(this);

      if (!m_pSocket->Create())
      {
            delete m_pSocket;
            m_pSocket = NULL;
            AfxMessageBox(IDS_CREATEFAILED);
      }
      m_pSocket->Connect(m_sServer, m_uPort);
      m_pSocket->AsyncSelect(FD_READ | FD_CONNECT | FD_CLOSE );
}

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

23 Experts available now in Live!

Get 1:1 Help Now