Habs wohl doch noch nicht ganz.
Ich poste gerade noc mal alles
Die Problemstellen sind fett markiert.
Server Teil1
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Call AsynchronousSocketListener.Main()
End Sub
End Class
'-------------------------------
Public Class StateObject
' Client socket.
Public workSocket As Socket = Nothing
' Size of receive buffer.
Public Const BufferSize As Integer = 1024
' Receive buffer.
Public buffer(BufferSize) As Byte
' Received data string.
Public sb As New StringBuilder
End Class
'-------------------------------
Public Class AsynchronousSocketListener
' Thread signal.
Public Shared allDone As New ManualResetEvent(False)
' This server waits for a connection and then uses asychronous operations
' to
' accept the connection, get data from the connected client,
' echo that data back to the connected client.
' It then disconnects from the client and waits for another client.
Public Shared Sub Main()
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](1023) {}
' Establish the local endpoint for the socket.
Dim localEndPoint As New IPEndPoint(Net.IPAddress.Any, 3000)
' Create a TCP/IP socket.
'Dim listener As New Socket(AddressFamily.InterNetwork,
' SocketType.Stream, ProtocolType.Tcp)
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
' Bind the socket to the local endpoint and listen for incoming
' connections.
listener.Bind(localEndPoint)
listener.Listen(1000)
While True
' Set the event to nonsignaled state.
allDone.Reset()
' Start an asynchronous socket to listen for connections.
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), _
listener)
' Wait until a connection is made and processed before continuing.
allDone.WaitOne()
End While
End Sub
Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
' Get the socket that handles the client request.
Dim listener As Socket = CType(ar.AsyncState, Socket)
' End the operation.
Dim handler As Socket = listener.EndAccept(ar)
' Create the state object for the async receive.
Dim state As New StateObject
state.workSocket = handler
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New _
AsyncCallback(AddressOf ReadCallback), state)
End Sub
Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
Dim content As String = String.Empty
' Retrieve the state object and the handler socket
' from the asynchronous state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim handler As Socket = state.workSocket
' Read data from the client socket.
Dim bytesRead As Integer = handler.EndReceive(ar)
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _
bytesRead))
content = state.sb.ToString()
Send(handler)
End If
End Sub
... |