Closing the connection server

Just starting out? Need help? Post your questions and find answers here.
User_Russian
Addict
Addict
Posts: 1161
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Closing the connection server

Post by User_Russian »

As an Internet client (connection open function OpenNetworkConnection ()), may determine that the connection was broken server?
I have used this method:

Code: Select all

DisableDebugger
Test=0
SendNetworkData(Connect, @Test, 0)
Err = WSAGetLastError_()
EnableDebugger
If Err
  *Mem = AllocateMemory(1024)
    If *Mem
      FillMemory(*Mem, 1024, 0)
      Test=FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, Err, 0, *Mem, 1024, 0)
      If Err>0
        ErrorText.s = PeekS(*Mem, Test)
      EndIf
      FreeMemory(*Mem)
    EndIf
 EndIf
It works, but only for TCP connections.
And if you use UDP, then not working properly.

How to verify that the server connection is broken?
User avatar
RichAlgeni
Addict
Addict
Posts: 811
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Closing the connection server

Post by RichAlgeni »

Try using the windows API function ioctlsocket. If there is no error, length will return the number of bytes available to be read.

Code: Select all

Define length
Define result
Define socketHandle.i

; make sure the socket is still valid

socketHandle = ConnectionID(Connect)
result = ioctlsocket_(socketHandle, #FIONREAD, @length)

If result < 0; socket error
    ProcedureReturn result
EndIf
User avatar
kryptonn
User
User
Posts: 47
Joined: Wed Apr 18, 2007 7:23 pm

Re: Closing the connection server

Post by kryptonn »

RichAlgeni wrote:Try using the windows API function ioctlsocket.
I known that User_Russian did not pay for PureBasic, uses the pirate copy, and spreads the pirate copies itself. He has not a moral right to get no support.

http://www.purebasic.fr/english/viewtop ... 02#p367802
AndyMK
Enthusiast
Enthusiast
Posts: 420
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: Closing the connection server

Post by AndyMK »

User_Russian wrote:As an Internet client (connection open function OpenNetworkConnection ()), may determine that the connection was broken server?
I have used this method:

Code: Select all

DisableDebugger
Test=0
SendNetworkData(Connect, @Test, 0)
Err = WSAGetLastError_()
EnableDebugger
If Err
  *Mem = AllocateMemory(1024)
    If *Mem
      FillMemory(*Mem, 1024, 0)
      Test=FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, Err, 0, *Mem, 1024, 0)
      If Err>0
        ErrorText.s = PeekS(*Mem, Test)
      EndIf
      FreeMemory(*Mem)
    EndIf
 EndIf
It works, but only for TCP connections.
And if you use UDP, then not working properly.

How to verify that the server connection is broken?
You should google UDP and learn as much as you can about it before posting such questions. UDP is a connectionless protocol. When you send a packet of data, you have no way of knowing if the packet got there unless you program that functionality in to it. Detecting a disconnect over UDP is something you have to program yourself.
Post Reply