'''''''本方法是一个socket的服务端
'''''''其调用方法为 sockets = New SocketServer(ip,port)
'''''''传入ip地址和端口号
Public class SocketServer
Shared socket As New System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)
Private Shared result As Byte() = New Byte(72) {}
Public Sub New(ByVal ip As String, ByVal p As Integer)
Dim host As String = ip ''IP地址
Dim port As Integer = p ''端口
Output.Logs("监听日志").Add("ip地址" & host & "--端口" & port)
Output.Logs("监听日志").Save("C:\Listen.txt",True)
socket.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Parse(host), port))
socket.Listen(90) ''设定最多100个排队连接请求
Dim myThread As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf ListenClientConnect)) ''通过多线程监听客户端连接
myThread.Start()
'Output.Show("已启动监听客户端")
Output.Logs("监听日志").Add("已开启一个线程成功--ip地址" & host & "--端口" & port)
Output.Logs("监听日志").Save("C:\Listen.txt",True)
End Sub
'
'''' <summary>
'''' 监听客户端连接
'''' </summary>
Private Shared Sub ListenClientConnect()
'Output.Show("监听到了客户端")
Output.Logs("监听日志").Add("客户端已经连接")
Output.Logs("监听日志").Save("C:\Listen.txt",True)
While True
Dim clientSocket As System.Net.Sockets.Socket = socket.Accept()
'clientSocket.Send(Encoding.UTF8.GetBytes("我是服务器"))
Dim receiveThread As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf ReceiveMessage))
receiveThread.Start(clientSocket)
End While
End Sub
'''' <summary>
'''' 接收消息
'''' </summary>
'''' <param name="clientSocket"></param>
Private Shared Sub ReceiveMessage(clientSocket As Object)
Dim myClientSocket As System.Net.Sockets.Socket = CType(clientSocket, System.Net.Sockets.Socket)
Dim msg As String
While True
Try
''通过clientSocket接收数据
Dim receiveNumber As Integer = myClientSocket.Receive(result)
If receiveNumber = 0 Then
Output.Logs("监听日志").Add("接收数据result为空值,不做处理返回")
Output.Logs("监听日志").Save("C:\Listen.txt",True)
Return
End If
'''将16进制字节流转换为16进制字符串
msg = BytesTohexString(result)
Output.Logs("监听日志").Add(Date.Now & "--获取到转换后的字符串--" & msg)
Output.Logs("监听日志").Save("C:\Listen.txt",True)
'msg = "接收客户端{0} 的消息:{1} , " & myClientSocket.RemoteEndPoint.ToString() & " , " & Encoding.Unicode.GetString(result, 0, receiveNumber)
''''------------------------转发字符串
Dim dataInfo As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf SendString))
dataInfo.Start(msg)
Output.Logs("监听日志").Add("向120服务器转发字符串")
Output.Logs("监听日志").Save("C:\Listen.txt",True)
''给Client端返回信息
'Dim sendStr As String = "已成功接到您发送的消息" //向客户端发送信息
' Dim bs As Byte() = Encoding.UTF8.GetBytes(sendStr) //向客户端发送信息
''Encoding.UTF8.GetBytes()不然中文会乱码
''返回信息给客户端
'’ myClientSocket.Close() '' //发送完数据关闭Socket并释放资源//长连接的话就不关闭
'' Output.Show("已经到了长连接")
'' myClientSocket.Send(bs, bs.Length, 0) ///向客户端发送信息
Catch ex As Exception
'' Console.WriteLine(ex.Message)
Output.Logs("监听日志").Add("监听异常--" & ex.Message)
Output.Logs("监听日志").Save("C:\Listen.txt",True)
myClientSocket.Close()
''关闭Socket并释放资源
myClientSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both)
''禁止发送和上传
Exit Try
End Try
End While
End Sub
'''''-------------------------------启用另外一个线程向另外一个服务器转发消息
Private Shared Sub SendString(ByVal str As String)
Dim hc As New HttpClient("http://10.131.50.120/weatherData.htm")
hc.C
Dim jo As new JObject
jo("data") = str
Dim ret As String
try
hc.Content = jo.ToString
ret = hc.GetData
catch ex As exception
'Return ex.message
End try
End Sub
End class
''''''-------------------------------------------------------将16进制字节流转换为16进制字符串
Public Function BytesTohexString(bytes As Byte()) As String
If bytes Is Nothing OrElse bytes.Count() < 1 Then
Return String.Empty
End If
Dim count = bytes.Count()
Dim cache = New StringBuilder()
'cache.Append("0x")
Dim ii As Integer = 0
While ii < count
Dim tempHex = Convert.ToString(bytes(ii), 16).ToUpper()
cache.Append(If(tempHex.Length = 1, "0" + tempHex, tempHex))
System.Threading.Interlocked.Increment(ii)
End While
Return cache.ToString()
End Function