Solved

Reading from a socket

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 163
substring method in java 1 136
x-bar in Google Sheets 2 93
Problem to App source 6 92
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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