通过C#中的TCP IP发送一个号码

时间:2015-05-13 15:40:42

标签: tcp ip

我想通过TCP IP在Integer中发送一个数字。我将Send_data中的数据另存为Integer号码。

我无法将其更改为stringchar[]以便发送。{p>我运行此代码时只在Client中收到一个空格。

这是我的代码:

NetworkStream clientStream = MyClient.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = BitConverter.GetBytes(Send_data[0]);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();

1 个答案:

答案 0 :(得分:0)

tcp客户端声明和连接:

IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("IP_address"), port);
TcpClient client = new TcpClient();            
client.Connect(endPoint);

NetworkStream声明:

NetworkStream networkStream = client.GetStream();

BinaryFormatter for serilazation

BinaryFormatter formatter = new BinaryFormatter();

                                    //int goes here
formatter.Serialize(networkStream , Send_data);