当flutter-app向服务器发送消息时,当flutter-app(client)断开连接时,c#服务器仅将其打印出来

问题描述: C#服务器可完美地与C#客户端配合使用,但是当我尝试使用Flutter应用程序(Dart客户端代码)将其加入时,它仅在与服务器断开连接时才向服务器发送“ hello”消息。客户端断开连接后,C#服务器将连续打印出“十六进制0a”字符,称为换行字符。我该如何解决?解决方案的代码是什么?谢谢! ^^

C#服务器:


    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Text;
    using System.Threading;

    namespace Server
    {
        class Program
        {
            private static TcpListener tcpListener;
            private static List<TcpClient> tcpClientsList = new List<TcpClient>();
            private static int clientsonServer = 0;

            static void Main(string[] args)
            {
                Console.Write("Please input,what the port should be: ");
                int port = Convert.ToInt32(Console.ReadLine());

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

                Console.Write(time());
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Server started");

                while (true)
                {
                    TcpClient tcpClient = tcpListener.acceptTcpClient();
                    tcpClientsList.Add(tcpClient);
                    Thread thread = new Thread(ClientListener);
                    thread.Start(tcpClient);
                    StreamWriter sWriter = new StreamWriter(tcpClient.GetStream());
                    //sWriter.WriteLine(QuestionsIntoArray());
                    //sWriter.Flush();
                    clientsonServer += 1;
                    Thread.Sleep(100);
                    Console.Write(time());
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine("There are " + clientsonServer + " clients on the server right now");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    //tcpClientsList.ForEach(Console.WriteLine);
                }
            }

            public static void ClientListener(object obj)
            {
                TcpClient tcpClient = (TcpClient)obj;
                StreamReader reader = new StreamReader(tcpClient.GetStream());

                Console.Write(time());
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Client connected");
                while (true)
                {
                    try {
                        string message = reader.ReadLine();
                        BroadCast(message,tcpClient);
                        Console.Write(time());
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine(message);
                    } catch (IOException) {
                        clientsonServer -= 1; 
                        Console.Write(time());
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine("A client has been disconnected");
                        Console.Write(time());
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.WriteLine("There are " + clientsonServer + " clients on the server right now");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;
                    }
                }
            }

            public static void BroadCast(string msg,TcpClient excludeclient)
            {
                foreach (TcpClient client in tcpClientsList)
                {
                    if (client != excludeclient)
                    {
                        try
                        {
                            StreamWriter sWriter = new StreamWriter(client.GetStream());
                            sWriter.WriteLine(msg);
                            sWriter.Flush();
                        } catch (Exception) {
                            //Console.WriteLine("Broadcast not possible");
                        }
                    }
                }
            }

            /*static string QuestionsIntoArray()
            {
                string text = System.IO.File.ReadAllText(@"C:\Users\nyiro\Desktop\Questions.txt");
                //string[] lines = System.IO.File.ReadAllLines(@"C:\Users\nyiro\Desktop\Questions.txt");
                return text;
            }*/

            static string time()
            {
                String datetime = Convert.ToString(DateTime.Now);
                string[] splittedTime = datetime.Split(' ');
                datetime = "[" + splittedTime[0] + splittedTime[1] + splittedTime[2] + " " + splittedTime[3] + "] ";
                Console.ForegroundColor = ConsoleColor.Green;
                return datetime;
            }
        }
    }

Flutter(Dart客户端):

import 'dart:io';
import 'dart:convert';
import 'dart:async';

main() async {
  try {
    Socket socket = await Socket.connect('192.168.0.17',5000);
    socket.add(utf8.encode('hello'));
    //socket.close();
  } catch (Exception) {
    print('cant join');
  }
  /*print('connected');
  socket.add(utf8.encode('hello'));

  // listen to the received data event stream
  socket.listen((List<int> event) {
    print(utf8.decode(event));
  });

  // send hello
  socket.add(utf8.encode('hello'));

  // wait 5 seconds
  //await Future.delayed(Duration(seconds: 5));

  // .. and close the socket
  //socket.close();*/
}
dalaoer007 回答:当flutter-app向服务器发送消息时,当flutter-app(client)断开连接时,c#服务器仅将其打印出来

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2961782.html

大家都在问