发送4bytes头消息到tcpip [英] Sending 4bytes header message to tcpip

查看:70
本文介绍了发送4bytes头消息到tcpip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多文章,但没有一个给我清晰的画面.

我的公司要求我编写一个程序,以将4字节的标头消息发送到特定的IP和端口...消息将具有4字节的标头,然后是消息本身...任何人都可以给我一些想法或最好具有一个示例和简单程序让我从...开始.

在此先感谢您抽出宝贵的时间. :-)

解决方案

使用TcpClient连接到远程端点,获取NetworkStream并将这四个字节和您的消息发送到该端点.像这样的东西:

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();


将4字节消息附加到要发送的内容的开头.

您可能希望通过某种方式来监听流量以及模式/示例消息,以便可以验证发送的内容是否符合他们的期望.

这是文章 [ 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 [ 解决方案

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();


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.


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[^]


这篇关于发送4bytes头消息到tcpip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆