Как отладить, почему моя программа потоков не может читать? - PullRequest
0 голосов
/ 14 января 2019

Я должен отправлять и получать сообщения одновременно, используя поток, но я могу только записывать данные, но не могу читать. Вот мой код:

Сторона сервера:

namespace ServerApplicaation
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any,3300);
            server.Start();
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("Send your data to {0}", client.Client.RemoteEndPoint);
            NetworkStream networkStream = client.GetStream();
            string str = "";
            int stream = 0;
            byte[] store;
            while (client.Connected)
            {
                Thread t1 = new Thread(() => Read(networkStream, out stream, out store));
                Thread t2 = new Thread(() => Write(networkStream, out str, out store));
                t1.Start();
                t2.Start();
            }
        }

        private static void Read(NetworkStream networkStream, out int stream, out byte[] store)
        {
            store = new byte[1024];
            stream = networkStream.Read(store, 0, store.Length);
            string s = Encoding.ASCII.GetString(store, 0, stream);
            Console.WriteLine("Client: {0}", s);
        }

        private static void Write(NetworkStream networkStream, out string str, out byte[] store)
        {
            str = Console.ReadLine();
            store = Encoding.ASCII.GetBytes(str);
            networkStream.Write(store, 0, store.Length);
        }
    }
}

Клиентская сторона:

namespace ClientApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            TcpClient client = new TcpClient("127.0.0.1",3300);
            NetworkStream networkStream = client.GetStream();
            byte[] store;
            string str = "";
            int stream = 0;
            while (client.Connected)
            {
                //Read(networkStream, stream, store);
                Thread t1 = new Thread(() => Write(networkStream, out str, out store));
                Thread t2 = new Thread(() => Read(networkStream, out stream, out store));
                t1.Start();
                t2.Start();


            }

        }

        private static void Read(NetworkStream networkStream, out int stream, out byte[] store)
        {
            store = new byte[1024];
            stream = networkStream.Read(store, 0, store.Length);
            string s = Encoding.ASCII.GetString(store, 0, stream);
            Console.WriteLine("Server: {0}", s);
        }

        private static void Write(NetworkStream networkStream, out string str, out byte[] store)
        {
            //SENDING THE MESSAGE
            str = Console.ReadLine();
            store = Encoding.ASCII.GetBytes(str);
            networkStream.Write(store, 0, store.Length);
        }
    }
}

Я хочу сделать одновременное общение.

...