Solved

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

831 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