Send TCP request to server and wait for response or timeout

Hi, all!

I've read this post and it helped get me started. My application needs to send a request to a server and receive a response. If no response is received within some timeout, it should return an error string.

Apple’s TCP sample code

More specifically, the server will usually return a string with something like '200 ok' or perhaps data specific to an inquiry request.

The 'send' and 'startReceive' functions work perfectly, but the application needs to wait for the response and interpret the data before continuing.

I guess the ideal would be to create a wrapper function around the 'send' and 'startReceive' that would return the String response or an error message if the response was not received before a timeout. So pass in a request string and timeout seconds value and receive a string. I have no idea what the proper way of doing this.

I didn't see any Tags for network related questions, so macOS it is!

Thanks for reading!

Pete

Send TCP request to server and wait for response or timeout
 
 
Q