简体   繁体   English

为什么此f#回显服务器不能被许多客户端连接?

[英]Why this f# echo server can't be connected by many clients?

I write this echo server: 我写这个回显服务器:

let listener=new TcpListener(IPAddress.Parse("127.0.0.1"),2000)

let rec loop (client : TcpClient,sr : StreamReader, sw : StreamWriter) = 
async {
        let line=sr.ReadLine()
        sw.WriteLine(line)
        if line="quit" then
            client.Close()
        else

            return! loop(client,sr,sw)
}

let private startLoop (listener:TcpListener) = 
    while true do
        let client = listener.AcceptTcpClient()
        let stream = client.GetStream()
        let sr = new StreamReader(stream)
        let sw = new StreamWriter(stream)
        sw.AutoFlush <- true
        sw.WriteLine("welcome")
        Async.Start(loop (client,sr,sw))

[<EntryPoint>]
let main argv = 
 listener.Start()
 startLoop(listener)
 0

when I open one or two telnet window to test it,it works fine 当我打开一个或两个telnet窗口进行测试时,它可以正常工作

but when I write this test program to test it: 但是当我编写此测试程序对其进行测试时:

static void Main(string[] args)
    {
        for (int a = 0; a < 5; a++)
        {
            var client = new TcpClient("localhost", 2000);
            Console.WriteLine(client.Connected);
            client.close();
        }
    }

the test program return one or two true,but the server raise an exception: 测试程序返回一两个true,但是服务器引发异常:

System.Net.Sockets.SocketException:An existing connection was forcibly closed by the remote host System.Net.Sockets.SocketException:现有的连接被远程主机强行关闭

in line 12:let line=sr.ReadLine() 在第12行:let line = sr.ReadLine()

and client raise the exception:System.Net.Sockets.SocketException:Because the target computer actively refused, unable to connect at line 16:var client = new TcpClient("localhost", 2000); 和客户端引发异常:System.Net.Sockets.SocketException:由于目标计算机被主动拒绝,无法在第16行连接:var client = new TcpClient(“ localhost”,2000);

I don't know why,please help me 我不知道为什么,请帮助我

Your problem is that the client opens a connection and then immediately closes it. 您的问题是客户端打开了一个连接,然后立即将其关闭。

The server however expects a "quit" message from the client before it will terminate the connection. 但是,服务器在终止连接之前需要客户端发出"quit"消息。 So the server sends a "welcome" to the client, then enters the loop . 因此,服务器向客户端发送"welcome" ,然后进入loop Inside the loop, sr.ReadLine() is called, which waits for the client to send something over the wire. 在循环内部,将调用sr.ReadLine() ,它等待客户端通过网络发送内容。

The client never sends anything. 客户端从不发送任何东西。 It closes the connection. 它将关闭连接。 Therefore, the server's call to ReadLine aborts with the a SocketException (forcibly closed...). 因此,服务器对ReadLine的调用会因SocketException被中止(强制关闭...)。 And you do not handle this exception, so the server dies. 而且您不处理此异常,因此服务器死了。

Then the client tries to connect once again, with no server listening anymore. 然后,客户端尝试再次连接,而不再有服务器在监听。 The client can't connect and you see another SocketException (actively refused...). 客户端无法连接,您会看到另一个SocketException (正在主动被拒绝...)。

You should guard your server code against clients that disconnect without saying "quit" first. 您应该保护服务器代码,以防客户端断开连接而不必先说"quit"

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

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