日期:2014-05-18 浏览次数:21029 次
NetWorkStream ns=new NetWorkStream(tcpclient.GetStream());
Byte[] WriteBytes = new Byte[1024]; TcpClient Client = new TcpClient(...); NetworkStream Stream = Client.GetStream(); WriteBytes[0] = 65; WriteBytes[1] = 48; WriteBytes[2] = 54; WriteBytes[3] = 53; WriteBytes[4] = 48; WriteBytes[5] = 48; WriteBytes[6] = 48; WriteBytes[7] = 51; Stream.Write(WriteBytes, 0, 8); ..... WriteBytes = new Byte[1024]; WriteBytes[0] = 65; WriteBytes[1] = 48; WriteBytes[2] = 54; WriteBytes[3] = 53; WriteBytes[4] = 48; WriteBytes[5] = 48; WriteBytes[6] = 48; WriteBytes[7] = 51; Stream.Write(WriteBytes, 0, 8);
------解决方案--------------------
上面贴的有点小问题,改下
Byte[] WriteBytes = new Byte[1024]; TcpClient Client = new TcpClient(...); NetworkStream Stream = Client.GetStream(); WriteBytes[0] = 65; WriteBytes[1] = 48; WriteBytes[2] = 54; WriteBytes[3] = 53; WriteBytes[4] = 48; WriteBytes[5] = 48; WriteBytes[6] = 48; WriteBytes[7] = 51; Stream.Write(WriteBytes, 0, 1024); ..... WriteBytes = new Byte[1024]; WriteBytes[0] = 12; WriteBytes[1] = 33; WriteBytes[2] = 54; Stream.Write(WriteBytes, 0, 1024);
------解决方案--------------------
public static byte[] Read(NetworkStream netstream)
{
byte[] buffer = new byte[1024];
byte[] rebuffer;
try
{
int i = netstream.Read(buffer, 0, buffer.Length);
rebuffer = new byte[i];
Buffer.BlockCopy(buffer, 0, rebuffer, 0, i);
return rebuffer;
}
catch
{
return null;
}
}
这个方法可以解决你的问题!
------解决方案--------------------
返回值为实际需要读取的数据,也就是说去掉了那些没用的数据!
------解决方案--------------------
解决了没?
------解决方案--------------------
NetworkStream stream = client.GetStream();
应该不是NetworkStream 的问题。
问题应该在Write 方法发送的byte[] 或接收的byte[] 中。
------解决方案--------------------