以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [求助]关于socket ,下面代码如何用按钮发送字符串和接收字符串 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=100826) |
||||||||
-- 作者:111yangguojun -- 发布时间:2017/5/19 10:42:00 -- [求助]关于socket ,下面代码如何用按钮发送字符串和接收字符串 Public socket As MyClient Public Class MyClient Private mainfrm As Object \'客户端项目 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, ByVal frm As Object) mainfrm = frm 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 [求助]下面代码目前写在全局代码管理器中。 1. 关于socket ,下面代码如何用按钮发送字符串和接收字符串。 2. 这段代码能不能写入内部函数,该如何写? If tcp Is Nothing Then MsgBox("无法连接服务器,请重试") Else Dim netStream As System.Net.Sockets.NetworkStream = tcp.GetStream() Dim loginInfo As String = System.Guid.NewGuid.ToString() & "|" & System.Net.Dns.GetHostName() \'唯一标识 和 机器码 \'Dim stream() As Byte = Encoding.Unicode.GetBytes(loginInfo) Dim stream() As Byte = New Byte() {&H01, &H03, &H00, &H00, &H00, &H01,&H84,&H0A} netStream.Write(stream, 0, stream.Length) \'往服务器发送登录信息 Dim buffer As Byte() = New Byte(50) {} netStream.Read(buffer, 0, buffer.Length) \'得到回发的信息 Dim ss As String = BitConverter.ToString(buffer) MsgBox(buffer.Length & " 数组长度 " & ss) \'Dim connResult As String = Encoding.Unicode.GetString(buffer).Substring(0, 2) \'If connResult.Equals("OK") Then \'登录成功 \'nStream = netStream \'receiveThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf ReceiveMsg)) \'receiveThread.IsBackground=True \'receiveThread.Start() \'Else \'MsgBox("登录失败……") \'Return \'End If End If End Sub End Class
|
||||||||
-- 作者:111yangguojun -- 发布时间:2017/5/19 11:18:00 -- 求助 |
||||||||
-- 作者:有点色 -- 发布时间:2017/5/19 12:54:00 -- mark tcp客户端
此主题相关图片如下:test.gif |
||||||||
-- 作者:有点色 -- 发布时间:2017/5/19 12:55:00 -- tcp的例子,参考
|
||||||||
-- 作者:111yangguojun -- 发布时间:2017/5/19 13:45:00 -- 可以发送了 谢谢! |
||||||||
-- 作者:zto001 -- 发布时间:2019/10/14 8:52:00 -- 111yangguojun你这个是怎么做的?可否教教我?
|
||||||||
-- 作者:aidimeng -- 发布时间:2020/12/12 13:22:00 -- 收藏 |