?
Solved

Reading from a socket

Posted on 1998-07-06
5
Medium Priority
?
206 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
[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
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
zingo earned 150 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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