` 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 ` Connect to server iReturn as integer = 0 print "DarkNet version: " + mn Get Version() RecvPacket = mn Create Packet() SendPacket = mn Create Packet() mn Set Memory Size SendPacket,1024 mn Start 1,0 mn Disable UDP 0 mn Enable Graceful Disconnect 0 iReturn = mn Connect(0,"127.0.0.1",6565,"",0,5,1) select iReturn case 1: print "Connected to server" endcase case 0: print "Connection timed out" print "Press any key to exit..." end endcase case -1: print "An unknown error occurred whilst trying to connect" print "Press any key to exit..." end endcase case -2: print "The connection request was rejected because the server is full" print "Press any key to exit..." end endcase endselect ` Main loop SendTimer as dword SendFreq as dword SendTimer = 0 SendFreq = 2000 iClientStatus as integer repeat iClientStatus = mn Client Connected(0,0) select 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 3: print "mn ClientConnected: " + str$(iClientStatus) ` Deal with data received and in packet queue while mn Recv TCP(0,RecvPacket,0) > 0 print "Final TCP data received" endwhile ` Send final data ` When mn SendTCP returns data has been sent ` and so cleanup after this will NOT cause the ` send to fail mn Add String SendPacket,"Goodbye server, I will miss you!",0,1 mn Send TCP 0,SendPacket,0,0,1 ` 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 Shutdown Client 0,0 print "Received all data, sent final message to server and fully disconnected" endcase endselect if iClientStatus = 1 ` TCP packets iReturn = mn Recv TCP(0,RecvPacket,0) if iReturn > 0 print "New TCP packet received" endif ` Send messages every SendFreq ms if timer() - SendTimer > SendFreq ` Formulate packet string$ = get date$() + ", " + get time$() mn Add String SendPacket,string$,0,1 print "String sent to server: " + string$ ` Send packet mn Send TCP 0,SendPacket,0,0,1 ` Update timer SendTimer = timer() endif endif until iClientStatus = 0 ` Cleanup and exit print print print "Disconnected from server!" print "Press any key to exit..." wait key mn Finish -1 end