tcpServer:如何发送内容并随后关闭 TCP 连接

问题描述 投票:0回答:1

我正在我的 C# 应用程序中使用此处给出的

tcpServer

http://www.codeproject.com/Articles/488668/Csharp-TCP-Server

它有一个

TcpServer
类,可以打开端口并开始监听它。我通过
OnDataAvailable
类提供的
TcpServer
事件处理程序接收数据。问题是,客户端期望并依赖 tcp 服务器来关闭连接。

在关闭连接之前,(有条件地)我想将一些数据发送回客户端。这是我为了实现同样的目标而写的:

static void myServer_OnDataAvailable(TcpServerConnection connection)
{
    //...Some code...
    connection.sendData(someText);
    if (connection.verifyConnected())
    {
        connection.forceDisconnect();
    }
}

现在,当我执行此代码时,它会在向客户端发送文本之前关闭连接。

我怎样才能达到同样的效果?

c# sockets tcp
1个回答
0
投票

试试这个

static void myServer_OnDataAvailable(TcpServerConnection connection)
{
    //...Some code...
    connection.sendData(someText);
    Thread.Sleep(2000);
    if (connection.verifyConnected())
    {
        connection.forceDisconnect();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.