Sending data over sockets

Posted on 2006-03-24
Last Modified: 2010-04-23
Hello experts,

When sending data from server to client or vice versa, what is the best way to notify the other of the 'nature' of the data? In otherwords, consider the following scenario:

Client sends files to server for processing. It also needs to send periodic status requests whereby the server sends back its progress with a particular file, i.e. how much of it it has processed.

In this scenario, what's the best way for the client to say: "The data I am about to Send() is a file", or "The data I am about to send is a progress request"?

I have considered using the first byte of the message as a notify byte, e.g. 1 = I am sending a file, 2 = I am sending a status request, etc...

The only other way I can think of is using two sockets, one for file transfers and one for status requests. I was just wondering what was the best way of carrying out such a task.
Question by:butterhook
    LVL 5

    Accepted Solution

    I think you are on the right track with both ideas. Usually when I do tasks like this I’ll setup a small set of commands that the server can speak to the client with and the client can speak to the server with.

    Much like your first byte idea you can signal the server to expect a certain type of data next.

    I have found that this has worked well for me.

    To take this idea to the next step I’ll create a command abstract class. I’ll then inherit from this class for each command I create, such as status update, or file transfer.

    Then I implement binary serialization on this object and serialize it to a stream, which I then send out to the server.

    I usually send the size of the serialized stream first so the server knows how much data to read before it attempts to de-serialize the command.

    Now when the server gets all the bytes it de-serializes the stream and then takes action on the request.
    LVL 5

    Expert Comment

    I would like to point out that in my above idea you would need to create a DLL holding the command classes that can be shared by both the client and the server. This may or may not be possible for you.
    LVL 1

    Author Comment

    Thank you. Your command class framework seems like an excellent way to go about it, and also answers another question I had about how to nicely encapsulate message formation and extraction instead of using the System.BitCoverter class all the time.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now