Buchempfehlung
Windows System Programming
Windows System Programming
Das Kompendium liefert viele interessante Informationen zur Windows-Programmierung auf Englisch. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

Client/Server/AskIP....TSNETest

Uploader:MitgliedMuttonhead
Datum/Zeit:09.08.2013 07:52:24

'simpleNetwork.bas
'hier ist einiges in bester C&P Tradition zusammengetragen worden:
'aus originalen Beispiel zu TSNE von ThePuppetMaster
'http://www.freebasic-portal.de/porticula/testgetwanipa-bas-1229.html
'und Beispielen von MOD
'http://www.freebasic-portal.de/porticula/tsne-code-client-1124.html


#include once "TSNE_V3.bi"

'subs für TSNE-Kommunikation
declare sub TSNE_Disconnected(byval V_TSNEID as uinteger)
declare sub TSNE_Connected(byval V_TSNEID as uinteger)
declare Sub TSNE_ConnectedAskForIP(ByVal V_TSNEID as UInteger)
declare sub TSNE_NewData(byval V_TSNEID as uinteger, byref V_Data as string)
declare sub TSNE_NewConnection(byval V_TSNEID as uinteger, byval V_RequestID as Socket, byval V_IPA as string)
declare sub TSNE_NewConnectionCanceled(byval V_TSNEID as uinteger, byval V_IPA as string)

dim shared as integer UseEndTag=1 'benutze Endtag als Messageende
dim shared as string EndTag
EndTag="[END]"                    'mit diesem Tag enden alle Netzwerkmessages


type SimpleNetwork
  public:
  Handle as uinteger      'TSNE Handle für die Netzwerkverbindung
  remote as uinteger      'TSNE ID der Gegenstelle
  IP as string            'IP welche von TSNE benutzt wird
  GuruCode as integer     'von TSNE verwalteter Fehlercode

  private:
  access_ReceivedData as any ptr 'Mutex Handle für Zugriff auf eingegangene Daten
  ReceivedData as string         'eingegangene Daten
  ReceivedDataPing as integer    'vollständige Daten eingegangen, nur bei UseEndTag=1

  access_status as any ptr'Mutex Handle für Zugriff auf Statusdaten der Netzwerkverbindung
  StatusText as string    'Statusmeldung allgemein
  connected as integer    'Verbindungsstatus 0=keine Verbindung, 1=Verbindung besteht

  public:
  declare constructor
  declare destructor

  declare sub SetRemote (r as uinteger)
  declare sub SetIP (ips as string)
  declare function GetIP as string

  declare sub SetStatusText (t as string)
  declare function GetStatusText as string
  declare function StatusTextPing as integer
  declare sub SetConnectionStatus (c as integer)
  declare function GetConnectionStatus as integer
  declare sub SetGuruCode (dc as integer)
  declare function GetGuruCode as integer

  declare sub SetReceivedData (vd as string)'"Verbindung" zwischen TSNE und dem SimpleNetwork bei eingehenden Daten
  declare sub SendMessage(m as string)
  declare function GetMessage as string
  declare function MessagePing as integer

  declare function CreateServer as integer
  declare function CreateClient as integer
  declare function AskForCurrentIP as string
  declare sub KillSimpleNetwork
end type



constructor SimpleNetwork
  remote=0
  IP="127.0.0.1"
  access_ReceivedData= mutexcreate
  access_status= mutexcreate
end constructor



destructor SimpleNetwork
  KillSimpleNetwork
end destructor



sub SimpleNetwork.SetIP (ips as string)
  IP=ips
end sub



function SimpleNetwork.GetIP as string
  function=IP
end function



sub SimpleNetwork.SetRemote (r as uinteger)
  remote=r
end sub



sub SimpleNetwork.SetStatusText (t as string)
  mutexlock access_status
    StatusText &=t
  mutexunlock access_status
end sub



function SimpleNetwork.GetStatusText as string
  mutexlock access_status
    function=StatusText
    StatusText=""
  mutexunlock access_status
end function


function SimpleNetwork.StatusTextPing as integer
  function=0
  mutexlock access_status
    if StatusText <> "" then function=1
  mutexunlock access_status
end function


sub SimpleNetwork.SetReceivedData (vd as string)
  mutexlock access_ReceivedData
    ReceivedData +=vd
  mutexunlock access_ReceivedData
end sub



function SimpleNetwork.GetMessage as string
  mutexlock access_ReceivedData
    function=ReceivedData
    ReceivedData=""
  mutexunlock access_ReceivedData
end function



sub SimpleNetwork.SendMessage(m as string)
  dim as string msg=m
  if UseEndTag then msg &=EndTag
  TSNE_Data_Send(remote, msg)
end sub



function SimpleNetwork.MessagePing as integer
  function=0
  mutexlock access_ReceivedData
    if UseEndTag then
      if right(ReceivedData,len(EndTag))=EndTag then function=1
    else
      if ReceivedData<>"" then function=1
    end if

  mutexunlock access_ReceivedData
end function



sub SimpleNetwork.SetConnectionStatus (c as integer)
  mutexlock access_status
    connected=c
  mutexunlock access_status
end sub



function SimpleNetwork.GetConnectionStatus as integer
  mutexlock access_status
    function=connected
  mutexunlock access_status
end function



function SimpleNetwork.CreateServer as integer
  function=0
  GuruCode = TSNE_Create_Server(Handle,9000, 1, @TSNE_NewConnection, @TSNE_NewConnectionCanceled)    'Server erstellen
  if GuruCode = TSNE_Const_NoError then
    SetStatusText "Server ready..."
    function=1
  else
    SetStatusText "Error: " & GuruCode
  end if
end function



function SimpleNetwork.CreateClient as integer
    function=0
   GuruCode = TSNE_Create_Client(Handle, IP,9000, @TSNE_Disconnected, @TSNE_Connected, @TSNE_NewData,120)
    if GuruCode = TSNE_Const_NoError then
      SetStatusText  "Searching Server"
      function=1
    else
      SetStatusText  "Error: " & GuruCode
    endif
end function



'niemals gleichzeitig mit bereits existierenden Servern/Clienten aufrufen
function SimpleNetwork.AskForCurrentIP as string
  function=""
  GuruCode = TSNE_Create_Client(Handle, "checkip.dyndns.org", 80, 0, @TSNE_Connected, @TSNE_NewData, 60)
  If GuruCode <> TSNE_Const_NoError Then
    Print "[FEHLER] " & TSNE_GetGURUCode(GuruCode)      'Fehler ausgeben
  End If
  TSNE_WaitClose(Handle)

  function=GetMessage
end function



sub SimpleNetwork.KillSimpleNetwork
  TSNE_Disconnect(Handle)
  TSNE_WaitClose(Handle)
  mutexdestroy access_ReceivedData
  mutexdestroy access_status
end sub

'******************************************************************************
dim shared as SimpleNetwork SN'************************************************
'******************************************************************************

'TSNE CallBacks****************************************************************
sub TSNE_Disconnected(byval V_TSNEID as uinteger)
  SN.SetConnectionStatus 0
  SN.SetStatusText("Disconnected")
end sub

sub TSNE_Connected(byval V_TSNEID as uinteger)
  SN.SetStatusText("Connected")
  SN.SetRemote(V_TSNEID)
  SN.SetConnectionStatus 1
end sub

Sub TSNE_ConnectedAskForIP(ByVal V_TSNEID as UInteger)
  'Anfrage vorbereiten
  Dim D as String
  D = "GET / HTTP/1.0" & Chr(13, 10)
  D += "Host: checkip.dyndns.org" & Chr(13, 10)
  D += "User-Agent: TSNE_V3_DyndnsUpDateClient" & Chr(13, 10)
  D += "connection: close" & Chr(13, 10)
  D += Chr(13, 10)
  'Anfrage and den Server senden
  Dim BV as Integer = TSNE_Data_Send(V_TSNEID, D)
  If BV <> TSNE_Const_NoError Then
    Print "[FEHLER] " & TSNE_GetGURUCode(BV)      'Fehler ausgeben
    TSNE_Disconnect(V_TSNEID)
  End If
End Sub

sub TSNE_NewConnection(byval V_TSNEID as uinteger, byval V_RequestID as Socket, byval V_IPA as string)
  SN.SetStatusText("Client attempts to connect")
  Dim XNeSNID as uinteger
  Dim XReturnIPA as string
  TSNE_Create_Accept(V_RequestID, XNeSNID, XReturnIPA, @TSNE_Disconnected, @TSNE_Connected, @TSNE_NewData)
end sub

sub TSNE_NewConnectionCanceled(byval V_TSNEID as uinteger, byval V_IPA as string)
  SN.SetStatusText("Connection refused")
end sub

sub TSNE_NewData(byval V_TSNEID as uinteger, byref V_Data as string)
  print V_Data
  SN.SetReceivedData (V_Data)
end sub