IP kommunikation via k-net-lib

Anfängerfragen zum Programmieren mit PureBasic.
picard
Beiträge: 1
Registriert: 22.02.2007 10:18

IP kommunikation via k-net-lib

Beitrag von picard »

hallo in die runde,

habe mit purebasic gerade erst angefangen, da visual basic mir für mein projekt nicht geeignet erscheint. leider komme ich mit der syntax noch nicht so ganz klar :oops:

hier also mein problem: ich möchte einen client schreiben, der auf netzwerkrechnern läuft und auf die anfragen eines servers lauscht. was er erhält soll er dann auswerten bzw. ausführen. soweit so gut. habe also zunächst ein bsp. proggi der k-net-lib modifiziert bzw. besser gesagt "abgespeckt". leider läuft die client version nun gegen die wand, sobald der server ein packet sendet.

der fehler lautet: "ERROR, The memory pointer passed to free memory"

hier der code des client:
---

Code: Alles auswählen

IncludeFile "K-NetLib.pb"

#Window = 0

#Radio_Client = 0
#Radio_Server = 1
#Frame_Option = 2
#Frame_Info   = 3
#String_Name  = 4
#Text_Name    = 5
#String_Port  = 6
#Text_Port    = 7
#String_IP    = 8
#Text_IP      = 9
#Editor_History  = 10
#String_Msg      = 11
#Button_Start    = 12
#Button_Shutdown = 13
#Button_Send     = 14

#Server = 0
#Client = 1

#GUID = "{C4765326-F34E-4240-A862-F3175F182F45}"

Global ConnectionType.l
Global Name.s, PortNR.l, IP.s
Global CourierNew.l

Procedure History(msg.s)
   
   Define.s Tmp
   
   Tmp.s  = GetGadgetText(#Editor_History)
   Tmp.s = "------------------------------------------------------" + Chr(13) + Tmp.s
   Tmp.s = msg.s + Chr(13) + Tmp.s
   SetGadgetText(#Editor_History, Tmp.s)
   
EndProcedure

Procedure.s GetDescription(MsgID.l)
   
   Select MsgID.l
     
     Case #KNL_MSG_IndicateConnect:  Description.s = "Indicate Connect"
     Case #KNL_MSG_ConnectAborted: Description.s = "Connect Aborted"
     Case #KNL_MSG_ConnectComplete: Description.s = "Connect Complete"
     
     Case #KNL_MSG_CreatePlayer: Description.s = "Create Player"
     Case #KNL_MSG_DestroyPlayer: Description.s = "Destroy Player"
     
     Case #KNL_MSG_IndicateSend: Description.s = "Indicate Send"
     Case #KNL_MSG_Receive: Description.s = "Packet Received"
     Case #KNL_MSG_SendComplete: Description.s = "Send Complete"
     
     Case #KNL_MSG_ServerInfo: Description.s = "Server Info Changed"
     Case #KNL_MSG_PlayerInfo: Description.s = "Player Infor Changed"
     
     Case #KNL_MSG_CreateGroup: Description.s = "Group Created"
     Case #KNL_MSG_EnterGroup: Description.s = "Player Added to Group"
     Case #KNL_MSG_LeaveGroup: Description.s = "Player Removed from Group"
     Case #KNL_MSG_DestroyGroup: Description.s = "Group Destroyed"
     Case #KNL_MSG_GroupInfo: Description.s = "Group Info Changed"
     
     Case #KNL_MSG_HostMigrate: Description.s = "Host is Migrating"
     Case #KNL_MSG_TerminateSession: Description.s = "Session is Terminating"
     
     Case #KNL_MSG_Handshake: Description.s = "Handshake in Progress"
     Case #KNL_MSG_ConnectionInfo: Description.s = "Connection Info Request"
     Case #KNL_MSG_GameInfo: Description.s = "Game Info Request"
     
     Case #KNL_MSG_AcceptFile: Description.s = "Accept File?"
     Case #KNL_MSG_FileAccepted: Description.s = "File Accepted"
     Case #KNL_MSG_FileRefused: Description.s = "File Refused"
     Case #KNL_MSG_FileTransferComplete: Description.s = "File Transfer Complete"
     
     Case #KNL_MSG_TimerIgnited: Description.s = "Game Timer Ignited!"
     Case #KNL_MSG_TimerStopped: Description.s = "Game Timer Stopped!"
     
     Case #KNL_MSG_HostID: Description.s = "Host ID"
     
     Case #KNL_MSG_CAPS: Description.s = "Caps Structure Requested"
     Case #KNL_MSG_SPCAPS: Description.s = "SP Caps Structure Requested"
     
     Case #KNL_MSG_HostFound: Description.s = "A Host has been found"
     Case #KNL_MSG_Download: Description.s = "Download Completed!"
     
   EndSelect
   
   ProcedureReturn Description.s
   
EndProcedure

CourierNew = LoadFont(#PB_Any, "Courier New", 8)

If OpenWindow(#Window, 216, 0, 428, 190, "KNL PB Chat", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar)
   
   If CreateGadgetList(WindowID(#Window))
          
      EditorGadget(#Editor_History, 12, 10, 405, 171)
      SetGadgetText(#Editor_History, "History. New items will be added at the top...")
      SetGadgetFont(#Editor_History, FontID(CourierNew))
      
   EndIf
   
EndIf

KNL_ErrorOption(1)

History("Initializing. Please wait...")

If KNL_Initialize(#GUID, #KNL_Service_TCPIP, #KNL_INI_Peer2Peer + #KNL_INI_Server)
   
   History("KNL Initialized for hosting a session...")
   
   If KNL_CreateHost(1001, "127.0.0.1","","",0)
      History("Host Created OK!")
      History("Waiting for clients to connect.")
   EndIf

EndIf

lFirst.l = 0

Repeat

   E = WindowEvent()
   
   Delay(50)

   If E = #PB_Event_CloseWindow
      Break
   EndIf
   
   While KNL_CheckErrors()
     History(KNL_GetErrorText())
     KNL_ClearError()
   Wend
   
        While KNL_CheckMessages()
;          
   If lFirst = 0
       lFirst = 1
   Else
;          ;ID = KNL_GetMessageID() ; Get the message ID
          Fields = KNL_MessageFields() ; Get the number of fields the message contains.
          History( Str( Fields ) )
;          
          Msg.s = "Message Received - ID: " + Str(ID) + Chr(13)
          Msg.s = Msg.s + "Description: " + GetDescription(ID) + Chr(13)
          Msg.s = Str(Fields)
          
          For I = 0 To Fields - 1
;              
;              ; KNL_GetString will automatically convert
;              ; any field content to a string and return
;              ; it to us:
;              
              Msg.s = Msg.s + Chr(13) + KNL_FieldName(I) + " = " + KNL_GetString(I) 
              ; Msg.s = Msg.s + KNL_GetString(I) 
          Next
;          
          History( cMsg.s)
;             
    EndIf
          KNL_ClearMessage()  
        Wend
ForEver

KNL_Shutdown()

CloseLibrary(KNL)
---

würde mich über ein paar tipps freuen und hoffe bald besser durch pb zu steigen ... danke schon mal und beste grüsse
markus
<)

code-tags gesetzt - Kaeru fecit
nächstes mal bitte selber dran denken.
...und welcome aboard...