Hallo,
Ich habe hier einen TCP/IP Server, dem ich mittels Post einen Stream sende.
Der Server Funktioniert soweit, jedoch bekomme ich beim Client eine Fehlermeldung :
WebException wurde nicht behandelt
Der Server hat eine Protokollverletzung ausgeführt.. Section=ResponseStatusLine
Die Fehlermeldung erscheint nachdem der Client die Rückmeldung des Servers erhalten soll.
Ich poste mal den Source.
PS: System.Web muss referenziert werden.
Der Server :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)
' Bind the socket to the local endpoint and listen for incoming
' connections.
listener.Bind(localEndPoint)
listener.Listen(100)
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
Private Shared Sub Send(ByVal handler As Socket) ', ByVal data As String)
Dim strSenden As String
strSenden = "Testing=1&CUPPath=" & System.Web.HttpUtility.UrlEncode( _
"C:\CUP")
Dim b() As Byte
b = System.Text.Encoding.Default.GetBytes(strSenden)
' Begin sending the data to the remote device.
handler.BeginSend(b, 0, b.Length, SocketFlags.None, New AsyncCallback( _
AddressOf SendCallback), handler)
End Sub
Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
' Retrieve the socket from the state object.
Dim handler As Socket = CType(ar.AsyncState, Socket)
Dim x As New System.Net.Sockets.SocketError
' Complete sending the data to the remote device.
Dim bytesSent As Integer = handler.EndSend(ar, x)
If Not x = SocketError.Success Then
MsgBox(x.ToString())
End If
handler.Shutdown(SocketShutdown.Both)
handler.Close()
' Signal the main thread to continue.
allDone.Set()
End Sub Fortsetzung folgt |