` 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 ` Setup server iReturn as integer Print "DarkNet version: " + mn Get Version() RecvPacket = mn Create Packet() SendPacket = mn Create Packet() mn Set Memory Size SendPacket,1024 mn Set Used Size SendPacket,1024 mn Start 1,0 mn Disable UDP 0 mn Set Local 0,"127.0.0.1",6565,"",0 mn Enable Graceful Disconnect 0 ` Enable graceful disconnect mn Start Server 0,50,0,0 Print "Server started on TCP port " + str$(mn Get Local Port TCP(0)) ` Send data to clients every SendFreq ms SendTimer as dword SendFreq as dword SendTimer = 0 SendFreq = 100 ` Disconnect a client every DisconnectFreq ms DisconnectTimer as dword DisconnectFreq as dword DisconnectTimer = 0 DisconnectFreq = 3000 ` Main loop do ` Graceful close for cl = 1 to mn Get Max Clients(0) iClientStatus = mn Client Connected(0,cl) select iClientStatus ` Shutdown client ` Further send operations to this client will fail silently ` mnClientConnected will now return 2 case 1: if timer()-DisconnectTimer > DisconnectFreq mn Shutdown Client 0,cl Print "Shut down client " + str$(cl) DisconnectTimer = timer() endif endcase ` Client is sending us data still ` We will wait forever, but a robust server should ` only wait for a set amount of time, and forcefully ` disconnect clients that take too long case 2: ` Nothing to do while we wait endcase ` Client has finished sending us data ` We must get it from packet queue ` Once packet queue is empty, client ` will be disconnected by mnClientConnected ` and mnClientConnected will return 0 case 4: Print "mnClientConnected (" + str$(cl) + "): " + str$(iClientStatus) while mn Recv TCP(0,RecvPacket,cl) > 0 string$ = mn Get String(RecvPacket,0,1) Print "Final string received from client " + str$(cl) + ": " + string$ endwhile endcase endselect next cl ` New clients iReturn = mn Client Joined(0) if iReturn > 0 Print "New client joined, ID: " + str$(iReturn) DisconnectTimer = timer() endif ` Leaving clients iReturn = mn Client Left(0) if iReturn > 0 Print "Client left, ID: " + str$(iReturn) endif ` Receive data for cl = 1 to mn Get Max Clients(0) if mn Client Connected(0,cl) = 1 ` TCP packets iReturn = mn Recv TCP(0,RecvPacket,cl) if iReturn > 0 string$ = mn Get String(RecvPacket,0,1) Print "String received from client " + str$(cl) + ": " + string$ endif endif next cl ` Send data if timer() - SendTimer > SendFreq for cl = 1 to mn Get Max Clients(0) if mn Client Connected(0,cl) = 1 mn Send TCP 0,SendPacket,cl,1,1 Print "Packet sent" endif next cl SendTimer = timer() endif loop