如何发送“你好”到服务器并回复“嗨”?

发布于 2024-10-31 15:21:48 字数 1051 浏览 4 评论 0原文

使用我的代码,我可以在服务器上读取消息并从客户端写入消息。但我无法从服务器写入响应并在客户端中读取。

客户端上的代码

var cli = new TcpClient();

cli.Connect("127.0.0.1", 6800);

string data = String.Empty;

using (var ns = cli.GetStream())
{
    using (var sw = new StreamWriter(ns))
    {
        sw.Write("Hello");
        sw.Flush();

        //using (var sr = new StreamReader(ns))
        //{
        //    data = sr.ReadToEnd();
        //}
    }
}

cli.Close();

服务器上的代码

tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();

while (run)
{
    var client = tcpListener.AcceptTcpClient();

    string data = String.Empty;

    using (var ns = client.GetStream())
    {
        using (var sr = new StreamReader(ns))
        {
            data = sr.ReadToEnd();

            //using (var sw = new StreamWriter(ns))
            //{
            //    sw.WriteLine("Hi");
            //    sw.Flush();
            //}
        }
    }
    client.Close();
}

如何让服务器读取数据后回复并让客户端读取此数据?

With my code I can read a message on the server and write from the client. But I am not being able to write a response from the server and read in the client.

The code on the client

var cli = new TcpClient();

cli.Connect("127.0.0.1", 6800);

string data = String.Empty;

using (var ns = cli.GetStream())
{
    using (var sw = new StreamWriter(ns))
    {
        sw.Write("Hello");
        sw.Flush();

        //using (var sr = new StreamReader(ns))
        //{
        //    data = sr.ReadToEnd();
        //}
    }
}

cli.Close();

The code on the server

tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();

while (run)
{
    var client = tcpListener.AcceptTcpClient();

    string data = String.Empty;

    using (var ns = client.GetStream())
    {
        using (var sr = new StreamReader(ns))
        {
            data = sr.ReadToEnd();

            //using (var sw = new StreamWriter(ns))
            //{
            //    sw.WriteLine("Hi");
            //    sw.Flush();
            //}
        }
    }
    client.Close();
}

How can I make the server reply after reading the data and make the client read this data?

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

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

发布评论

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

评论(2

最舍不得你 2024-11-07 15:21:48

由于您使用的是

TcpClient client = tcpListener.AcceptTcpClient();

,因此您可以直接写回客户端,而不需要它进行自我识别。 如果您使用 Stream.Read().ReadLine() 而不是 .ReadToEnd(),您拥有的代码将实际工作ReadToEnd() 将在网络流上永远阻塞,直到流关闭。请参阅类似问题的此答案,或来自 MSDN,

ReadToEnd 假设流
知道它什么时候结束。为了
交互协议,其中
服务器仅在您请求时才发送数据
对于它并且不关闭
连接,ReadToEnd 可能会阻塞
无限期,因为它没有达到
结束,应该避免。

如果您在一侧使用 ReadLine(),则需要在另一侧使用 WriteLine(),而不是 Write()。另一种方法是使用循环调用 Stream.Read() 直到没有任何内容可供读取。您可以在 MSDN 上的 AcceptTcpClient() 文档。相应的客户端示例位于 TcpClient 文档

Since you are using

TcpClient client = tcpListener.AcceptTcpClient();

, you can write back to the client directly without needing it to self-identify. The code you have will actually work if you use Stream.Read() or .ReadLine() instead of .ReadToEnd(). ReadToEnd() will block forever on a network stream, until the stream is closed. See this answer to a similar question, or from MSDN,

ReadToEnd assumes that the stream
knows when it has reached an end. For
interactive protocols in which the
server sends data only when you ask
for it and does not close the
connection, ReadToEnd might block
indefinitely because it does not reach
an end, and should be avoided.

If you use ReadLine() at one side, you will need to use WriteLine() - not Write() - at the other side. The alternative is to use a loop that calls Stream.Read() until there is nothing left to read. You can see a full example of this for the server side in the AcceptTcpClient() documentation on MSDN. The corresponding client example is in the TcpClient documentation.

软的没边 2024-11-07 15:21:48

俗气,低效,但对一次性程序来说却很有效:

  • 客户端:在流中,包含它希望接收响应的端口和 IP 地址。
  • 客户端:为此创建一个监听器
    端口和IP。
  • 服务器:读入端口/IP信息并
    依次连接,然后发送回复
    溪流。

但是,这是一个很好的起点,请查看 Sockets 类正确的双向通信。

Cheesy, inneficient, but does the trick on a one-time throwaway program:

  • Client: In the stream, include the port and IP address it wishes to receive the response from.
  • Client: Create a listener for that
    port and IP.
  • Server: Read in the port/IP info and
    in turn connect, then send the reply
    stream.

However, this is a great place to start, look into Sockets class for proper bi-directional communication.

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