Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2000-02-22
2
Medium Priority
?
200 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

715 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