WCDeveloper
asked on
NetworkStream.Write - Malformed Packet
I am attempting to send a very simple packet of data over a TCP connection. My server never received the packet, so I connected Wireshark to see what was happening. Wireshark is reporting it as a "Malformed Packet" (packet 1625 of attached capture). I do not understand what is wrong with the data I am sending (or how I'm sending it).
tcpClient = new TcpClient(ipAddress, PORTNUMBER);
netStream = tcpClient.GetStream();
BinaryWriter packet = new BinaryWriter(new MemoryStream(sendBuffer));
packet.Write((uint)BodyType.cmd_GETCALIBPOS);
packet.Write((uint)8);
packet.Write((uint)data1);
packet.Write((uint)data2);
netStream.Write(sendBuffer, 0, 16);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can examine NetworkStream with Reflector:
Public Overrides Sub Write(ByVal buffer As Byte(), ByVal offset As Integer, ByVal size As Integer)
....
streamSocket.Send(buffer, offset, size, SocketFlags.None)
Then NetworkStream on Output is full transparent from StreamSocket.
Public Overrides Sub Write(ByVal buffer As Byte(), ByVal offset As Integer, ByVal size As Integer)
....
streamSocket.Send(buffer, offset, size, SocketFlags.None)
Then NetworkStream on Output is full transparent from StreamSocket.
Public Overrides Sub Write(ByVal buffer As Byte(), ByVal offset As Integer, ByVal size As Integer)
If Me.m_CleanedUp Then
Throw New ObjectDisposedException(MyBase.GetType.FullName)
End If
If (buffer Is Nothing) Then
Throw New ArgumentNullException("buffer")
End If
If ((offset < 0) OrElse (offset > buffer.Length)) Then
Throw New ArgumentOutOfRangeException("offset")
End If
If ((size < 0) OrElse (size > (buffer.Length - offset))) Then
Throw New ArgumentOutOfRangeException("size")
End If
If Not Me.CanWrite Then
Throw New InvalidOperationException(SR.GetString("net_readonlystream"))
End If
Dim streamSocket As Socket = Me.m_StreamSocket
If (streamSocket Is Nothing) Then
Throw New IOException(SR.GetString("net_io_writefailure", New Object() { SR.GetString("net_io_connectionclosed") }))
End If
Try
streamSocket.Send(buffer, offset, size, SocketFlags.None)
Catch exception As Exception
If ((TypeOf exception Is ThreadAbortException OrElse TypeOf exception Is StackOverflowException) OrElse TypeOf exception Is OutOfMemoryException) Then
Throw
End If
Throw New IOException(SR.GetString("net_io_writefailure", New Object() { exception.Message }), exception)
Catch obj1 As Object
Throw New IOException(SR.GetString("net_io_writefailure", New Object() { String.Empty }), New Exception(SR.GetString("net_nonClsCompliantException")))
End Try
End Sub
ASKER
Changed my implementation to use the Socket class instead of the TcpClient class. Now it's working fine.
Thanks!
Thanks!
ASKER
The reason I'm using NetworkStream is because the TcpClient class does not expose the "send" method. It only allows access to the NetworkStream. Read/Writing must be done on the stream.