none
IOTCPCLIENT Help RRS feed

  • Question

  • My Client In IOTCP will not accept messages. I cannot figure out why. I think the event handler is not actually going to the Message Received Sub. 

    Also how do I "verify" my account?

    For Debugging Purposes

    My Server Code (Part 1) :  http://smallbasic.com/program/?GQT657

    Lines 12,21-26,157-162,178,184,193,208,222,233,267,310,314,338,344-347, and 354 have been auto commented.  Please uncomment them. 

    Part2: http://smallbasic.com/program/?KWG236

    Lines 19 & 22 have been auto commented out.

    Part3: http://smallbasic.com/program/?BJJ539

    Lines 28,30,32 have been auto commented out. 

    Client in Question (In Active Development) : http://smallbasic.com/program/?WJT633

    Code as requested:

    Client in Question:

    startup = 0
    Advanced()
    'Main()
    
    Sub Advanced
      IOTCPClient.OnMessageRecieved = MsgRe
      IOTCPClient.OnServerDisconnect = ServerDisconnect
      GraphicsWindow.Show()
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "Blue"
      GraphicsWindow.Title = "TCP Chat Client"
      
      GraphicsWindow.Height = 200 
      GraphicsWindow.Width = 300
      
      IPBox = Controls.AddTextBox(75,1)
      NickBox = Controls.AddTextBox(75,40)
      
      GraphicsWindow.BrushColor = "White"
      GraphicsWindow.DrawBoundText(1,1,1000,"IP Adress :")
      GraphicsWindow.DrawBoundText(1,40,1000,"Nick Name :")
      GraphicsWindow.BrushColor = "Black"
      If startup = 1 Then
        Button1 = Controls.AddButton("Go!",230,100)
      Else
        Button2 = Controls.AddButton("Set",230,100)
      EndIf
      Controls.ButtonClicked = AdvancedHandler
    EndSub
    
    Sub AdvancedHandler
      IOTCPClient.OnMessageRecieved = MsgRe
      IOTCPClient.OnServerDisconnect = ServerDisconnect
      IP = Controls.GetTextBoxText(IPBox)
      NickName = Controls.GetTextBoxText(NickBox)
      
      If IP = "" Or NickName = "" Then
        GraphicsWindow.ShowMessage("Null Value Encountored. Please fill in all fields.","Null Error")
        Advanced()
      EndIf
      
      IPValid = NetworkPlus.IsIPAddress(IP)
      If IPValid = "True" Then
        startup = 1
        Connect()
        Main()
      Else
        GraphicsWindow.ShowMessage("Bad Value: IP Encountored.","Bad IP")
        Advanced()
      EndIf
    EndSub
    
    Sub Main
      IOTCPClient.OnMessageRecieved = MsgRe
      IOTCPClient.OnServerDisconnect = ServerDisconnect
      GraphicsWindow.Show()
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "Blue"
      GraphicsWindow.Title = "TCP Chat Client"
      GraphicsWindow.Width = 800
      GraphicsWindow.Height =450
      
      GraphicsWindow.BrushColor = "Black"
      
      GraphicsWindow.DrawBoundText(10,36,100,"Chat Log:")
      Chatlog = Controls.AddMultiLineTextBox(10,50)
      Controls.SetSize(Chatlog,700,100)
      
      GraphicsWindow.DrawBoundText(10,180,1000,"Type your message here:")
      MsgBox = Controls.AddMultiLineTextBox(10,200)
      Controls.SetSize(MsgBox,700,100)
      
      GraphicsWindow.BrushColor = "Black"
      SendButton =  Controls.AddButton("Send",670,310)
      ConnectButton = Controls.AddButton("Connect",720,160)
      IOTCPClient.OnMessageRecieved = MsgRe
      IOTCPClient.OnServerDisconnect = ServerDisconnect
      Controls.ButtonClicked = MainHandler
    EndSub
    
    Sub MainHandler
      Buttonclickedcaption =  Controls.GetButtonCaption(Controls.LastClickedButton)
      IOTCPClient.OnMessageRecieved = MsgRe
      IOTCPClient.OnServerDisconnect = ServerDisconnect
      If Buttonclickedcaption = "Connect" Then
        Connect()
        Main()
      ElseIf Buttonclickedcaption = "Send" then 
        Msg = Controls.GetTextBoxText(MsgBox)
        SendMsg()
        Main()
      EndIf
    EndSub
    
    Sub Connect
      IOTCPClient.Connect(IP,1000)
    EndSub
    
    Sub Disconnect
      
    EndSub  
    
    Sub SendMsg
      IOTCPClient.SendMessage(Msg)
    EndSub
    
    Sub MsgRe
      GraphicsWindow.ShowMessage("Hi","")
      MsgR = IOTCPClient.LastMessageData 
      Msg = "Message Recieved"
      SendMsg()
      Msg = ""
      Controls.SetTextBoxText(Chatlog,Text.Append(Controls.GetTextBoxText(Chatlog),MsgR))
    EndSub
    
    Sub ServerDisconnect
      TextWindow.WriteLine("Error")
    EndSub

    Server Code:

    TextWindow.Clear()
    
    debug = 0
    TextWindow.Title = "TCP Server v 0.2"
    top = Desktop.Height
    left = Desktop.Width 
    
    TextWindow.Top = 500
    TextWindow.Left = 1000
    
    File.CreateDirectory(Program.Directory + "\" +"Log")
    mpath = Program.Directory + "\" +"Log" +"\" 'master path
    path =   mpath + "Log.txt"
    path2 =  mpath + "Blacklist.txt"
    ippath = mpath + "Clients.txt"
    ipclient = Program.Directory + "\" +"IP.exe"
    
    cmd = "C:\Windows\System32\cmd.exe"
    
    File.DeleteFile(path)
    File.DeleteFile(path2)
    File.DeleteFile(ippath)
    
    authtiacte = 1 'Auto Authentiactes user
    PassswordGet()
    authtiacte = 0 'De authenticates user
    last = 0
    
    serverclient = Program.Directory + "\" + "TCPServerClient.exe" ' Server Client Path
    LDProcess.Start(serverclient,Password) 'Passes Password to Server Client & starts it
    
    TextWindow.Write("Initializing server on " + IOTCPServer.IP + ", port 1000...")
    
    Sub PassswordGet 'Set Password for Server ; Null = 0
      If authtiacte = 1 Then
        TextWindow.WriteLine("")
        TextWindow.WriteLine("Please Press Alt + F7 after finishing this prompt.")
        TextWindow.Write("Set Password for Server : ")
        password = TextWindow.Read()
        TextWindow.Write("Please Press Alt + F7.")
        TextWindow.Read()
        If Password = "" Then
          Password = 0
        EndIf
      EndIf
      TextWindow.Clear()
    EndSub
    
    If IOTCPServer.StartServer(1000) = "SUCCESS" Then
      TextWindow.ForegroundColor = "Green"
      TextWindow.CursorLeft = 76
      TextWindow.Title = "TCP Server v0.2 [OK] " + IOTCPServer.IP
      TextWindow.Write("[OK]")
    Else
      TextWindow.ForegroundColor = "Red"
      TextWindow.CursorLeft = 74
      TextWindow.Title = "TCP Server v0.2 [FAIL]"
      TextWindow.Write("[FAIL]")
    EndIf
    
    main()
    Sub main 'Main Menu
      TextWindow.ForegroundColor = "Green"
      IOTCPServer.OnClientConnect = OnClientConnect
      IOTCPServer.OnMessageRecieved = OnMessageRecieved
      IOTCPServer.OnClientDisconnect = OnClientDisconnect
      TextWindow.WriteLine("")
      TextWindow.WriteLine("                 ╔═══════════════════════════════════════════╗")
      TextWindow.WriteLine("                 ║ 1. Boot                                   ║")
      TextWindow.WriteLine("                 ║ 2. Brodcast Message                       ║")
      TextWindow.WriteLine("                 ║ 3. Send Message                           ║")
      TextWindow.WriteLine("                 ║ 4. Clients                                ║")
      TextWindow.WriteLine("                 ║ 5. Clear Screen                           ║")
      TextWindow.WriteLine("                 ║ 6. Change Password                        ║")
      TextWindow.WriteLine("                 ║ 7. Shutdown Sever                         ║")
      TextWindow.WriteLine("                 ╚═══════════════════════════════════════════╝")
      Start()
    EndSub
    
    Sub Start 'Options
      IOTCPServer.OnClientConnect = OnClientConnect
      IOTCPServer.OnMessageRecieved = OnMessageRecieved
      IOTCPServer.OnClientDisconnect = OnClientDisconnect
      TextWindow.ForegroundColor = "Gray"
      TextWindow.Write("Option : ")
      option = TextWindow.ReadNumber()
      
      If option = 1 then 
        TextWindow.Clear()
        TextWindow.Write("IP:")
        IPUser = TextWindow.Read()
        failure  = IOTCPServer.SendMessage("***BOOT***",IPUser)
        failure2 = IOTCPServer.DisconnectClient(IPUser)
        TextWindow.WriteLine(failure + ";"+ failure2)
        
        emsg = LDEncryption.AESEncrypt("Server Boot : " + IPUser + ";" + failure+ ";" + failure2,Password)
        
        filemsg = emsg
        filecolor = 4
        writefile()
        
        Blacklistprocess()
        main()
      ElseIf option = 2 Then
        msguser = ""
        Broadcast()
        msguser = ""
        Broadcastflag = 0
        Start()
      ElseIf option = 3 then 'Send Message
        TextWindow.Clear()
        TextWindow.Write("IP:")
        IPUser = TextWindow.Read()
        TextWindow.Write("Message:")
        msguser = TextWindow.Read()
        IOTCPServer.SendMessage("Server:",IPUser)
        failure = IOTCPServer.SendMessage(msguser ,IPUser)
        TextWindow.WriteLine(failure)
        
        emsg = LDEncryption.AESEncrypt("Server  Message to " + IPUser + ";" + msguser + ";" + failure,Password)
        
        filemsg = emsg
        filecolor = 4
        writefile()
        
        Start()
      Elseif option = 4 then 'Clients
        arraedip = IOTCPServer.GetClients()
        index1 =  Text.GetIndexOf(arraedip,"=")
        index2 = Text.GetIndexOf(arraedip,";")
        emsg =   LDEncryption.AESEncrypt("Clients: "+arraedip,Password)
        
        filemsg = emsg
        filecolor = 5
        writefile()
        
        Start()
      ElseIf option = 5 then 'Clear Screen
        TextWindow.Clear()
        main()
      elseIf option = 6 then 'Change Password
        UserAuthenticate()
        If authtiacte = 1 then  
          PassswordGet()
          LDProcess.Start(serverclient,Password)
        EndIf
        main()
      ElseIf option = 7 then 'Shutdown Server
        UserAuthenticate()
        If authtiacte = 1 then  
          LDProcess.Start(cmd,"cmd /k Taskkill /f /im TCPServerClient.exe")
          LDProcess.Start(cmd,"cmd /k Taskkill /f /im cmd.exe")
          File.DeleteFile(path)
          File.DeleteFile(path2)
          File.DeleteFile(ippath)
          TextWindow.WriteLine("Sending Brodcast")
          msguser = "Server Shutdown Started. All Clients will be booted."
          Broadcast()    
          IOTCPServer.StopServer()
          Program.End()
        else
          main()
        EndIf
      else  
        Start()
      endIf 
    EndSub
    
    Sub OnClientConnect 'When A client connects to the server
      File.WriteLine(path,2,"1")
      a = IOTCPServer.LastClientConnectIP
      b = IOTCPServer.LastClientConnectPort
      
      blacklist = 0
      x = File.ReadLine(path2,1)
      num = 1
      While x <> "" 'Checks File to see if client is blacklisted
        If x = a Then
          blacklist = 1
          x = ""
        ElseIf x <> "" then
          num = num + 1
          x = File.ReadLine(path2,num)
        EndIf
      EndWhile
      
      If blacklist = 1 Then 'Blacklisted Client
        IOTCPServer.SendMessage("***BOOTL***",a)
        IOTCPServer.DisconnectClient(a)
      Else 'Non Blacklisted Client
        failure = IOTCPServer.SendMessage("You are now connected to the server.",a)
        emsg = LDEncryption.AESEncrypt("New connection: " + a+ ", port " + b,Password)
        
        filemsg = emsg
        filecolor = 1
        writefile()
        File.DeleteFile(ippath)
        LDProcess.Start(ipclient,IOTCPServer.GetClients())
      EndIf
    EndSub
    
    Sub OnClientDisconnect
      a = IOTCPServer.LastClientDisconnectIP
      While x <> "" 'BLacklist Process
        If x = a Then
          blacklist = 1
          x = ""
        elseIf x <> "" then
          num = num + 1
          x = File.ReadLine(path2,num)
        EndIf
        
      EndWhile 'If disconnect wasnt caused by being Blacklisted
      If blacklist <> 1 Then
        emsg = LDEncryption.AESEncrypt(a + " has disconnected",Password)
        filemsg = emsg
        filecolor = 2
        writefile()
        
        File.DeleteFile(ippath)
        LDProcess.Start(ipclient,IOTCPServer.GetClients())
      EndIf 
    EndSub
    
    Sub OnMessageRecieved
      a = IOTCPServer.LastMessageIP
      b = IOTCPServer.LastMessageData
      msgl = Text.GetSubText(b,1,1)
      
      If msgl = "*" Then
        msguser = a + ":" + Text.GetSubTextToEnd(b,2)
        Broadcast()
        a = msguser
        msguser = ""  
      Else
        emsg = LDEncryption.AESEncrypt("New message from: " + a + ": " + b ,Password)
        
        filemsg = emsg
        filecolor = 3
        writefile()
      EndIf
    EndSub
    '1 = Green ; 2 = Red ; 3 = Yellow
    
    Sub Blacklistprocess 'Adds an IP to Blacklist File
      TextWindow.WriteLine("Blacklist?")
      TextWindow.WriteLine("1.Y")
      TextWindow.WriteLine("2.N")
      TextWindow.Write("Option:")
      options = TextWindow.ReadNumber()
      
      If options = 1 then
        File.AppendContents(path2,IPUser)
      EndIf
    EndSub
    
    Sub UserAuthenticate
      TextWindow.Clear()
      TextWindow.WriteLine("User Authentication:")
      TextWindow.Write("Password of Server:")
      temppassword = TextWindow.Read()
      authtiacte = 0
      TextWindow.Clear()
      If temppassword = "" Then
        temppassword = 0
      EndIf
      
      If temppassword = Password Then
        TextWindow.WriteLine("User Authenitacted")
        authtiacte = 1
        temppassword = ""
      Else
        TextWindow.WriteLine("Wrong Password")
        authtiacte = 0
        temppassword = ""
      EndIf
    EndSub
    
    Sub Broadcast 'Gets Clients connected to the server and sends them a mass message
      clientlist = mpath + "Clients.txt"
      
      If msguser = "" Then
        TextWindow.Write("Message : " )
        msguser = TextWindow.Read()
        Broadcastflag = 1
      EndIf
      
      LDProcess.Start(ipclient,IOTCPServer.GetClients())
      emsg = LDEncryption.AESEncrypt("Brodcast Message : " + msguser,Password)
      filemsg = emsg
      filecolor = 3
      writefile()
      
      If debug = 1 Then
        TextWindow.WriteLine(File.ReadContents(clientlist))
      EndIf
      
      x = File.ReadLine(clientlist,1)
      ipnum = 2
      While x <> ""
        
        If debug = 1 then
          TextWindow.WriteLine(x)
        EndIf
        If Broadcastflag = 1 Then
          err =  IOTCPServer.SendMessage("Server : "+msguser,x)
        Else
          err =  IOTCPServer.SendMessage(msguser,x)
        EndIf
        
        If err = "FAIL" then
          TextWindow.ForegroundColor = "RED"
          TextWindow.WriteLine("Could not Broadcast message to : " + x )
          TextWindow.ForegroundColor = "Gray"
          
          emsg = LDEncryption.AESEncrypt("Could not Broadcast message to : " + x ,Password)  
          filemsg = emsg
          filecolor = 2
          writefile()
        EndIf   
        x = File.ReadLine(clientlist,ipnum)
        ipnum = ipnum + 1
      EndWhile
    EndSub
    
    Sub writefile
      File.WriteLine(path,1,filemsg)
      File.WriteLine(path,2,filecolor)
      Auto()
    EndSub
    
    Sub Auto 'Data Integrity Unit Server Side;  Useful for the TCP Server Client
      last = last + 1
      File.WriteLine(path,3,last)
      ready = 1
    EndSub


    Client for Server:

    ' Copyright 2014 Abhishek Sathiabalan
    arg = ProgramPlus.ArgumentCount
    errcount = 0
    pa = 0
    count = 0
    If arg = 0 then
      LDProcess.Start(Program.Directory +"\" + "TCPServer.exe","")
      Program.End()
    else
      password = ProgramPlus.GetArgument(1)
    endIf
    TextWindow.Title = "Server Client "
    path = Program.Directory + "\" +"Log"+ "\"  + "Log.txt"
    TextWindow.ForegroundColor = "Gray"
    TextWindow.WriteLine("")
    TextWindow.WriteLine("                 ╔════════════════════════════════════════════╗")
    TextWindow.WriteLine("                 ║ This window outputs messages to the server ║")
    TextWindow.WriteLine("                 ║  as well as connections and disconnections ║")
    TextWindow.WriteLine("                 ╚════════════════════════════════════════════╝")
    TextWindow.WriteLine("")
    Timer.Interval = 10
    Timer.Tick = Tick
    TextWindow.Left = 1000
    TextWindow.Top = 1
    Sub Tick 
      count = count + 1
      msg = File.ReadLine(path,1)
      color = File.ReadLine(path,2)
      a = File.ReadLine(path,3)
      TextWindow.Title = "Server Client " + a + ";" + pa + ";"' + count
      If color = 1 Then
        TextWindow.ForegroundColor = "Green"
      ElseIf color = 2 then
        TextWindow.ForegroundColor = "Red"
      ElseIf color = 3 then
        TextWindow.ForegroundColor = "Yellow"
      ElseIf color = 4 then
        TextWindow.ForegroundColor = "Blue"
      ElseIf color = 5 then
        TextWindow.ForegroundColor = "White"
      EndIf
      If errcount > 10 Then
        TextWindow.ForegroundColor = "WHITE"
        TextWindow.WriteLine("Error: System Corruption /Bad Password.")
        TextWindow.WriteLine("Server Restart /Server Client Suggested")
        TextWindow.WriteLine("Server Client Paused")
        TextWindow.WriteLine("Press any button to continue")
        errcount = 0
        TextWindow.PauseWithoutMessage()
        ProgramPlus.End()
      EndIf 
      If a = pa + 1 Then
        pa = a
        msg = LDEncryption.AESDecrypt(msg,password)
        TextWindow.WriteLine(msg)
        If msg = "" Then
          errcount = errcount + 2
          TextWindow.WriteLine("Possible Bad Password")
        EndIf
      ElseIf a > pa then  
        TextWindow.ForegroundColor = "White"
        temp = pa + 1
        TextWindow.WriteLine("Error: Possible Corruption.")
        TextWindow.WriteLine("Expected Value : " + temp)
        TextWindow.WriteLine("Given Value : " + a )
        TextWindow.ForegroundColor = "Gray"
        pa = a
        errcount = errcount + 1
      EndIf
    EndSub

    Gets List of Clients for server:

    'Copyright 2014 Abhishek Sathiabalan 
    arg = ProgramPlus.ArgumentCount
    TextWindow.Title = "Client List - Server Side Software v1"
    If arg = 0 Then
      a[1] = "127.0.0.1"
      a[2] = "192.168.1.1"
      a[3] = "192.168.1.2"
      a[4] = "192.168.1.3"
      a[5] = "192.168.1.5"
      a[6] = "192.168.1.4"
      path = Program.Directory +"\" + "IP Generic Test Data.txt"
      type = 1
    Else
      a =  ProgramPlus.GetArgument(1)
      path = Program.Directory +"\" + "Log" + "\" + "Clients.txt"
    EndIf
    
    File.DeleteFile(path)
    For count = 0 To Array.GetItemCount(a)
      File.AppendContents(path,a[count])
    EndFor
      Program.End()







    • Edited by a65001 Wednesday, December 31, 2014 9:08 PM As Requested by Zock77
    Wednesday, December 31, 2014 3:30 PM

Answers

  • Okay after some more tinkering i figured out the bug was linked to the text box rebuilding itself in the Client Program..
    • Marked as answer by a65001 Monday, January 12, 2015 11:40 PM
    • Unmarked as answer by a65001 Monday, January 12, 2015 11:40 PM
    • Marked as answer by a65001 Thursday, February 5, 2015 11:47 PM
    Monday, January 12, 2015 11:40 PM

All replies

  • Would you please post the full client and server code here? That way we don't have to mess with commented lines and two-part codes. :D

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Wednesday, December 31, 2014 5:29 PM
    Answerer
  • Done. 

    Just run all the code in the same folder. Also I have a lot of extensions installed : They are 

    Data Extension, Excel Library for Small Basic, I/O Extension, & LitDev Demonstration C# Extension Set if it matters at all. 

    Thursday, January 1, 2015 4:15 PM
  • Hi a65001!

    Do the IOTCP samples included with the extension work properly?


    Please mark any answers and "vote as helpful" any posts that help you!

    Monday, January 12, 2015 1:26 AM
    Answerer
  • Okay after some more tinkering i figured out the bug was linked to the text box rebuilding itself in the Client Program..
    • Marked as answer by a65001 Monday, January 12, 2015 11:40 PM
    • Unmarked as answer by a65001 Monday, January 12, 2015 11:40 PM
    • Marked as answer by a65001 Thursday, February 5, 2015 11:47 PM
    Monday, January 12, 2015 11:40 PM
  • If you are still having issues with this try simplifying the program as much as possible to help you identify the problem or some-one to help you - it is hard (overly time consuming) to help with a long program where much of it it is irrelevant to the issue.
    Sunday, February 1, 2015 3:39 PM
    Moderator