0

I have basically implemented this asynchronous server socket example (and the corresponding client). Using it, I can respond to the client if I follow the example exactly, i.e., if the call to Send() the response is in the ReadCallback() method.

If, however, I try and send a response outside of here, i.e., in a callback that I've attached to my message processing routine (running in a different thread), I get this error, saying the Socket is not connected. If I try and send a response somewhere else in the Server code, say in the while(true) loop that's listening to incoming connections, I get the same error.

Am I missing something fundamental here?

Edit:

Ok, so I read Two-way communication in socket programming using C, and I now think, according to that answer, that I have to modify the example I linked to so that I reply to the server on the socket returned by the accept process. My goal is to be able to call Send() outside of the receive callback, say from Main(), after the client and server are connected.

Please can someone suggest how I modify the example to achieve what I want? I'm getting thoroughly confused about this, and don't want to create a separate stream if I don't need to (which according to the question I posted, I don't need to...).

1
  • Posting the relevant code snippet before/after your modifications would help us understand better. Talk is good, but usually does not precisely describe the code. Commented Dec 12, 2013 at 15:45

1 Answer 1

1

If you want to keep the connection open then would need to persist the handler variable as that is the open socket connection. Then whenever you want to send that connection a message you retrieve its socket and send.

Also, you obviously wouldn't call Shutdown() and Close() on the handler variable.

Sign up to request clarification or add additional context in comments.

1 Comment

You, sir, are an absolute hero. That is the easiest mod I've made in weeks. This was definitely a case of "couldn't see the forest for the trees"! (FYI, it was the persistence I was missing).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.