从服务器读取文件内容,然后通过套接字将其发送到客户端 C#

发布于 2024-10-01 09:03:59 字数 1856 浏览 7 评论 0原文

我在这里尝试通过服务器发送文本文件的内容并将其发送到客户端

这是服务器客户

Socket server = new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
        server.Bind(localEP);
        server.Listen(10);
        Console.WriteLine("Waiting for Client...");
        Socket client = server.Accept();
        IPAddress clientAddress = ((IPEndPoint)client.RemoteEndPoint).Address;
        Console.WriteLine("Got connection from " + clientAddress);

    NetworkStream stream = new NetworkStream(client); 
    StreamReader reader = new StreamReader(stream);
    StreamWriter writer = new StreamWriter(stream);


    writer.WriteLine("Welcome to my test server");
    writer.Flush();
    string line = null;
    while ((line = reader.ReadLine()).Length != 0)
    {
        Console.WriteLine("loooking for this file:" + line);
        System.IO.FileInfo fi = new System.IO.FileInfo(line);
        Console.WriteLine("Found");
        writer.WriteLine("File Size: " + fi.Length + "\nContent:");
        StreamReader tr = new StreamReader(line);
        string s = null;
        //string b = "";
    while((s= tr.ReadLine()).Length != 0)
        {
            writer.WriteLine(tr.ReadLine());
            writer.Flush();
        }

        tr.Close();

    }
    client.Close(); server.Close();

端从服务器读取的部分是这样

String line = null;
        line = textBox3.Text;
        writer.WriteLine(line);   // Send line to Server
        writer.Flush();
        string s = null;
        // Read line from server, then echo on the screen 
        while((s= reader.ReadLine()).Length != 0)
        {
            textBox4.Text += reader.ReadLine() + "\r\n\r\n";
        }

当我运行代码时,根本没有错误,但是客户端卡住了,当我停止服务器时,文件的内容将显示,,,顺便说一句,它是一个 GUI 应用程序

i am trying here to send the content of a text file by the server and send it to the client

this is the server

Socket server = new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
        server.Bind(localEP);
        server.Listen(10);
        Console.WriteLine("Waiting for Client...");
        Socket client = server.Accept();
        IPAddress clientAddress = ((IPEndPoint)client.RemoteEndPoint).Address;
        Console.WriteLine("Got connection from " + clientAddress);

    NetworkStream stream = new NetworkStream(client); 
    StreamReader reader = new StreamReader(stream);
    StreamWriter writer = new StreamWriter(stream);


    writer.WriteLine("Welcome to my test server");
    writer.Flush();
    string line = null;
    while ((line = reader.ReadLine()).Length != 0)
    {
        Console.WriteLine("loooking for this file:" + line);
        System.IO.FileInfo fi = new System.IO.FileInfo(line);
        Console.WriteLine("Found");
        writer.WriteLine("File Size: " + fi.Length + "\nContent:");
        StreamReader tr = new StreamReader(line);
        string s = null;
        //string b = "";
    while((s= tr.ReadLine()).Length != 0)
        {
            writer.WriteLine(tr.ReadLine());
            writer.Flush();
        }

        tr.Close();

    }
    client.Close(); server.Close();

the part of the client where it reads from the server is this

String line = null;
        line = textBox3.Text;
        writer.WriteLine(line);   // Send line to Server
        writer.Flush();
        string s = null;
        // Read line from server, then echo on the screen 
        while((s= reader.ReadLine()).Length != 0)
        {
            textBox4.Text += reader.ReadLine() + "\r\n\r\n";
        }

when i run the code, no errors at all, but the client get stuck, and when i stop the server, the content of the file will show,,, BTW, its a GUI application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开雨落又逢春i 2024-10-08 09:03:59

while ((s = reader.ReadLine()) != null) {
textBox4.Text += s;
}

while ((s = reader.ReadLine()) != null) {
textBox4.Text += s;
}

旧城空念 2024-10-08 09:03:59

StreamReader 的示例代码 使用以下构造来检测流结束。另外 - 你真的想在该循环中阅读两行吗?

while (reader.Peek() >= 0) 
{
    s= reader.ReadLine();
    textBox4.Text += s + Environment.NewLine + Environment.NewLine;
}

Sample code for StreamReader uses the construct below to detect end of stream. Also - do you really want to read two lines in that loop?

while (reader.Peek() >= 0) 
{
    s= reader.ReadLine();
    textBox4.Text += s + Environment.NewLine + Environment.NewLine;
}
木森分化 2024-10-08 09:03:59

你提到这是一个 GUI 应用程序?如果是这样,您在哪个线程上进行阅读?如果您在主线程上进行读取,则应用程序消息循环将被冻结,并且在您停止另一端并终止连接之前不会显示任何内容。

You mentioned that this is a GUI app? If so, on which thread are you doing the reading? If you are doing the read on the main thread, then the application messageloop will be frozen and nothing will show up until you stop the other side and kill the connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文