标题:为什么我的TcpClient只能发送一次
取消只看楼主
fily1314
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:166
专家分:1190
注 册:2007-7-18
结帖率:75%
 问题点数:0 回复次数:2 
为什么我的TcpClient只能发送一次

客户端代码如下:

Private th As Threading.Thread
Private tcpc As System.Net.Sockets.TcpClient

Private Sub ConnectBt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectBt.Click
Try
tcpc = New System.Net.Sockets.TcpClient(TcpAddrTextBox.Text, CInt(TcpPortTextBox.Text))
th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
th.Start()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub

Private Sub SendBt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendBt.Click
Try
Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
Dim DataStr As String = MsgSendTextBox.Text

Dim reqStream As New IO.StreamWriter(tcpStream)
reqStream = New IO.StreamWriter(tcpStream)
reqStream.Write(DataStr)
reqStream.Flush()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub

Private Sub MyListen()
Try
Dim ReadStream As IO.StreamReader
Dim Count As Integer
Dim i As Integer
Dim RecvStr As String
Dim tcpStream As Net.Sockets.NetworkStream
While True
tcpStream = tcpc.GetStream
ReadStream = New IO.StreamReader(tcpStream)
Dim Buf(1023) As Char
Count = ReadStream.Read(Buf, 0, 1024)
If Count > 0 Then
i = 0
RecvStr = ""
While i < Count
RecvStr += Buf(i)
i += 1
End While
MsgRecvRichTextBox.AppendText(RecvStr & Chr(10) & Chr(13))
End If
End While
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub




服务器端代码如下:

Private th As Threading.Thread
Private tcpl As System.Net.Sockets.TcpListener
Private sSocket As System.Net.Sockets.Socket

Private Sub StartBt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBt.Click
th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
th.Start()
End Sub

Private Sub MyListen()
Try
Dim ipAddress As System.Net.IPAddress = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0)
tcpl = New System.Net.Sockets.TcpListener(ipAddress, CInt(TcpPortTextBox.Text))

tcpl.Start()
Dim RecvStr As String
Dim Count As Integer
Dim i As Integer
While True
RecvStr = ""
sSocket = tcpl.AcceptSocket()
Dim MyBuffer(1024) As Byte

Count = sSocket.Receive(MyBuffer)

If Count > 0 Then
RecvStr = ""
For i = 0 To Count - 1
RecvStr += Chr(MyBuffer(i))
Next
Dim Array() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(RecvStr)
sSocket.Send(Array, Array.Length, Net.Sockets.SocketFlags.None)
End If

End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub


代码实现的功能很简单,客户端向服务器发送一段字符串,服务器收到后再发回来,但是不知道为什么客户端连接上以后,只能发送和接收一次,再次发送就没反应了,这过程tcpc并没有关闭连接啊,兄弟们帮忙看看是服务器的问题还是客户端的问题。

搜索更多相关主题的帖子: TcpClient Threading System tcpc New 
2007-07-18 15:04
fily1314
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:166
专家分:1190
注 册:2007-7-18
得分:0 
555555...
发了这么多天咋都米人理呢
2007-07-21 11:55
fily1314
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:166
专家分:1190
注 册:2007-7-18
得分:0 

谢谢版主大大
现在可以了
2007-07-23 14:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-156301-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.166510 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved