Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法读取C中的流#_C#_.net_Sockets - Fatal编程技术网

C# 无法读取C中的流#

C# 无法读取C中的流#,c#,.net,sockets,C#,.net,Sockets,我正在构建一个简单的程序,它可以将消息从服务器传递到客户端 现在我可以成功地在服务器和客户端之间建立连接,但是客户端程序无法从流中读取数据。这是我的密码 服务器程序代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Net.Sockets; namespace chat_cl

我正在构建一个简单的程序,它可以将消息从服务器传递到客户端

现在我可以成功地在服务器和客户端之间建立连接,但是客户端程序无法从流中读取数据。这是我的密码

服务器程序代码

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();
        }
    }
}
这是客户端程序的代码

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);
        }
    }
}
现在,我能够成功地建立程序之间的连接,如各种检查消息所示。服务器程序也能够成功地将数据发送到流中

但是,客户端程序无法从流中读取数据。它似乎卡在readline()函数上

现在我已经为这个问题绞尽脑汁好几个小时了,如果有人能帮助我,我将不胜感激。

看看你的服务器:

sw.AutoFlush = true;
sw.Write("A test message");
你从来没有写过换行符,这是客户端等待看到的。

看看你的服务器:

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

你从来没有写过换行符,这是客户端等待看到的。

换句话说(对于OP,而不是Skeet),你的客户端有
sr.ReadLine()
,因此正在等待换行符。好的,我在服务器代码中添加了换行符,现在客户端代码似乎不再被卡住。然而,我没有得到“测试消息”,而是从客户机代码中的流中得到了空白。是其他原因吗…….换句话说(对于OP,而不是Skeet),您的客户端有
sr.ReadLine()
,因此正在等待换行。好的,我在服务器代码中添加了换行符,现在客户端代码似乎不再卡滞。然而,我没有得到“测试消息”,而是从客户机代码中的流中得到了空白。是别的什么吗。。。。。。。