Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UDP Sockets VC++

Status
Not open for further replies.

bajarich

Programmer
Jan 14, 2005
3
US
Hi,

I need to create a function in an visual c++ application which will send a specific UDP packet once every second. Within the function(or outside of it actually), I need to listen for a response from a remote computer. Basically it is a message which request statistics from a device, the device will send back a message containing relevant information. Presently I need to verify that the response was sent. I have quickly written some code, but it's problematic. The code ties the CPU up.

CString TempStr;
signed wait_for_response_delay; // in milliseconds
char RcvBuf[MAXPKTSIZE];
int RcvByteCnt;
bool proceed = true;

StatMsgCnt = 0;
StatMsgCnt2 = 0;

RequestStatMsg ReqStatMsg;

// Setup status request message
ReqStatMsg.MsgID = REQUEST_STAT_MSG;
ReqStatMsg.MsgSize = sizeof(RequestStatMsg);

while(true){ // break on button

// Send data packet and close connection
Socket.Send(&ReqStatMsg, sizeof(RequestStatMsg));

StatMsgCnt2++; // update the sent message counter

wait_for_response_delay = 999;

wait_for_response_delay += clock(); // sync with clock

// Check for new messages

while(clock() < wait_for_response_delay){
RcvByteCnt = Socket.Receive(&RcvBuf, MAXPKTSIZE);
} // This is the culprit

// If found, update GUI
if ( RcvByteCnt > 0){
StatMsgCnt++; // update the received message counter
}
else{// Display time and error message to scrolling
textbox}
.
.
.
I'm not much of a windows programmer. Is there a polling function that I can use here? I'd like the program not to be locked up as well, so I can push a 'stop test' button and end the test.

Oh, and while am at it, what's the best method to display the output to a scrolling text box for analysis?

Any suggestions would be appreciated. Thanks!

Rich




 
VC++.NET Socket API - Check the poll method

You should use the poll method to determine if the socket has any input data.
So something along the lines of
Code:
  Socket.Send(&ReqStatMsg, sizeof(RequestStatMsg));
  
  StatMsgCnt2++; // update the sent message counter

  if ( Socket.poll(1000000,SelectRead) ) {
    RcvByteCnt = Socket.Receive(&RcvBuf, MAXPKTSIZE);
    if ( RcvByteCnt > 0 ) {
      StatMsgCnt++; // update the received message counter
    } else {
      // Device closed the connection
      // Display time and error message
      // to scrolling textbox
    }
  } else {
    // Device failed to respond in time
    // Display time and error message
    // to scrolling textbox
  }

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top