以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 多个tcp客户端怎么连接? (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=185494) |
||||
-- 作者:YDS -- 发布时间:2023/2/25 18:28:00 -- 多个tcp客户端怎么连接? Public socket As MyClient Public Class MyClient Public receiveThread As System.Threading.Thread Public nStream As System.Net.Sockets.NetworkStream Public tcp As System.Net.Sockets.TcpClient Public Sub New(ByVal ip As String, ByVal port As Integer) Dim server As System.Net.IPAddress = System.Net.IPAddress.Parse(ip) tcp = New System.Net.Sockets.TcpClient() Try tcp.Connect(ip, port) Catch generatedExceptionName As Exception tcp = Nothing End Try If tcp Is Nothing Then MsgBox("无法连接服务器,请重试") Else Dim netStream As System.Net.Sockets.NetworkStream = tcp.GetStream() nStream = netStream receiveThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf ReceiveMsg)) receiveThread.IsBackground=True receiveThread.Start() msgbox("登录成功") End If End Sub Private Sub ReceiveMsg() While True Dim packet As Byte() = New Byte(1024) {} nStream.Read(packet, 0, packet.Length) Dim str As String = Encoding.Default.GetString(packet) Dim txt = forms("窗口1").Controls("TextBox2") txt.Text &= iif(txt.text > "", vbcrlf, "") & str End While End Sub End Class 以上是全局代码,单个连接没有问题,但是我现在要同时开四个连接,需要怎么写代码?
|
||||
-- 作者:有点蓝 -- 发布时间:2023/2/26 19:40:00 -- new 4个对象就行了 全局代码 Public socket1,socket2,socket3,socket4 As MyClient 按钮 socket1=new MyClient(ip,port) socket2=new MyClient(ip,port)……
|
||||
-- 作者:YDS -- 发布时间:2023/2/27 11:17:00 -- 那返回的数据呢? Private Sub ReceiveMsg() While True Dim packet As Byte() = New Byte(1024) {} nStream.Read(packet, 0, packet.Length) Dim str As String = Encoding.Default.GetString(packet) Dim txt = forms("窗口1").Controls("TextBox2") txt.Text &= iif(txt.text > "", vbcrlf, "") & str End While End Sub 这里怎么反馈给不同的框?
|
||||
-- 作者:有点蓝 -- 发布时间:2023/2/27 11:36:00 -- 先去看看相关的帮助文档:https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.tcplistener?view=netframework-4.0 服务端接收的时候获取到的客户端都是对应每个独立的客户端,直接使用这个客户端的NetworkStream 写回数据即可 Dim client As TcpClient = server.AcceptTcpClient()
|
||||
-- 作者:YDS -- 发布时间:2023/2/27 12:03:00 -- 我想知道收到回写数据以后怎么区分? Private Sub ReceiveMsg() While True Dim packet As Byte() = New Byte(1024) {} nStream.Read(packet, 0, packet.Length) Dim str As String = Encoding.Default.GetString(packet) Dim txt = forms("窗口1").Controls("TextBox2") txt.Text &= iif(txt.text > "", vbcrlf, "") & str End While End Sub 这里怎么知道是那个socket返回的数据
|
||||
-- 作者:有点蓝 -- 发布时间:2023/2/27 12:05:00 -- 接收的每个NetworkStream 都是独立的,不需要区分 |
||||
-- 作者:YDS -- 发布时间:2023/2/27 12:41:00 -- Private Sub ReceiveMsg() While True Dim packet As Byte() = New Byte(1024) {} nStream.Read(packet, 0, packet.Length) Dim str As String = Encoding.Default.GetString(packet) Dim txt = forms("窗口1").Controls("TextBox2") txt.Text &= iif(txt.text > "", vbcrlf, "") & str End While End Sub 那上面的代码怎么写?四个socket都登陆成功后,上面的代码接收到的反馈给那个TextBox?
|
||||
-- 作者:YDS -- 发布时间:2023/2/27 12:43:00 --
能否帮我把第二个连接的的代码修改一下,剩下两个我自己补充。
|
||||
-- 作者:有点蓝 -- 发布时间:2023/2/27 13:39:00 --
|
||||
-- 作者:YDS -- 发布时间:2023/2/27 17:45:00 -- Private Sub ReceiveMsg() While True Dim packet As Byte() = New Byte(1024) {} nStream.Read(packet, 0, packet.Length) Dim str As String = Encoding.Default.GetString(packet) Functions.Execute("TCP", nstr, str) End While End Sub 你好,这里我不想接收的数据都是1024,有什么办法能让我是接收到多少个字节就输出多少个字节吗? [此贴子已经被作者于2023/2/27 18:42:57编辑过]
|