简体   繁体   English

客户端发出一些请求后,TCP服务器停止响应[Windows 10 Universal app,C#,XAML]

[英]TCP server stops responding after a few requests from client [Windows 10 Universal app, C#, XAML]

I tried to make a little UWP card game to learn how to use TCP, but my server always stops responding to the client after a few connections. 我尝试制作一些UWP纸牌游戏来学习如何使用TCP,但是我的服务器在几次连接后总是停止响应客户端。

The client sends different messages to the server, like "DrawCard;(name of card)". 客户端向服务器发送不同的消息,例如“ DrawCard;(卡名)”。 Interestingly, the first 30-40 messages always reach the server without a problem, but after certain types of messages the server just stops accepting any new ones: For instance, I can draw as many cards as I like, but when I play one, the server stops listening. 有趣的是,前30到40条消息始终可以毫无问题地到达服务器,但是某些类型的消息之后,服务器将停止接受任何新消息:例如,我可以画任意数量的卡,但是当我玩时,服务器停止监听。

I have tried to solve this problem for a few hours now, so I hope someone can help me. 我已经尝试解决这个问题了几个小时,所以希望有人能帮助我。

When the app starts, this function is called: 应用启动时,将调用以下函数:

public async static Task StartListening()
    {
        Windows.Networking.Sockets.StreamSocketListener socketListener = new Windows.Networking.Sockets.StreamSocketListener();
            socketListener.ConnectionReceived += SocketListener_ConnectionReceived;

            await socketListener.BindServiceNameAsync("1337");

            //If I leave this line out, it doesn´t even start listening for connections. Any idea why?
            await System.Threading.Tasks.Task.Delay(500);
}

This is the event that gets triggered when a connection is received: 这是收到连接时触发的事件:

public async static void SocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender,
Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args)
    {
            Stream inStream = args.Socket.InputStream.AsStreamForRead();
            StreamReader reader = new StreamReader(inStream);
            string request = await reader.ReadLineAsync();

            HandleData(request);

            ////Send the line back to the remote client.
            Stream outStream = args.Socket.OutputStream.AsStreamForWrite();
            StreamWriter writer = new StreamWriter(outStream);
            await writer.WriteLineAsync(request);
    }

HandleData() uses HandleData()的使用

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () =>
                    {
                         /...
                    });

to edit some ObservableCollections that are bound to UI elements. 编辑绑定到UI元素的一些ObservableCollections。

The client code is: 客户端代码为:

public async Task SendDataToServer(string data)
    {
        Windows.Networking.Sockets.StreamSocket socket = new Windows.Networking.Sockets.StreamSocket();

            Windows.Networking.HostName serverHost = new Windows.Networking.HostName(hostname);

            string serverPort = "1337";
            await socket.ConnectAsync(serverHost, serverPort);

            //Write data to the server.
            Stream streamOut = socket.OutputStream.AsStreamForWrite();
            StreamWriter writer = new StreamWriter(streamOut);
            await writer.WriteLineAsync(data);

            //Read data from the server.
            Stream streamIn = socket.InputStream.AsStreamForRead();
            StreamReader reader = new StreamReader(streamIn);
            string response = await reader.ReadLineAsync();
        }
    }

In my test scenario, the client app runs on my Windows Mobile phone and the server runs on my PC. 在我的测试方案中,客户端应用程序在Windows Mobile手机上运行,​​服务器在我的PC上运行。 The PC doesn´t send any data back to the client apart from returning the received message to make sure it was received correctly. 除了返回已接收的消息以确保正确接收消息外,PC不会将任何数据发送回客户端。

Thank you very much for any help you can offer. 非常感谢您提供的任何帮助。 I have tried everything I could find on the Internet, but I didn´t find anything that worked for me. 我已经尝试了所有可以在Internet上找到的东西,但是没有找到对我有用的东西。

Thank you very much @Jay Zuo - MSFT! 非常感谢@Jay Zuo-MSFT! I looked at the official samples and found out that I missed the following line of code: 我查看了官方示例,发现我错过了以下代码行:

CoreApplication.Properties.Add("listener", socketListener);

According to the MS sample, this "save[s] the socket, so subsequent steps can use it." 根据MS示例,这“节省了套接字,因此后续步骤可以使用它”。 The app works as intended now. 该应用程序现在可以正常运行。 Thank you very much for your help, @Rafael and @Jay Zuo - MSFT ! 非常感谢您的帮助,@ Rafael和@Jay Zuo-MSFT!

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

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