我想通过TCP IP在Integer中发送一个数字。我将Send_data
中的数据另存为Integer
号码。
我无法将其更改为string
或char[]
以便发送。{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();
答案 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);