비동기 소켓 서버를 처리하는 SocketSvr 클래스가 있습니다. 그것은 내 기본 폼에서 BackgroundWorker를 통해 호출됩니다. 기본적으로 메인 폼의 텍스트 상자에 소켓 데이터 정보를 표시하고 메인 폼에서 서버 시작/중지 버튼을 사용하여 해당 작업을 수행하기를 원합니다. 기본적 startserver는() 또는 중지 stopServer() 함수를 호출하여, 매우 간단 상기VB.Net 백그라운드 작업자에서 실행중인 비동기 소켓 서버를 올바르게 종료합니다.
Public Class Form1
Dim WithEvents Socketsvr As New SocketSvr
Private Sub ToggleServerButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ToggleServerButton.Click
If ToggleServerButton.Text = "Stop Server" Then
ToggleServerButton.Text = "Start Server"
Socketsvr.StopServer()
Else
ToggleServerButton.Text = "Stop Server"
Socketsvr.StartServer()
End If
End Sub
Private Sub UpdateOutput_Event(ByVal sender As Object, _
ByVal text As String) Handles Socketsvr.UpdateOutput
Me.ServerOutputTextbox.AppendText(text + vbCrLf)
End Sub
End Class
: 여기
는 Form1.vb에서의 코드이다. 아래 이벤트는 백그라운드 프로세스에서 호출 된 이벤트를 통해 텍스트 상자를 업데이트하는 데 사용하는 Raised 이벤트입니다.다음은 Socketsvr.vb의 코드 중 일부입니다. 게시물의 출혈을 최소화하기 위해 관련성없는 코드를 제거합니다.
Public Sub StopServer()
bw.CancelAsync()
allDone.Set()
End Sub 'StopServer
Public Sub StartServer()
bw.WorkerSupportsCancellation = True
bw.RunWorkerAsync()
End Sub 'StartServer
Private Sub bw_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](1023) {}
' Establish the local endpoint for the socket.
Dim ipHostInfo As New IPHostEntry
ipHostInfo.AddressList = New IPAddress() _
{New IPAddress(New [Byte]() {127, 0, 0, 1})}
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 8888)
' Create a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Bind the socket to the local endpoint and listen for incoming connections.
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1)
listener.Bind(localEndPoint)
listener.Listen(100)
While True
' If cancellation is pending, shut down server
If bw.CancellationPending Then
Out("Server stopped at " + DateTime.Now.ToString())
Exit While
End If
Out("Server started at " + DateTime.Now.ToString())
' Set the event to nonsignaled state.
allDone.Reset()
Try
' Start an asynchronous socket to listen for connections.
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), listener)
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
' Wait until a connection is made and processed before continuing.
allDone.WaitOne()
End While
End Sub
StopServer() 기능을 실행할 때 클라이언트의 요청을 올바르게 종료하는 방법을 잘 모릅니다. 위의 코드에서 백그라운드 프로세스의 취소 대기열을 배치합니다. 재미있는 점은 일단 StopServer()가 실행되면 더 많은 연결을 수락 한 다음 그 후에 수락을 중지한다는 것입니다.
나는 위의 코드에서 다음 줄을 제거하는 경우 :
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1)
- 나는 내 생각은 (나는 분명히 소켓을 다시 사용할 수 없습니다 불평) 서버를 두 번 시작하려고 할 때 충돌 배경 노동자에게 bw.CancellationPending 콜에 뭔가를 추가해야합니까?
더 자세한 정보가 필요하면 통찰력을 주시기 바랍니다.
감사합니다 - 아직도 내가 VB.Net 매우 미숙 한 생각으로 주위에 내 머리를 정리하려고은 - 곧 다시 연락하고 그것을 모두 밖으로 어떻게 작동하는지 볼 수 있습니다. 잠시 동안 +1을하고 일단 내가 일하면 답을 표시 할 것입니다. –
업데이트 : 이것은 매력처럼 일했습니다. 정말 고마워요! –
나는 또한 비동기 서버 샘플을 기반으로 소켓 서버에 대해 wpf UI와 동일한 작업을 수행하고 싶습니다. 나는 그것을 누군가가 C#에서 수정 된 코드를 제공 할 수 주셔서 감사하겠습니다. –