Solved

I have a function which sends a text string to a socket.

Posted on 2000-02-22
2
198 Views
Last Modified: 2010-04-02
I have a function which sends a text string to a socket.
I only want the function to return when I recieve a reply from the socket.

Any ideas how i do this?


further details

function x
{
  m_socket->Send("some text");

 return socket's (m_socket->Reply())
}
0
Comment
Question by:yakky
[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 Comments
 
LVL 1

Accepted Solution

by:
JMu earned 100 total points
ID: 2547074
The correct answer depends on the protocol you use.

int SendStringAndReceiveReply( const char* string, char* replyBuffer, int replyBufferLength )
{
  int bytes = strlen( string );
  int bytesWritten = m_socket->Send( string, bytes, 0 );

  if ( bytesWritten != bytes )
    return -1;

  // then read a reply - I assume you use blocking socket

  int bytesReceived = m_socket->Recv( replyBuffer, replyBufferLength, 0 );

  return bytesReceived;
}

This is a problem if server never send a reply. For that you could use wait timeout and ioctlsocket( FIONREAD ... ) in a loop.

I would use async socket and wait FD_READ. See WSAAsyncSelect and WSAEventSelect. These are Win32-functions. If you don't use Windows, you may use select()-funtion, I think.

JMu
0
 

Author Comment

by:yakky
ID: 2549741
Thanks very much its done the trick
Yakky
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

751 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