Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:06:01+00:00 2026-06-14T11:06:01+00:00

My app communicates with a server over TCP, using AsyncSocket. There are two situations

  • 0

My app communicates with a server over TCP, using AsyncSocket. There are two situations in which communication takes place:

  1. The app sends the server something, the server responds. The app needs to read this response and do something with the information in it. This response is always the same length, e.g., a response is always 6 bytes.

  2. The app is “idling” and the server initiates communication at some time (unknown to the app). The app needs to read whatever the server is sending (could be any number of bytes, but the first byte will indicate how many bytes are following so I know when to stop reading) and process this information.

The first situation is working fine. readDataToLength:timeout:tag returns what I need and I can do with it what I want. It’s the second situation that I’m unsure of how to implement. I can’t use readDataToLength:timeout:tag, since I don’t know the length beforehand.

I’m thinking I could do something with readDataWithTimeout:tag:, setting the timeout to -1. That makes the socket to constantly listen for anything that’s coming in, I believe. However, that will probably interfere with data that’s coming in as response to something I sent out (situation 1). The app can’t distinguish incoming data from situation 1 or situation 2 anymore.

Anybody here who can give me help me solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:06:02+00:00Added an answer on June 14, 2026 at 11:06 am

    Your error is in the network protocol design.

    Unless your protocol has this information, there’s no way to distinguish the response from the server-initiated communication. And network latency prevents obvious time-based approach you’ve described from working reliably.

    One simple way to fix the protocol in your case (if the server-initiated messages are always less then 255 bytes) – add the 7-th byte to the beginning of the response, with the value FF.

    This way you can readDataWithTimeout:tag: for 1 byte.
    On timeout you retry until there’s a data.
    If the received value is FF, you read 6 more bytes with readDataToLength:6 timeout: tag:, and interpret it as the response to the request you’ve sent earlier.
    If it’s some other value, you read the message with readDataToLength:theValue timeout: tag:, and process the server-initiated message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Android client app that communicates with server using Socket . On my
I have an iPhone app that communicates with a server (both of which I
I have an android application which communicates over plain tcp/ip (no http) connection with
I am building an Android App that will communicate over TCP with a server.
I am working on an iPhone App which communicates with a Server to store
I'm creating an app which communicates alot with a server. I want to make
I'm designing an iPhone app that communicates with a server over HTTP. I only
I am working on an app with communicates with our server. There is a
I am developing an app, in which c sharp server communicates with android client.
I have a simple client-server app on android. the android service communicates with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.