Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I read a lot of article but none give me a clear picture.

My company required me to write a program to send a 4 byte header message to a specific IP and Port... Message will have a 4 byte header followed by the message itself... can anybody give me some idea or best to have a sample and simple program for me to start with...

Thank you in advance for those who will give their time. :-)
Posted

Use a TcpClient to connect to the remote end point, get a NetworkStream and send the four bytes and your message to that. Something like:

TcpClient client = new TcpClient("<hostname>", <port>);
NetworkStream ns = client.GetStream();
ns.WriteByte(<byte1>);
ns.WriteByte(<byte2>);
ns.WriteByte(<byte3>);
ns.WriteByte(<byte4>);
// then the rest of your message

ns.Flush();
 
Share this answer
 
v3
Comments
BobJanova 23-Aug-11 12:15pm    
You can also use Write to write a 4 byte array, which is probably faster.
Prepend the 4 byte message to the start of whatever it is you are sending.

You might want something to sniff the traffic, as well as a pattern/sample message, so that you can verify what you're sending is what they expect.

Here's an article[^] with a simple send() implementation.

Cheers.
 
Share this answer
 
If its some kind of length or something, then use int type and use GetBytes of BitConverter class to produce a 4 byte array.

If you use short data type, then you'll get 2 bytes array after calling GetBytes().

http://msdn.microsoft.com/en-us/library/system.bitconverter.getbytes.aspx[^]
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=VS.80).aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900