奉送500分!急等解决!我在使用System.Net.Sockets类中TcpClient.GetStream时,发现Write语句后,必须关闭流,接收端Readbyte才能正常接收

Tiger0305 2004-01-05 10:07:20
在使用System.Net.Sockets类时,我希望传输大数据流(超过8192字节),并且在发送后并不断开流,而继续接收对方的反馈信息.

我应该如何做呢?

最好有例程,我的邮箱Tiger0305@vip.163.com
...全文
35 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ganenpingsohucom 2004-01-05
  • 打赏
  • 举报
回复
调用 Write 方法将数据发送到远程主机。应调用 Read 方法来接收从远程主机传来的数据。这两种方法都将阻塞,直到执行了指定的操作为止。通过检查 DataAvailable 属性可避免对读取操作的阻塞。true 值表示数据已从远程主机到达,可以进行读取。这样便保证立即完成 Read

注意 在完成数据的发送和接收之后,您无须关闭 NetworkStream。关闭 TcpClient 会将 NetworkStream 释放给垃圾回收器。

Dim tcpClient As New TcpClient()
' Uses the GetStream public method to return the NetworkStream.
Try
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
networkStream.Write(sendBytes, 0, sendBytes.Length)
Else
Console.WriteLine("You cannot write data to this stream.")
tcpClient.Close()
Return
End If
If networkStream.CanRead Then

' Reads the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
' Read can return anything from 0 to numBytesToRead.
' This method blocks until at least one byte is read.
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

' Returns the data received from the host to the console.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("This is what the host returned to you: " + returndata))
Else
Console.WriteLine("You cannot read data from this stream.")
tcpClient.Close()
Return
End If
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
Tiger0305 2004-01-05
  • 打赏
  • 举报
回复
我的5个分帖,每个100分
http://expert.csdn.net/Expert/topic/2633/2633395.xml?temp=.9001581
http://expert.csdn.net/Expert/topic/2633/2633393.xml?temp=7.276553E-02
http://expert.csdn.net/Expert/topic/2633/2633389.xml?temp=.7606317
http://expert.csdn.net/Expert/topic/2633/2633423.xml?temp=.2109491
http://expert.csdn.net/Expert/topic/2633/2633428.xml?temp=.8792993
斗斗来三 2004-01-05
  • 打赏
  • 举报
回复
MARK一下,这几天在学,等等做了再告诉你

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧