首页
论坛
新职位
搜索论坛
什么's new
新职位
新的个人资料帖子
最新活动
会员
目前的访客
新的个人资料帖子
搜索个人资料帖子
VB.NET社区
登录
寄存器
什么's new
搜索
搜索
仅搜索标题
通过:
新职位
搜索论坛
Menu
Log in
Register
安装应用
安装
首页
论坛
C#
Windows表格
当第二次发出请求时,TcpClient.GetStream()。Read()读取错误的字节。
您正在使用过期的浏览器。它可能无法正确显示此网站或其他网站。
您应该升级或使用
替代浏览器
.
回复主题
信息
<blockquote data-quote="Veeran" data-source="post: 10145" data-attributes="member: 11603"><p>In my windows application, where communication between client and server made through TCP/IP using NetworkStream. when client sends message(byte array of command) the response is received correctly. But for the second time when request made, the order of bytes in the response has changed. Wrong data has received from server.Here is the code.</p><p></p><p>Thread to handle data reception in page load:</p><p>[CODE=csharp]ThreadDatareceive = new Thread(new ParameterizedThreadStart(udfHandleClientCommunication)); ThreadDatareceive.Start(Class.myclient);</p><p>ThreadDatareceive.Start(Class.myclient);[/CODE]</p><p></p><p>Write to stream:</p><p>[CODE=csharp]bal.byteCommand = new byte[] { 0x90, 0x20, 0x02, 0x01, 0x00, 0x00, 0x01, 0xB0 };</p><p>if (Class.myclient.Connected)</p><p>{</p><p> stream = Class.myclient.GetStream();</p><p> stream.Write(bal.byteCommand, 0, bal.byteCommand.Length);</p><p> bal.EventNum = 1;</p><p> bal.timer.Start();</p><p> bal.byteResponse.Clear();</p><p>}</p><p>//Scan();</p><p>else</p><p> MessageBox.Show("Please Configure the Module");[/CODE]</p><p></p><p></p><p>Read from stream:</p><p></p><p>[CODE=csharp]private void udfHandleClientCommunication(object client)</p><p>{</p><p> if (this.InvokeRequired)</p><p> {</p><p> MyDelegate1 mydelegate2 = new MyDelegate1(udfHandleClientCommunication);</p><p> this.Invoke(mydelegate2, new object[]{client});</p><p> }</p><p> else</p><p> {</p><p> try</p><p> {</p><p> //List byteData = new List();</p><p> while (true)</p><p> {</p><p> tcpClient = (TcpClient)client;</p><p> stream2 = tcpClient.GetStream();</p><p> byte[] byteBuffer = new byte[1];</p><p> //Array.Clear(byteBuffer, 0, byteBuffer.Length);</p><p> stream2.Read(byteBuffer, 0, byteBuffer.Length);</p><p> bal.byteResponse.AddRange(byteBuffer);</p><p></p><p> try</p><p> {</p><p> switch (bal.EventNum)</p><p> {</p><p> case 1:</p><p> // The scan response is of 97 bytes. So,</p><p> // wait for the full response</p><p> // before processing the response data.</p><p> if (bal.byteResponse.Count >= 97)</p><p> {</p><p> if(bal.byteResponse[2] == 0x42)</p><p> {</p><p> udfReadStatusResponse();</p><p> break; </p><p> } </p><p> }</p><p></p><p> case 2:</p><p> // The scan setup is of 11 bytes. So,</p><p> // wait for the full response</p><p> // before processing the response data.</p><p> if (bal.byteResponse.Count >= 11)</p><p> {</p><p> if(bal.byteResponse[2] == 0x43)</p><p> {</p><p> udfReadSetupResponse();</p><p> break; </p><p> } </p><p> }</p><p></p><p> }</p><p> }</p><p> catch</p><p> {</p><p> bal.DataReceivedFlag = false;</p><p> bal.TimeouFlag = true;</p><p> }</p><p> }</p><p> }</p><p> catch (Exception ex)</p><p> {</p><p> MessageBox.Show(ex.Message);</p><p> }[/CODE]</p><p></p><p>When I run application (Status win form) receives data correctly.But when I click status again, the data read from stream.read() is incorrect.</p><p></p><p>I am closing the client connection in the form close(). May be reading one byte from stream every time is the problem or closing connection properly.</p></blockquote><p></p>
Insert quotes…
验证
发表回复
首页
论坛
C#
Windows表格
当第二次发出请求时,TcpClient.GetStream()。Read()读取错误的字节。
本网站使用Cookie来帮助个性化内容,调整您的体验并在注册时保持登录状态。
继续使用本网站,即表示您同意我们使用cookie。
接受
了解更多…
最佳
底部