Solved

Reading from a socket

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

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
Expand data scrubbing tool 13 32
Expand to include initial dialog with two choices. 9 67
Shell script to login to remote m/c and execute commands 10 78
unix example issues 18 75
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

13 Experts available now in Live!

Get 1:1 Help Now