Solved

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

Posted on 2000-02-22
2
199 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

636 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