tags:

views:

219

answers:

2

My server use UDP. It sends 900bytes/1ms to my program automatically after being acquired. I'm using socket API in Windows (VB 6). I had made a test and I know that the message processing time (about 0.3ms) of my program is shorter than cycle time (1ms). So the cause should be socket internal buffer. I try calling setsockopt function to set the bigger buffer:

setsockopt(SockNum, SOL_SOCKET, SO_RCVBUF, SockBuffer(1), 1048576)

but I still lost data. How can I fix my problem? I'm using recv function to receive data. Should recvfrom be better?

More one, I need make a FIFO buffer for UDP. How I can do (algorithms or examples)?

+1  A: 

In your question you seem to be complaining about using UDP and losing data.

If you are using UDP, you are going to lose data. The way that you avoid losing data is to use TCP, not UDP. If you try to take the User Datagram Protocol and add reliable delivery of data to it, you will end up with something that has all of the flow-control and data windowing of TCP... except it won't be implemented as well as you want.

Remember, "Those who do not understand TCP are doomed to reinvent it.... poorly"

vy32
you can always implement a TCP like SYN/ACK system, but with less verifications, thus being a middle ground between TCP and UDP. But if the problem is losing data, it's probably because of UDP, yes.
Ricardo Ferreira
thanks, but my server is a electronic board. It use UDP and I can't change
kheo
regarding, my computer (running my program - client) connect direct to the server by 5m cable so I think it is hardly to lost data.
kheo
There are many places to lose data other than the physical layer. They might be lost because of background processes on your computer, or buffers being overflowed, or what have you. As soon as you add SYN/ACK, you are re-inventing TCP. But badly. What about your flow control? Window size? Sliding window sizes? Etc.
vy32
A: 

I can't change the server side: protocol, add SYN/ACK, etc. So I need find the way at client side. The provider of server board supplied a manager program. It acquires data without data lost. So I think there is not problem in the platform on my computer. Thanks for all of your support but I hope you can help me to find the way at client side (my program). Don't refer to change to TCP or other protocols or add any flowcontrol because my server can't be change.

In my program, I using hooking, WSAAsyncSelect function and checking FD_READ event to receive data. But if server sent 1000 packets (900 bytes). My program received FD_READ event quantity no enough (<1000, normally 600 -> 700 packets). What is wrong?

Public Sub ProcessMessage(ByVal lFromSocket As Long, ByVal lParam As Long)
    Dim X As Long
    Dim sockout As SOCKADDR
    Dim sockaddrsize As Long

    if FD_READ = lParam then                              

        sockout = saZero
        sockout.sin_family = AF_INET
        sockout.sin_port = htons(PORT_TO_CONNECT)
        sockout.sin_addr = GetHostByNameAlias(SERVER_TO_CONNECT)
        sockaddrsize = Len(sockout)

        strData = ""

        'Do
            X = recvfrom(lFromSocket, ReadBuffer(1), 1048576, 0, sockout, sockaddrsize)
            If X > 0 Then
                strData = strData & Left$(StrConv(ReadBuffer, vbUnicode), X)
            End If
        'Loop Until X = 0 Or X = SOCKET_ERROR

        If strData <> "" Then
            vSamples = vSamples + 1
            Put FileNum, , strData
        End If
    End If
End Sub
kheo