meriano

Hello all

I have a weird situation and I pretty much dont know where to go in trying to solve it. I am sending 58 bytes 10 times a second from one PC to another. I am the .BeginReceive & .EndReceive of the socket class and I hard coded the buffer size to the number of bytes I want to receive. I do get the 58 bytes every time but when I look at the byte stream - the bytes are sometimes swapped. I monitor the sending end and everything looks fine when it is being transmitted but when I monitor the receive end, it is all out of sorts but I dont get any error messages. Is there something I am missing

M


Re: Visual Basic General Tcp receive acting strange

nobugz

Not much to go by without seeing code. But that might just be what it looks like when you receive 58 bytes but send more or less. You'll see a packet that is made from the tail of the previous one and the head of the next one.





Re: Visual Basic General Tcp receive acting strange

meriano

Thanks - yeah its my simulator issue it was missing a byte every so many frames.

I have 4 channels running right now and each is hardcoded to receive a certain number of bytes. I want the program to be as versatile as possible so i want to move away from hardcoding the TCP buffer size but whenever I do I seem to get the same issue as I mentioned in my first post. Is there a way to get around this.

It does seem to work on the 2 channels that are receiveing 1 frame a seoncd but the other 2 are receiveing 10 frames a second and the problem is consistent. (I set a default buffer sixe of 10K on every channel). I would assume that a delay between sending frames would help would this affect the timing of the data, it still needs to be stored at 10 times per seocnd

Thanks again




Re: Visual Basic General Tcp receive acting strange

nobugz

The standard technique is to put the size of the packet ahead of the packet. On the receiving side, you would first read the bytes that give the size, then read as many bytes as needed to get the rest of the packet.





Re: Visual Basic General Tcp receive acting strange

meriano

Thanks for the reply - I have seen this suggestion on other threads but i was not sure exactly how to go about doing it. Do i put the packet size in the beginning of the packet or do i send it first and then send the actual data. I only ask because I use a queue to stack all of the data in one thread and then I dequeue on the transmitting thread. I guess since queues are first in first out, if I put the number of bytes on the queue first and then the data it should still be transmitted properly!
well I will give it a shot anyway.

m




Re: Visual Basic General Tcp receive acting strange

nobugz

It doesn't matter. It is a stream, not a set of packets. The logical thing to do is to let the transmitting thread put the length on the stream since it is an implementation detail.





Re: Visual Basic General Tcp receive acting strange

meriano

Hi again,
I have been trying your suggestion without success for the past week or so. I know how to get the number of bytes I am about to send but i cant seem to figure out how to get the length on the stream. I have been looking found many posts that say this is the best way to do it but I have yet to find any examples of the code. Would it be possible to get me started.
The other thing is that i am not using NetworkStream asI have seen in most posts - i am currently just using socket.beginSend. Is there an advantage of NetworkStream

Thanks again in advance




Re: Visual Basic General Tcp receive acting strange

nobugz

Socket.BeginSend() has four overloads. Two of them take a byte[], offset and size. Since you specify the size, how could you not know the size You'd better post your code.





Re: Visual Basic General Tcp receive acting strange

meriano

I apologize my last post was not really clear. I do know and send the number of bytes in the socket.BeginSend() method but I am not sure how to receive only that number of bytes on the receiver. The number of bytes is variable so I know I cannot hard code the receive buffer size, but how do you only receive a specific number of bytes.

Sending code

Public Sub Send(ByVal dataToSend As String)
Try
bytesToSend() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(dataToSend)
mysocket.Send(bytesToSend,0,bytesToSend.Length)

Catch se as SocketException
MsgBox(se.ToString)

End Try
End Sub


Receiving

Private Class StateClass
Public clientSocket As Socket
Public clientBuffer() As Byte ------> how do i know the buffer size before hand
End Sub

Public Sub OnAcceptConnection(ByVal result as IAsyncResult)
Dim state as New stateClass
Dim serverSocket As Socket = CType(result.AsyncState, Socket)

Try
state.clientSocket = serverSocet.EndAccept(result)

state.clientSocket.BeginReceive(state.clientBuffer,0,state.clientBuffer.Length,SocketFlags.None, _
New AsyncCallback(AddressOf ReceiveCallback,stateClass)


Catch acceptex as Exception
MsgBox(acceptex.ToString)

EndTry
EndSub

Private ReceiveCallback(ByVal result2 as IAsyncResult)
Try
Dim state As stateClass,CType(result2.AyncState,stateClass)
Dim numberBytesRead As Int32 = state.clientSocket.EndReceive(result2)

If numberBytesRead >0 Then
Dim receivedString As String = ASCII.GetString(state.clientBuffer)
Console.WriteLine(receivedString)
End If

Catch ex As Exception
MsgBox(ex.ToString)

End Try
End Sub