Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

14 Experts available now in Live!

Get 1:1 Help Now