[Mono-list] Mono Socket Server drops clients when it does big operations

acrym soreric94 at gmail.com
Sun Mar 22 09:42:26 UTC 2015


Mono 3.99 
My socket server runs fine in Windows, but on Linux/Mono it drops a socket.
Whenever the server runs an operation longer than a fraction of a second,
the socket is disconnected, but occasionally the client stays connected. The
client is Silverlight 4 and is run from Windows.

More reproducible is when two clients connect. The first connects fine. The
second connects, but when the server runs an operation, my server
disconnects the first client. When this happens, the first client sends
exactly 0 bytes.

I have my clients in a static list so they won't be GC'd, and timeout is set
high. The only thing I can think of is the client might be pinging the
server, and in that window of time when it handles the message, it's not in
a receive state. But that would make sockets extremely fragile.

Is there any reason - Mono-related or otherwise - that could cause this
behavior?

  static void OnBeginAccept(IAsyncResult ar)
    {
        try
        {
            TcpClient _MessageClient =
_MessageListener.EndAcceptTcpClient(ar);
            byte[] _ReceiveBuffer = new byte[8197];
            System.IO.StreamWriter streamW = new
System.IO.StreamWriter(_MessageClient.GetStream());
            ClientDefinition newClient = new ClientDefinition();
            newClient.Socket = _MessageClient.Client;
            newClient.Socket.ReceiveTimeout = 7000;
newClient.Socket.SendTimeout = 7000;
            newClient.username = "undefined" + clients.Count;
            newClient.receiveBuffer = new byte[8197];
            clients.Add(newClient);

            newClient.Socket.BeginReceive(newClient.receiveBuffer, 0, 8196,
SocketFlags.None, new AsyncCallback(OnReceiveComplete), newClient);
            _MessageListener.BeginAcceptTcpClient(new
AsyncCallback(OnBeginAccept), null);
            Console.WriteLine("new client accepted");
        }
        catch (Exception ex) { LogError(ex); }
    }


  static void OnReceiveComplete(IAsyncResult ar)
    {
        ClientDefinition fromClient = null; //Wrapper for Socket
        fromClient = (ClientDefinition)ar.AsyncState;
        if (fromClient == null) return;

        try
        {
            if (!fromClient.Socket.Connected)
            {
                Console.WriteLine("fromclient (" + fromClient.username + ")
not connected");
                CloseClient(fromClient);
                return;
            }
            int receiveLength = 0;
            receiveLength = fromClient.Socket.EndReceive(ar);
            if (receiveLength == 0)
            {
                Console.WriteLine("Receivelength == 0");
                CloseClient(fromClient);
            }


            SocketMessage message = null;
            message =
Serializer.Deserialize<SocketMessage>(fromClient.receiveBuffer);
            System.Array.Clear(fromClient.receiveBuffer, 0, receiveLength);

            /******Here is where I handle the messages. And when doing an
operation that is 'big', a client is closed.*****/

            fromClient.Socket.BeginReceive(fromClient.receiveBuffer, 0,
8196, SocketFlags.None, new                        
AsyncCallback(OnReceiveComplete), fromClient);


        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception in OnReceiveComplete: " +
ex.Message + ex.StackTrace + "obj: " + ex.Source);
            CloseClient(fromClient);

        }





--
View this message in context: http://mono.1490590.n4.nabble.com/Mono-Socket-Server-drops-clients-when-it-does-big-operations-tp4665670.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list