以文本方式查看主题

-  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=170729)

--  作者:chenjiu6202
--  发布时间:2021/8/5 11:02:00
--  socket连接中断,报错通用数据管理平台停止
socket服务端用于接收多个客户端发送来的气象数据,但是,服务端使用不定期的报错通用数据管理平台停止
造成服务器崩溃,很苦恼,麻烦专家老师给看看

--  作者:chen37280600
--  发布时间:2021/8/5 11:10:00
--  
你自己用的第三方socket工具?
--  作者:chenjiu6202
--  发布时间:2021/8/5 11:10:00
--  
麻烦老师给看看
--  作者:chenjiu6202
--  发布时间:2021/8/5 11:11:00
--  
不是的
--  作者:chenjiu6202
--  发布时间:2021/8/5 11:11:00
--  
怎么上传不上来
--  作者:chenjiu6202
--  发布时间:2021/8/5 11:12:00
--  
\'\'\'\'\'\'\'本方法是一个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

--  作者:chenjiu6202
--  发布时间:2021/8/5 11:15:00
--  
我写了个调试日志,怎么上传不上来,莫名其妙的就自动断开了,
有时候几个月都不带断开的,有时候几个小时就断开了,每次断开就报错通用数据管理平台停止 
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:listen.txt


--  作者:chenjiu6202
--  发布时间:2021/8/5 11:16:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:blueking.foxdb


--  作者:有点蓝
--  发布时间:2021/8/5 14:08:00
--  
首先建议把日志功能改为写数据库,因为这个日志功能不是线程安全的。多线程情况下会有文件写冲突。或者把日志功能包装放到一个方法里,使用try处理

其次这里加上日志看有没有反馈

    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
        Output.Logs("监听日志").Add("接口调用异常--" & ex.Message)
        Output.Logs("监听日志").Save("C:\\Listen.txt",True)
            \'Return ex.message
        End try
    End Sub
End class

--  作者:chenjiu6202
--  发布时间:2021/8/5 15:57:00
--  
好的,我改改看吧