' This demonstrates how to perform a graceful disconnect, where data sending/receiving ' is allowed to complete before the client is disconnected. ' In this demo the server begins the graceful disconnect process ' It is also possible for a client to initiate a graceful disconnect Imports System.Threading Module Module1 Sub Main() ' mnClientConnected return value constants Const C_NOT_CONNECTED As Integer = 0 Const C_CONNECTED As Integer = 1 Const C_NO_SEND As Integer = 2 Const C_NO_RECV As Integer = 3 Const C_NO_SEND_RECV As Integer = 4 ' Connect to server Dim iReturn As Integer = 0 Console.WriteLine("DarkNet version: " + mn.GetVersion()) Dim RecvPacket As Int64 = mn.CreatePacket() Dim SendPacket As Int64 = mn.CreatePacket() mn.SetMemorySize(SendPacket, 1024) mn.Start(1, 0) mn.DisableUDP(0) mn.EnableGracefulDisconnect(0) iReturn = mn.Connect(0, "127.0.0.1", 6565, "", 0, 5, True) Select (iReturn) Case (1) Console.WriteLine("Connected to server") Case (0) Console.WriteLine("Connection timed out") Console.WriteLine("Press any key to exit...") Console.ReadKey() Return Case (-1) Console.WriteLine("An unknown error occurred whilst trying to connect") Console.WriteLine("Press any key to exit...") Console.ReadKey() Return Case (-2) Console.WriteLine("The connection request was rejected because the server is full") Console.WriteLine("Press any key to exit...") Console.ReadKey() Return End Select ' Main loop Dim SendTimer As Integer = 0 Dim SendFreq As Integer = 2000 Dim iClientStatus As Integer Do ' Use less CPU Thread.Sleep(1) iClientStatus = mn.ClientConnected(0, 0) Select Case (iClientStatus) ' Server has begun to gracefully disconnect us ' No further data can be received but we can use ' what has already been received, and is stored in ' the packet queue Case (C_NO_RECV) Console.WriteLine("mn.ClientConnected: " + iClientStatus.ToString()) ' Deal with data received and in packet queue While (mn.RecvTCP(0, RecvPacket, 0) > 0) Console.WriteLine("Final TCP data received") End While ' Send final data ' When mn.SendTCP returns data has been sent ' and so cleanup after this will NOT cause the ' send to fail mn.AddString(SendPacket, "Goodbye server, I will miss you!", 0, True) mn.SendTCP(0, SendPacket, 0, False, True) ' Shutdown ' No further data can be sent ' mn.ClientConnected will use mn.DisconnectClient/mn.Finish ' to clean up the instance and will return 0, completing ' the disconnect process mn.ShutdownClient(0, 0) Console.WriteLine("Received all data, sent final message to server and fully disconnected") End Select If (iClientStatus = C_CONNECTED) Then ' TCP packets iReturn = mn.RecvTCP(0, RecvPacket, 0) If iReturn > 0 Then Console.WriteLine("New TCP packet received") End If ' Send messages every SendFreq ms If (System.DateTime.Now.Millisecond + (System.DateTime.Now.Second * 1000) + (System.DateTime.Now.Minute * 60 * 1000)) - SendTimer > SendFreq Then ' Formulate packet Dim str As String = System.DateTime.Now.Date + ", " + System.DateTime.Now.TimeOfDay.ToString() mn.AddString(SendPacket, str, 0, True) Console.WriteLine("String sent to server: " + str) ' Send packet mn.SendTCP(0, SendPacket, 0, False, True) ' Update timer SendTimer = (System.DateTime.Now.Millisecond + (System.DateTime.Now.Second * 1000) + (System.DateTime.Now.Minute * 60 * 1000)) End If End If Loop Until (iClientStatus = C_NOT_CONNECTED) ' Cleanup and exit Console.WriteLine("Disconnected from server!") Console.WriteLine("Press any key to exit...") Console.ReadKey() mn.Finish(-1) End Sub End Module