0

So i have a simple tcp client/server setup that sends objects back and forth and it works all good.

But let's say i now want to make a call and ask the server for a special set of lets say "products" and i need to know what "answer" of the incoming messages is the one i specially asked for.

Since the loop that receives messages just checks what type they have and react to it it's hard to say "this message is the one you asked for".

Example:
- Button is clicked
- Message is send to the server
- A few other messages arrive why i wait for the answer
- The right message arrives <- How to know its the "right" one?
- Call the function that i want to
- Update something in my form

Hope it's clear what i meant.

phil
  • 137
  • 12
  • 2
    If you are writing such low level communication, add some unique `messageId`, which is send with request and arrives back from server. – Konrad Kokosa Jul 29 '14 at 08:05
  • possible duplicate of [how do i get TcpListener to accept multiple connections and work with each one individually?](http://stackoverflow.com/questions/5339782/how-do-i-get-tcplistener-to-accept-multiple-connections-and-work-with-each-one-i) – Leo Chapiro Jul 29 '14 at 08:06
  • @Konrad Kokosa Low level communication? What would be a better approach? – phil Jul 29 '14 at 08:20
  • Asynchronous `HttpClient` for example. – Konrad Kokosa Jul 29 '14 at 08:26

2 Answers2

1

I'd recommend to design your messaging as followed:

  1. use serializable objects (XmlSerializer) as shown here
  2. this object should have two "main" properties: the payload itself and an identifier to get "what kind of message this is", and maybe add some requestId property (to know that this message is for you, if you are listening to a broadcasting service)
Community
  • 1
  • 1
Matthias R.
  • 441
  • 2
  • 15
1

You can use the WCF framework to process requests, it handles the serialization & message parsing over HTTP/TCP.

dvasanth
  • 1,337
  • 1
  • 9
  • 10