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