Converting & Sending Sockets Data with a Leading Length Value

Most articles that illustrate how to send data via sockets tend to focus on sending and receiving simple textual data. While this is certainly useful, one very common practical need is to prefix a length value to the data being sent. However, when you start sending and receiving numeric data via sockets, you have to be aware of conversion issues in terms of what type of machine is on the other end of the connection. Specifically, you need to know how to convert numeric data from the local machine's format (host order) to the industry standard format for sending sockets data (network order).

Note: If the terms host order, network order, little-endian, or big-endian are new to you or you'd like a refresher on these terms, please refer to my article entitled "Sockets Byte-Ordering Primer."

Basically, there are two main helper classes/methods that you'll need to know about when sending data over sockets. The first is the IPAddress.HostToNetworkOrder method. This method takes a value and converts it from the host (local machine's) format to network order (big-endian). Note that I didn't say that it converts little-endian to big-endian because there are times when the host order will be big-endian. It's always a good idea to convert to network order even if both machines use big-endian format because, as with today's abilities to move code from one platform to another, you never know when someone might want to run your code on a different platform than the one you originally designed it for. The second method is the Encoding class, which can be used to convert a String object into a byte array—as required by the Socket.Send method.

Below I present to you a simple SendRequest method that takes a socket object and a string value to be sent and then prefixes the numeric value of the string's length to the data before sending it. The function performs the following steps:

  • The length of the data to be transferred is determined via the String.Length property.
  • Converts the string's length value from host order to network order using the IPAddress. HostToNetworkOrder method.
  • The Socket.Send method only works with byte arrays so the sample converts the length to that data typeusing with the Encoding.ASCII.GetBytes method.
  • The length value is then sent via the Socket.Send method.
  • The string value to send to the remote machine is copied into a byte array via the Encoding.ASCII.GetBytes method. (Note that for a Unicode value, I could have used the Encoding.Unicode.GetBytes method.)
  • The string value is sent to the remote machine via Socket.Send.
void SendRequest(Socket socket, string request)
   // get string length
   int reqLen = request.Length;

   // convert string length value to network order
   int reqLenH2N = IPAddress.HostToNetworkOrder(reqLen);

   // get string length value into a byte array -- for use with
   // Socket.Send
   byte[] reqLenArray = BitConverter.GetBytes(reqLenH2N);

   // send the length value
   socket.Send(reqLenArray, 4, System.Net.Sockets.SocketFlags.None);

   // copy string to a byte array
   byte[] dataArray = Encoding.ASCII.GetBytes(request);

   // send the string array
   socket.Send(dataArray, reqLen,

About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.


  • Send data contact poom wm

    Posted by milfey on 06/19/2008 10:32pm

    Hi. I wanna ask u, how to sending sockets data (send data contact poom in windows mobile)to server (using c++). Please help n Thanks.

  • Should it be titled "Sending BSTR thru WinSocket" ?

    Posted by Zhefu Zhang on 03/25/2004 10:09am

    just kidding... thanks

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds