简体   繁体   English

无法从C#流中读取

[英]Unable to read from stream in C#

I am building a bare bones program that simple delivers a message from server to client. 我正在构建一个简单的程序,该程序可以简单地将消息从服​​务器传递到客户端。

Now i am successfully able to establish connection between the server and client, however the client program is unable to read from the stream. 现在,我可以成功建立服务器与客户端之间的连接,但是客户端程序无法从流中读取。 Here's my code. 这是我的代码。

Code for server program 服务器程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace chat_client_console
{
    class Program
    {
        static TcpListener listener;
        static void Main(string[] args)
        {
            string name = Dns.GetHostName();
            IPAddress[] address = Dns.GetHostAddresses(name);

            /*
            foreach(IPAddress addr in address)
            {
                Console.WriteLine(addr);
            }*/
            Console.WriteLine(address[1].ToString());
            listener = new TcpListener(address[1], 2055);

            listener.Start();

            Socket soc = listener.AcceptSocket();
            Console.WriteLine("Connection successful");
            Stream s = new NetworkStream(soc);

            StreamReader sr = new StreamReader(s);
            StreamWriter sw = new StreamWriter(s);

            sw.AutoFlush = true;
            sw.Write("A test message");
            Console.WriteLine("Test message delivered. Now ending the program");

            /*
            string name = Dns.GetHostName();
            Console.WriteLine(name);
            //IPHostEntry ip = Dns.GetHostEntry(name);
            //Console.WriteLine(ip.AddressList[0].ToString());
            IPAddress[] adr=Dns.GetHostAddresses(name);
            foreach (IPAddress adress in adr)
            {
                Console.WriteLine(adress);
            }
            */
            Console.ReadLine();
        }
    }
}

and here's the code from the client program 这是客户端程序中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace chat_client_console_client
{
    class Program
    {
        static void Main(string[] args)
        {
            string display;
            TcpClient client = new TcpClient("localhost", 2055);
            Stream s = client.GetStream();
            Console.WriteLine("Connection successfully received");

            StreamWriter sw = new StreamWriter(s);
            StreamReader sr = new StreamReader(s);
            sw.AutoFlush = true;
            while (true)
            {

                display = sr.ReadLine();
                Console.WriteLine("Reading stream");
                if (display == "")
                {
                    Console.WriteLine("breaking stream");
                    break;
                }

            }

            Console.WriteLine(display);
        }
    }
}

now i am successfully able to establish connection between the programs as indicated by various check messages. 现在,我可以成功地在程序之间建立连接,如各种检查消息所示。 The server program is also successfully able to send the data into the stream. 服务器程序还可以成功地将数据发送到流中。

However the client program is unable to read data from the stream. 但是,客户端程序无法从流中读取数据。 It seems to be stuck at readline() function. 它似乎卡在readline()函数中。

Now i have been banging my head against the wall on this problem for hours now and would be greatly thankful if somebody is able to help me. 现在,我已经在这个问题上动脑筋了好几个小时了,如果有人能够帮助我,我将非常感激。

Look at your server: 查看您的服务器:

sw.AutoFlush = true;
sw.Write("A test message");

You're never writing a line break, which is what the client is waiting to see. 您永远不会编写换行符,这是客户端等待的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM