LordTitiKaka
LordTitiKaka

Reputation: 2156

stream was not writable when trying to write to Networkstream

I'm new with socket and trying to write a Client-Server application My applicationhas those two main methods :

SERVER running on separate Thread :

    public void socketListener()
    {
        byte[] StreamMessage = new byte[9632];
        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint localEndPoint =new IPEndPoint(IPAddress.Any , ControlLayer.GlobalParam.PEER2PEER_PORT);
            listener.Bind(localEndPoint);
            listener.Listen(10);

            while (true)
            {
                Socket Handler = listener.Accept();
                //int ByteRec = Handler.Receive(StreamMessage);

                int MessageLength;
                MessageLength = Handler.Receive(StreamMessage, 0, StreamMessage.Length, SocketFlags.None);
                //return MessageLength;

               // string message = System.Text.Encoding.Default.GetString(StreamMessage);
                string message = System.Text.Encoding.UTF8.GetString(StreamMessage);

                OnDataRecievedFromRemotePeer(this, message, "TcpServer");//send data to screen
                Task.Run(() => { ParseMessage(message, Handler); });
            }
    }

once data arrives I prase it collect data and send it using Client CLIENT :

    public void Write(string message)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(CreateClient), message);
    }

    private void CreateClient(object message)
    {
        try
        {
            peerClient = new TcpClient();
            peerClient.Connect(remoteIP, 6001/*TODO remove this */);
            netStream = peerClient.GetStream();//<- Exception 
            StreamWriter sw = new StreamWriter(netStream);
            sw.Write((string)(message));
            netStream.Close();
            peerClient.Close();
        }
        catch(Exception ex)
        {
            //TODO :
        }
    }

Each station is symmetrical and have those two methods

I can tell that the server is working and accepting socket and data but once I want to respond back I get exception in the Line marked in the CreateClient
stream was not writable and when looking on the netStream it is written that I have ObjectDisposed Exception . What can be the cause of that ?

Also please inform me if more code is needed

Upvotes: 4

Views: 1959

Answers (1)

weismat
weismat

Reputation: 7411

You have a classical race here between the server closing the connection before the client has processed the response of the server.
TCP is a "polite" protocol, which means that you can not perform a fire and forget action on the server. The connection needs to be alive on both ends until both sides have processed all messages. Thus either the client needs to send an acknowledge/logout, so that the server can close the connection or at least the server has to wait x seconds until closing it.

Upvotes: 2

Related Questions