I've successfully made a TCP connection; Now what?

Started by Yellownakji, March 28, 2019, 01:19:01

Previous topic - Next topic

Yellownakji

My socket is connected via TCP/IP.   How can i manage/report if it's still connected?  How do i know if it disconnects?

I also noticed that when i connect my socket to a host, it freezes the whole application for a bit before returning results.   Is this normal?  Can i have my application continue normally without this freeze?.. maybe make it a background process?

I'm new to networking.

therevills

Can you show us your code?

With the freezing it sounds like you should be doing your connection asynchronously.

Yellownakji

Quote from: therevills on March 28, 2019, 02:35:35
Can you show us your code?

With the freezing it sounds like you should be doing your connection asynchronously.


here's my little prototype:


Graphics(640, 480, 0, 60, GRAPHICS_BACKBUFFER)
SetBlend(ALPHABLEND)
SetColor(255, 255, 255) 'White


'var
Global app_end:Byte = False

Global SocketInfo:TAddrInfo
Global Socket:TSocket
Global CreatedSocket:Byte = False
Global Host:String
Global Port:Int
Global DreamPiInfo:TAddrInfo[]
Global Configured:Byte = False
Global ConnectionStatus:Byte = False 'If the dreamPi is online or not.
Global Do_Timer:Byte = False
Global Timer:Int = 400
Global Retries:Int = 0



Function CreateSocket()
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SocketInfo = New TAddrInfo(AF_INET_, SOCK_STREAM_) 'IPV4 Socket Stream
Socket = TSocket.Create(SocketInfo) 'Create a socket wih the SocketInfo parameters

If Not Socket
CreatedSocket = False
app_end = True
Print(String("Quit: Can't create socket"))
Else
CreatedSocket = True
Print(String("Created socket."))
End If

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
End Function

Function ConfigureDreampi()
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Host = "192.168.1.99"
Port = 1998
DreamPiInfo = AddrInfo(Host, Port, SocketInfo)

If Not DreamPiInfo
Configured = False
Print(String("Hostname not resolved."))
Else
Configured = True
Print(String("Hostname Resolved."))
Print(String("Attempting to find DreamPi.."))
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
End Function

Function FindDreampi()
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Retries = Retries + 1


If Not Socket.Connect(DreamPiInfo[0])
Print(String("DreamPi offline"))
ConnectionStatus = False
Do_Timer = True
Else
Print(String("DreamPi online @ " + Socket.RemoteIp))
ConnectionStatus = True
Do_Timer = True
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
End Function





While Not app_end = True
Cls

If CreatedSocket = False
CreateSocket() 'Try to create a socket
Else
If Configured = False
ConfigureDreampi() 'Try to configure dreampi
Else
'-----------------------------------------------------
If Do_Timer = False
Timer = 400
FindDreampi()
Else

Timer = Timer - 1
If Timer <= 0
Do_Timer = False
Print(String("Re-Checking... (#" + Retries + ")"))
Socket.close()
CreatedSocket = False
Configured = False
End If

End If
'-----------------------------------------------------
End If
End If

If ConnectionStatus = True
SetColor(0, 255, 0)
DrawRect(0, 0, 640, 480)
Else
SetColor(255, 0, 0)
DrawRect(0, 0, 640, 480)
End If

Flip
WEnd

Derron

#3
- use threads
- use optionally confirming UDP instead of TCP as the confirmation overhead of TCP can create delays

Edit (now on my computer):
TCP connections confirm each packet which increases the time a data package needs. If the library doesn't do this in a thread then it means it waits until it receives the verification - or a timeout.
In BlitzMax you could put everything in a thread which is not connected to rendering something (graphics). So just create a thread in which you do your stuff. And if just this waiting time annoys you, you could send the incoming data via a callback to your "main thread"/main app.

With callback I mean something like a function "OnNetworkDataReceived(data:byte[])" or so. Do not forget to use a "LockMutex" before calling "OnNetworkDataReceived" from within your thread (and "UnlockMutex" afterwards). This is needed as another thread/line of code might alter stuff you change within that function (so you could even lock/unlock your mutex right around the variable you change in this function). To avoid concurrent modifications of a variable you do this mutex locks.



Bye
Ron