SOCKET通讯,第一次可以和服务器进行正常连接和数据交互,但是当关闭客户端,再次连接服务器时一直连接不上。大侠们救救我啊!!!
服务器端开发语言:VB.NET
客户端卡发语言:C语言[嵌入式]
当我第一启动服务器和客户端的时候,客户端发过来的数据可以正常的取得,并且可以把结果返回给客户端,但是当我把客户端关闭,再一次去连接服务器的时候,却怎么也连接不上,为什么呢。
但是当我把服务器和客户端同时关闭再开启的时候,就又可以进行正常通讯了。
服务器端代码如下:
Public Class Remote_Check
' 变数定义
Public Shared allDone As ManualResetEvent
Private Shared listenerTcp As Socket
Private th As Threading.Thread
Private th1 As Threading.Thread
'
'函数名: Initialize
'
'函数说明: 画面初期设定
'
'参数: 无
'
'返回值: 无
'
'备考:
'
Private Sub Initialize()
End Sub
'
'函数名: Start
'
'函数说明: 部品检证系统启动
'
'参数: 无
'
'返回值: 无
'
'备考:
'
Public Sub Start()
Try
' SOCKET通信开始
th = New Thread(New ThreadStart(AddressOf Main))
th.Start()
' MAIL送信
th1 = New Thread(New ThreadStart(AddressOf SendMail))
th1.Start()
Catch ex As Exception
Me.Close()
' 错误信息出力,警告错误表示
PageControlUtil.ShowErrorMsgBox("WREC998", ex.Message, MsgBoxStyle.OKOnly, "WREC996", Define.LogType.ErrorLog)
End Try
End Sub
'
'函数名: Close
'
'函数说明: 部品检证系统停止
'
'参数: 无
'
'返回值: 无
'
'备考:
'
Public Sub Close()
Try
' 线程停止
If th.IsAlive Then
th.Abort()
End If
If th1.IsAlive Then
th1.Abort()
End If
' SOCKET通信结束
If Not listenerTcp Is Nothing Then
'listenerTcp.Shutdown(SocketShutdown.Both)
listenerTcp.Close()
End If
If Not allDone Is Nothing Then
allDone.Close()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
'
'函数名: Main
'
'函数说明: SOCKET连接建立
'
'参数: 无
'
'返回值: 无
'
'备考: 无
'
Public Shared Sub Main()
Try
allDone = New ManualResetEvent(False)
' 读入数据流
Dim bytes() As Byte = New [Byte](1023) {}
' 本地信息取得
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, CInt(FunctionUtil.getConfig("BarCodePort")))
' TCP/IP协议创建
listenerTcp = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' SOCKET绑定
listenerTcp.Bind(localEndPoint)
listenerTcp.Listen(100)
While True
Application.DoEvents()
allDone.Reset()
' 数据接受开始
listenerTcp.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), listenerTcp)
' 数据出力完成前,其他的线程处于等待状态
allDone.WaitOne()
End While
Catch ex As Exception
Throw ex
End Try
End Sub
'
'函数名: SendMail
'
'函数说明: MAIL送信
'
'参数: 无
'
'返回值: 无
'
'备考: 无
'
Public Sub SendMail()
Try
While True
GetAndCheckInformation.SendMail()
th1.Sleep(10000)
End While
Catch ex As Exception
Throw ex
End Try
End Sub
'
'函数名: AcceptCallback
'
'函数说明: 数据接收,初始化
'
'参数: ARG1 - ar
'
'返回值: 无
'
'备考: 无
'
Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
Try
Dim listener As Socket = CType(ar.AsyncState, Socket)
Dim handler As Socket = listener.EndAccept(ar)
Dim state As New StateObject
' 初始化
state.workSocket = handler
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
Catch ex As Exception
Throw ex
End Try
End Sub
'
'函数名: ReadCallback
'
'函数说明: 数据接收
'
'参数: ARG1 - ar
'
'返回值: 无
'
'备考: 无
'
Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim handler As Socket = state.workSocket
Try
Dim content As String = String.Empty
Dim bytesRead As Integer = handler.EndReceive(ar)
Dim strSend As String = String.Empty
If bytesRead > 0 Then
' 数据取得
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
content = state.sb.ToString()
' 数据结束判定
If content.IndexOf("<EOF>") > -1 Then
' 读入信息用户的场合
If content.StartsWith("U") Then
strSend = GetAndCheckInformation.checkUserInformation(content)
End If
' 读入信息机种的场合
If content.StartsWith("Z") Then
strSend = GetAndCheckInformation.checkModelInformation(content)
End If
' 部品检证的场合
If content.StartsWith("P") Then
strSend = GetAndCheckInformation.checkModelAndFenceInformation(content)
End If
' 读入信息管理员密码输入时
If content.StartsWith("W") Then
strSend = GetAndCheckInformation.checkPassWordInformation(content)
End If
' 数据回显到客户端
Send(handler, strSend)
Else
' 数据取得
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
End If
End If
Catch ex As Exception
' 数据回显到客户端
Send(handler, "Error" + vbCr)
Throw ex
End Try
End Sub
'
'函数名: Send
'
'函数说明: 数据客户端返回
'
'参数: ARG1 - handler
' ARG2 - data
'
'返回值: 无
'
'备考: 无
'
Private Shared Sub Send(ByVal handler As Socket, ByVal data As String)
Try
Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
' 数据返回
handler.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), handler)
Catch ex As Exception
Throw ex
End Try
End Sub
'
'函数名: SendCallback
'
'函数说明: 数据客户端返回
'
'参数: ARG1 - ar
'
'返回值: 无
'
'备考: 无
'
Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
Try
Dim handler As Socket = CType(ar.AsyncState, Socket)
' 数据输送到客户端
Thread.Sleep(300)
Dim bytesSent As Integer = handler.EndSend(ar)
handler.Shutdown(SocketShutdown.Both)
handler.Close()
allDone.Set()
Catch ex As Exception
Throw ex
End Try
End Sub
End Class