gpt4 book ai didi

c# - TCP 在不关闭连接的情况下发送多条消息

转载 作者:可可西里 更新时间:2023-11-01 02:54:01 26 4
gpt4 key购买 nike

大家好,我已经为 Android 构建了一个应用程序来获取 GPS 协调,我想通过 TCP 将数据发送到我的 C# UWP 服务器。作为概念,我打开了一个套接字,我想在不关闭套接字的情况下发送多条消息。

socket = new java.net.Socket("192.168.2.10", 9999);
printwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
printwriter.println("message1");
printwriter.println("message2");
printwriter.println("message3");
printwriter.println("message4");
printwriter.flush();

问题是我在服务器上只收到消息 1,有时也收到消息 2。另一条消息不会显示在服务器上。我不想建立新连接,因为我打算发送很多消息。如果你们知道解决方案,我们将不胜感激。

我目前正在使用来自 https://msdn.microsoft.com/en-us/windows/uwp/networking/sockets 的 C# 中的 UWP 服务器代码.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Maps
{

class Connection
{
public async void Connectie()
{
try
{
System.Diagnostics.Debug.WriteLine("Waiting for connection................");

//Create a StreamSocketListener to start listening for TCP connections.
Windows.Networking.Sockets.StreamSocketListener socketListener = new Windows.Networking.Sockets.StreamSocketListener();

//Hook up an event handler to call when connections are received.
socketListener.ConnectionReceived += SocketListener_ConnectionReceived;

//Start listening for incoming TCP connections on the specified port. You can specify any port that' s not currently in use.
await socketListener.BindServiceNameAsync("9999");
System.Diagnostics.Debug.WriteLine("Waiting for connection................");
}
catch (Exception e)
{
//Handle exception.
}
}

private async void SocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender,
Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args)
{
Stream inStream = args.Socket.InputStream.AsStreamForRead();
StreamReader reader = new StreamReader(inStream);
reader = new StreamReader(args.Socket.InputStream.AsStreamForRead());
System.Diagnostics.Debug.WriteLine("connection................");
//Read line from the remote client.
string request = await reader.ReadLineAsync();
System.Diagnostics.Debug.WriteLine(request);
}
}
}

最佳答案

除了@Hasson 所说的阅读所有行之外,您还应该确保正确处理流(最好在 StreamReader 上使用 using,并且您可以在 Stream 上执行相同的操作,尽管 StreamReader 处理也会关闭它)。看起来您的代码希望在一次调用中获取整个消息,因此如果您不想使用 while 循环在每个 ReadLineAsync 之前检查 !reader.EndOfStream,您可以执行如下操作:

using (Stream inStream = args.Socket.InputStream.AsStreamForRead())
using (StreamReader reader = new StreamReader(inStream))
{
System.Diagnostics.Debug.WriteLine("connection................");
//Read line from the remote client.
string request = await reader.ReadToEndAsync();
System.Diagnostics.Debug.WriteLine(request);
}

我也有点担心这个类的用法。如果实例化 new Connection() 的代码没有保持对它的引用可用,垃圾收集器将吞噬它,您的服务器将不再监听。

例如,如果您在点击按钮或加载页面时执行如下操作:

Connection conn = new Connection();
conn.Connectie();

如果没有维护 conn 的引用,那么在该函数调用完成后,垃圾收集器将自动处理它和您的 StreamSocketListener。

关于c# - TCP 在不关闭连接的情况下发送多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com