F# TCP Socket: how to restore processing after server comes back to normal

Unanswered F# TCP Socket: how to restore processing after server comes back to normal

  • Saturday, December 22, 2012 8:52 AM
     
     

    Hi,

    I am writing a TCP socket client program in F#.  The TCP socket server in Java sends data in real time to socket client.  I used asynchronous socket programming to receive data from server.  I have no control or access in server.

    The issue now is: from time to time, the socket server java program losses its data source, but the program is still running without exception, my program is still running without quitting, but can’t get more data from socket server, after 5 to 10 minutes, even after socket server regains its data source and begins sending data in real time, my program can’t continue to receive data in real time.

    let toIList<'T> (data : 'T array) =

        let segment = new System.ArraySegment<'T>(data)

        let data = new List<System.ArraySegment<'T>>() :> IList<System.ArraySegment<'T>>

        data.Add(segment)

        data

    type Socket with

         member this.MyReceiveAsync(data, flags : SocketFlags) =

                Async.FromBeginEnd(toIList data, flags,

                      (fun (data : IList<System.ArraySegment<byte>>,

                            flags : SocketFlags, callback, state) ->

                            this.BeginReceive(data, flags, callback, state)), this.EndReceive)

    let connectSendReceive (socket : Socket) =

        async

            {

            let receiveBuffer = Array.zeroCreate<byte> 1024

            let flags = new SocketFlags()

            let! result = socket.MyReceiveAsync(receiveBuffer, flags)

            let asyncString = Encoding.ASCII.GetString(receiveBuffer, 0, result)

            process(asyncString)

            return asyncString

            }

    let client = new TcpClient("127.0.0.1", 8080)

    let iSocket = client.Client

    while true do

          let taskClient = Async.StartAsTask(connectSendReceive(iSocket))

          taskClient.Wait()

     

    In the above, the socket server immediately begins to send real time data after the client connects with the server.  The data is string for less than 100 bytes in one chunk, but the data keeps coming all the time until the socket client disconnects. The process block is used to extract necessary data from real time data stream.

    I want to know if I can add some code in the above to make my program pause or sleep if socket server is not sending any data out, but can continue the process block after the data sending is restored.

    Any idea is welcome!

     

All Replies

  • Saturday, December 22, 2012 8:14 PM
     
     

    As always, if you're doing network programming, put client and server on separate hosts so you can capture the network traffic, and see what the server is actually putting on the wire -- is the connection even seeing any traffic at the TCP level during the loss of data or after the resumption?

    Because the server is a black box, it's hard to tell whether there are idiosyncrasies of implementation -- perhaps the server loses track of the whole live connection when the data source is lost, and isn't actually sending anything to you, requiring a new connection to be opened.