Solved

Reading from a socket

Posted on 1998-07-06
5
188 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
Folder Comparison 12 54
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 55
deburging in oracle form 12 104
withoutTen challenge 14 130
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

789 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