开发者

C# Socket doesn't disconnect properly

开发者 https://www.devze.com 2023-01-27 12:41 出处:网络
I\'m working on a server f开发者_开发技巧or the game Minecraft, which shows a dark-red screen when the player is disconnected. I\'m disconnecting players like this:

I'm working on a server f开发者_开发技巧or the game Minecraft, which shows a dark-red screen when the player is disconnected. I'm disconnecting players like this:

Socket.Shutdown( SocketShutdown.Both );
Socket.Close();

I seem to be unable to write data after that, but the disconnected screen doesn't appear. It only appears when I completely terminate the server application, which indicates that the connection isn't closed at all.

What am I doing wrong?

Edit:

socket.BeginAccept( new AsyncCallback( AcceptClient ), null );

private void AcceptClient( IAsyncResult result )
    {
        try
        {
            // Initialize player
            Socket client = _socket.EndAccept( result );

.

Log( "Received unrecognized packet from " + player.IPAddress() + ", disconnecting client!" );

player.Disconnect();
_clients.Remove( player );


I don't know what the API looks like for Minecraft, but odds are that there's a command you need to send to the client first, to let them know they've been disconnected.


What about Socket.Disconnect() ?

0

精彩评论

暂无评论...
验证码 换一张
取 消