Announcement

Collapse
No announcement yet.

TCP SEND Question.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TCP SEND Question.

    Im sending the contents of a bitmap over TCP functions over my LAN
    with this function:

    '------------------------------------------------------------------------------------------------------------------------
    Code:
    Function SendInfo(ByVal SDAddress As Long) As Long  
      
      Local SD As SendData Ptr
      Local TheString As String
      
      SD = SDAddress
      
      TheString = Peek$(@SD.StringPointer, @SD.StringLength)
      
      ErrClear
      
      Tcp Send @SD.hFileOpen, TheString  
      
      Call PostMessage(HWND_TASKBAR, %TCP_DATASENT, Err, Len(TheString))
    
    End Function
    No errors are reported.

    This is the function i use to receive the information in the other side:

    Code:
    Function ReceiveInfo(ByVal hFileOpen As Long) As Long
    
    Local Recibido As String
    Local Buffer   As String
    Local SD       As SendData
    
    ErrClear
    
    Do
     Tcp Recv hFileOpen, 18945, Buffer
     If Buffer = "" Then Exit Do     
     Recibido  = (Recibido & Buffer) 
     If (Len(Buffer) < 18945) Then
      If Eof(hFileOpen) Then Exit Do
     End If 
    Loop
    
    SD.hFileOpen     = hFileOpen
    SD.StringPointer = StrPtr(Recibido)
    SD.StringLength  = Len(Recibido)
           
    Call SendMessage(HWND_MAINFORMSERVER, %TCP_DATAREADY, hFileOpen, VarPtr(SD))
    
    End Function
    It works good with small amounts of information, however, with the bitmaps
    which weight around 3MB each, the information is truncated at 80kb. Not even close to the real contents.

    My question is, can any characters contained in the BMP raw information
    be interpreted as EOF?

    In case not... what can i possibly be doing wrong here?

    Thanx.

  • #2
    >>>...any characters contained in the BMP raw information be interpreted as EOF? <<<

    Quite possibly. Write a short test program and check for CHR$(26).

    If that passes muster, you might modify your program to send the file in chunks instead of all in one fell swooop.
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

    Comment


    • #3
      Server and client with threaded TCP sends

      I haven't tried sending really big files, but this doesn't care
      what characters are in the data. I'll try later sending big files
      and see if it makes any difference.

      Code:
      #PBFORMS CREATED V1.51
      'CgiListen2.Bas
      #COMPILE EXE
      #DIM ALL
      '******************************************************************************************************
      'This is a server and a client.  The code was based upon EchoServer.
      'The TCP SEND FILE runs in another thread so packets are received at random times.
      '
      'Send file sends data using a random sleep statement so packet
      'reconstruction can be seen in the MSGBOX.  Remove when done.
      'Placed CHR$(26) in the sent data to show any characters can be sent.
      'To send files a TCP SEND might start with <SENDING MYFILE >
      'When done, server might look for          <FILE COMPLETE>
      'Since you control both ends, even <SENDING 9,999,999 bytes>
      'Change these 2 variables
      %PortNumber = 443
      $IP        = "192.168.0.100"
      '*********************************************************************************
      #INCLUDE "win32api.inc"
      '------------------------------------------------------------------------------
      '   ** Includes **
      '------------------------------------------------------------------------------
      #PBFORMS BEGIN INCLUDES
      #IF NOT %DEF(%WINAPI)
          #INCLUDE "WIN32API.INC"
      #ENDIF
      #PBFORMS END INCLUDES
      '------------------------------------------------------------------------------
      #INCLUDE "WS2_32.INC"
      '------------------------------------------------------------------------------
      '   ** Constants **
      '------------------------------------------------------------------------------
      #PBFORMS BEGIN CONSTANTS
      %IDD_DIALOG1 =  101
      %BUTTON1     = 1001
      %BUTTON2     = 1002
      %BUTTON3     = 1003
      %BUTTON4     = 1004
      #PBFORMS END CONSTANTS
      %INVALID_SOCKET = &HFFFFFFFF???
      '------------------------------------------------------------------------------
      %TCP_ACCEPT = %WM_USER + 4093  ' Any value larger than %WM_USER + 500
      %TCP_ECHO   = %WM_USER + 4094  ' Any value larger than %WM_USER + 500
      '------------------------------------------------------------------------------
      '   ** Declarations **
      '------------------------------------------------------------------------------
      DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
      DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
      #PBFORMS DECLARATIONS
      '------------------------------------------------------------------------------
      '   ** Main Application Entry Point **
      '------------------------------------------------------------------------------
      GLOBAL gPacketCounter AS DWORD
      FUNCTION PBMAIN()
          ShowDIALOG1 %HWND_DESKTOP
      END FUNCTION
      '------------------------------------------------------------------------------
      '------------------------------------------------------------------------------
      '   ** CallBacks **
      '------------------------------------------------------------------------------
      CALLBACK FUNCTION ShowDIALOG1Proc()
          LOCAL PortNumber AS LONG
          LOCAL s AS STRING
          LOCAL sBuffer AS STRING
          LOCAL sPacket AS STRING
          STATIC Counter_STATIC AS LONG
          STATIC hServer_STATIC AS LONG
          STATIC hEcho_STATIC AS LONG
          SELECT CASE AS LONG CBMSG
              CASE %WM_INITDIALOG
                  ' Initialization handler
                  hServer_STATIC = FREEFILE
                  PortNumber = %PortNumber 'global not needed, but used in this example
                  DIM hPort AS LONG
                  TCP OPEN SERVER PORT PortNumber AS hServer_STATIC
                  IF ERR THEN
                    hServer_STATIC = 0
                    DIALOG POST CBHNDL, %WM_USER + 999, 0,0  'don't use dialog end in initdialog per manual
                  ELSE
                    TCP NOTIFY hServer_STATIC, ACCEPT TO CBHNDL AS %TCP_ACCEPT
                  END IF
                  hEcho_STATIC = %INVALID_SOCKET    'why invalid here, so doesn't go through CASE statement?
                  FUNCTION = 1
              CASE %WM_USER + 999
                  DIALOG END CBHNDL
                  FUNCTION =1
              CASE %TCP_ACCEPT
                   'each time an html page is loaded a cgi program will send request through here
                   SELECT CASE CBLPARAM
                    CASE %FD_ACCEPT
                       hEcho_STATIC = FREEFILE
                       TCP ACCEPT hServer_STATIC AS hEcho_STATIC
                       TCP NOTIFY hEcho_STATIC, RECV CLOSE TO CBHNDL AS %TCP_ECHO
                     END SELECT
                FUNCTION = 1
              CASE %TCP_ECHO
                SELECT CASE CBLPARAM
                  CASE %FD_READ
                    IF hEcho_STATIC <> %INVALID_SOCKET THEN
                      sBuffer = ""
                      sPacket = ""
                      DO
                        TCP RECV hEcho_STATIC, 1024, sBuffer
                        sPacket = sPacket & sBuffer
                      LOOP UNTIL sBuffer = "" OR ISTRUE EOF(hEcho_STATIC) OR ISTRUE ERR
       
                      IF LEN(sBuffer) THEN
                        'This is where you check for a command like <START>/<END> <Bytes being sent 1,393,293>
                        s = sPacket          'packet complete, may not be entire file.
                        INCR gPacketCounter  '
                        ? s                  '
                      END IF
                    END IF
                END SELECT
      '---------------------------------------------------------------------------------------------
              CASE %WM_NCACTIVATE
                  STATIC hWndSaveFocus AS DWORD
                  IF ISFALSE CBWPARAM THEN
                      ' Save control focus
                      hWndSaveFocus = GetFocus()
                  ELSEIF hWndSaveFocus THEN
                      ' Restore control focus
                      SetFocus(hWndSaveFocus)
                      hWndSaveFocus = 0
                  END IF
              CASE %WM_COMMAND
                  ' Process control notifications
                  SELECT CASE AS LONG CBCTL
                      CASE %BUTTON1
                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                          BUTTON1
                        END IF
                      CASE %BUTTON2
                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                          BUTTON2
                        END IF
                      CASE %BUTTON3
                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                          BUTTON3
                        END IF
                      CASE %BUTTON4
                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                          BUTTON4
                      END IF
                  END SELECT
              CASE %WM_CLOSE
              CASE %WM_QUERYENDSESSION
                  'FUNCTION = 1
          END SELECT
      END FUNCTION
      '------------------------------------------------------------------------------
      '------------------------------------------------------------------------------
      '   ** Dialogs **
      '------------------------------------------------------------------------------
      FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
          LOCAL lRslt AS LONG
      #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
          LOCAL hDlg  AS DWORD
          DIALOG NEW hParent, "CGIListen2", 145, 132, 286, 25, %WS_POPUP OR _
              %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR _
              %WS_VISIBLE OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT OR _
              %DS_CENTER, %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
              %WS_EX_RIGHTSCROLLBAR OR %WS_EX_CONTROLPARENT, TO hDlg
          CONTROL ADD BUTTON, hDlg, %BUTTON1, "TCP Send Text", 0, 5, 65, 15
          CONTROL ADD BUTTON, hDlg, %BUTTON2, "", 72, 5, 65, 15
          CONTROL ADD BUTTON, hDlg, %BUTTON3, "Send File", 144, 5, 65, 15
          CONTROL ADD BUTTON, hDlg, %BUTTON4, "", 216, 5, 65, 15
      #PBFORMS END DIALOG
          DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
      #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
      #PBFORMS END CLEANUP
          FUNCTION = lRslt
      END FUNCTION
       
      SUB BUTTON1        'SendText
        LOCAL LineNumber AS LONG
        LOCAL h AS LONG
        h = FREEFILE
        TCP OPEN PORT %PortNumber AT $IP AS #h
        IF ERR THEN ? "Unable to open in button 1":EXIT SUB
        FOR LineNumber = 1 TO 2
          TCP SEND #h, "This is line" + STR$(LineNumber)
          IF ERR THEN ? "Send error" + STR$(ERR):EXIT FOR
          SLEEP 250
        NEXT
        TCP CLOSE #h
      END SUB
      SUB BUTTON2 'Read text
          ? "Removed code"
      END SUB
      SUB BUTTON3  'Send file
        'Send file(s) from another thread
        LOCAL x AS LONG, hThread AS DWORD
        THREAD CREATE MyThread(BYVAL x) TO hThread???
        SLEEP 500
        THREAD CLOSE hThread TO x
      END SUB
      THREAD FUNCTION MyThread(BYVAL x AS DWORD) AS DWORD
        LOCAL hFile AS LONG
        LOCAL sBuffer AS STRING
        LOCAL sFileName AS STRING
        hFile = FREEFILE
        TCP OPEN PORT %PortNumber AT $IP AS #hFile
        IF ERR THEN
          ? "Unable to open port"+ STR$(%PortNumber) + " in MyThread"
        ELSE
          LOCAL xx AS LONG
          LOCAL sDummy AS STRING
          LOCAL SleepTime AS LONG
          FOR xx = 1 TO 10
            SleepTime = RND(50,250)
            sDummy = CHR$(26,26,26 ,"line" + STR$(xx),26,26,26,10)
            TCP SEND #hFile, sDummy
            SLEEP SleepTime
          NEXT
          IF ERR THEN ? "Send error" + STR$(ERR)
        END IF
        TCP CLOSE #hFile
        ? "Thread completed"
      END FUNCTION
       
      SUB BUTTON4  'Execute file
        ? "Removed code"
      END SUB
      FUNCTION Exist(File$) AS LONG
        LOCAL Dummy&
        Dummy& = GETATTR(File$)
        FUNCTION = (ERRCLEAR = 0)  '0 or -1
      END FUNCTION
      Last edited by Mike Doty; 26 Dec 2008, 04:49 PM.
      How long is an idea?

      Comment


      • #4
        Haven't had the chance to play with this, but what I've almost always found best (expecially with this type of app) is to define my own start/end markers - and always make them at least 7 characters.

        So I would have the field (or bitstream) start with something like: '***START***' and end with '***END***'

        It may slow things down somewhat, but I've found taht to be fairly reliable. Regardless of the protocol used.

        Then (if needed) I would manually trigger the protocol's EOF or end of transmission event.

        JS
        John,
        --------------------------------
        John Strasser
        Phone: 480 - 273 - 8798

        Comment


        • #5
          Something really strange is happening to my app...

          It is only triggering the TCP_ECHO + %FD_READ once... but the ReceiveInfo is being called 3 or 4 times..

          Heres my code:
          Code:
            Case %TCP_ECHO To (%TCP_ECHO+128)   ' Esta llegando informacion de un cliente.    
            
              hFileOpen = GetFileOpenByIndex(wMsg-%TCP_ECHO)
            
              Select Case LoWrd(LParam)
                Case %FD_READ             
                 Thread Create ReceiveInfo(hFileOpen) To hThread
                 Thread Close HThread To Result
                 
                 Sleep 200
                 
                 Function = %TRUE
          THE GetFileOpenByIndex function just finds the handle to be used in an array of max 15 items. Nothing out of ordinary.

          Then i call the receive info i posted first. I log everything to file, and i get 1 hit to TCP_ECHO but 3 calls to ReceiveInfo.... really weird. the Receiveinfo is not used anywhere else!!
          Last edited by Elias Montoya; 27 Dec 2008, 01:17 PM.

          Comment


          • #6
            The thread experts should step in.
            Is that the correct place to create thread?
            How long is an idea?

            Comment


            • #7
              I think i found it. Moment...

              Comment


              • #8
                Hello again,

                Mike, my server seems to be working fine. But my client seems to be sending information reaaaaaaallly slow. Your program sends 17000 bytes in about 100ms or less. My client sends 17000 bytes in around 19 seconds!!

                I set up your program to listen to connections from my client and your program receives 17000 bytes in 19 seconds or more. That is way too much! So i guess im doing something wrong in my client:

                I have a timer set up to trigger a function every 1000 ms, and this is the code:

                Code:
                ' THREAD.
                Function AttemptConnection(ByVal hWndForm As Dword) As Dword 
                 hServer = FreeFile
                 ErrClear 
                 Tcp Open Port Config.ServerPort At Trim$(Config.ServerIP, Any Chr$(0, 32)) As hServer 
                 If Err Then 
                  hServer = 0 
                  GoTo DidntWorked
                 End If 
                 Tcp Notify #hServer, Recv Close To hWndForm As %TCP_RECEIVE 
                 
                 ' Tcp Send #hServer, Repeat$(1000, "?????????????????") ' This was just to send 17000 bytes as soon as your program connects
                 
                 DidntWorked: 
                 AttemptingConnection = %FALSE 
                End Function
                
                '------------------------------------------------------------------------------------------------------------------------
                Function TASKBAR_TIMER3_WM_TIMER ( _
                                                 hWndForm      As Dword, _  ' handle of Form
                                                 wTimerID      As Dword  _  ' the timer identifier
                                                 ) As Long
                
                Local hThread As Dword
                Local Result  As Dword
                
                If AttemptingConnection Then Exit Function
                If IsFalse(hServer) Then
                 AttemptingConnection = %TRUE 
                 Thread Create AttemptConnection(HWND_TASKBAR) To hThread
                 Thread Close HThread To Result 
                End If
                
                End Function
                The logic of this code is:

                If there are no connections and no connections are being stablished, try to connect, but if there is a connection or one is trying to be stablished, do nothing.

                It works. As soon as the Server gets available, the client connects right away. But its very slow to send information...

                Any ideas why?



                That is the problem. an

                Comment


                • #9
                  Looking, now.
                  How long is an idea?

                  Comment


                  • #10
                    Code:
                    If IsFalse(hServer) Then
                     AttemptingConnection = %TRUE 
                     Thread Create AttemptConnection(HWND_TASKBAR) To hThread
                     Thread Close HThread To Result 
                    End If
                    This code should wait until the thread function 'attemptconnection' has completed:
                    Code:
                    If IsFalse(hServer) Then
                     AttemptingConnection = %TRUE 
                     Thread Create AttemptConnection(HWND_TASKBAR) To hThread
                      WaitForSingleObject  hThread, %INFINITE  
                    Thread Close HThread To Result 
                    End If
                    That function either connects or it don't.
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      Thanx Michael, The thing is that i dont want to hang the dialog until the connection is
                      stablished. i want the user to be able to move dialog or do some other stuff. Othwerwise
                      i wouldnt make it in a thread, i would do it directly in the timer function.


                      By the way this is the non working code:

                      Code:
                       Global AttemptingConnection As Dword
                       Global hServer As Dword
                       
                       %PortNumber = 8912                 
                       $IP        = "192.168.1.100"       '<---Change as needed 
                       
                       
                      '------------------------------------------------------------------------------------------------------------------------
                      Function AttemptConnection(ByVal hWndForm As Dword) As Dword 
                       
                       
                      
                       hServer = FreeFile
                       ErrClear 
                       Tcp Open Port 8912 At "192.168.1.100" As hServer   ' Try to open it
                       If Err Then 
                        hServer = 0 
                        GoTo DidntWorked
                       End If 
                        
                       Tcp Send #hServer, Repeat$(1000, "?????????????????")
                       
                       DidntWorked: 
                       AttemptingConnection = %FALSE    '<--- So the app keeps looking for a server.
                       
                      End Function
                      
                      '------------------------------------------------------------------------------------------------------------------------
                      Function FORM1_TIMER1_WM_TIMER  ( _
                                                       hWndForm      As Dword, _  ' handle of Form
                                                       wTimerID      As Dword  _  ' the timer identifier
                                                       ) As Long
                      
                      Local hThread As Dword
                      Local Result  As Dword
                      
                      If AttemptingConnection Then Exit Function   ' 'If we are trying to connect, no need to retry.
                      If IsTrue(hServer)      Then Exit Function   ' If we already have a connection, no need to create another.
                       
                      AttemptingConnection = %TRUE                 'So we dont start new connections until we know if one is possible
                       
                      
                      Thread Create AttemptConnection(0) To hThread   ' Try to create one.
                      Thread Close HThread To Result 
                       
                      
                      End Function
                      You can try it with this modified code from mike:

                      Code:
                      #PBFORMS CREATED V1.51
                      'CgiListen2.Bas
                      #COMPILE EXE
                      #DIM ALL
                      '******************************************************************************************************
                      'This is a server and a client.  The code was based upon EchoServer.
                      'The TCP SEND FILE runs in another thread so packets are received at random times.
                      '
                      'Send file sends data using a random sleep statement so packet
                      'reconstruction can be seen in the MSGBOX.  Remove when done.
                      'Placed CHR$(26) in the sent data to show any characters can be sent.
                      'To send files a TCP SEND might start with <SENDING MYFILE >
                      'When done, server might look for          <FILE COMPLETE>
                      'Since you control both ends, even <SENDING 9,999,999 bytes>
                      'Change these 2 variables
                      %PortNumber = 8912     '443
                      $IP        = "192.168.1.100"   '<--- Change as needed
                      '*********************************************************************************
                      #INCLUDE "win32api.inc"
                      '------------------------------------------------------------------------------
                      '   ** Includes **
                      '------------------------------------------------------------------------------
                      #PBFORMS BEGIN INCLUDES
                      #IF NOT %DEF(%WINAPI)
                          #INCLUDE "WIN32API.INC"
                      #ENDIF
                      #PBFORMS END INCLUDES
                      '------------------------------------------------------------------------------
                      #INCLUDE "WS2_32.INC"
                      '------------------------------------------------------------------------------
                      '   ** Constants **
                      '------------------------------------------------------------------------------
                      #PBFORMS BEGIN CONSTANTS
                      %IDD_DIALOG1 =  101
                      %BUTTON1     = 1001
                      %BUTTON2     = 1002
                      %BUTTON3     = 1003
                      %BUTTON4     = 1004
                      #PBFORMS END CONSTANTS
                      %INVALID_SOCKET = &HFFFFFFFF???
                      '------------------------------------------------------------------------------
                      %TCP_ACCEPT = %WM_USER + 4093  ' Any value larger than %WM_USER + 500
                      %TCP_ECHO   = %WM_USER + 4094  ' Any value larger than %WM_USER + 500
                      '------------------------------------------------------------------------------
                      '   ** Declarations **
                      '------------------------------------------------------------------------------
                      DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
                      DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
                      #PBFORMS DECLARATIONS
                      '------------------------------------------------------------------------------
                      '   ** Main Application Entry Point **
                      '------------------------------------------------------------------------------
                      GLOBAL gPacketCounter AS DWORD
                      FUNCTION PBMAIN()
                          ShowDIALOG1 %HWND_DESKTOP
                      END FUNCTION
                      '------------------------------------------------------------------------------
                      
                      SUB BUTTON1        'SendText
                        LOCAL LineNumber AS LONG
                        LOCAL h AS LONG
                        h = FREEFILE
                        TCP OPEN PORT %PortNumber AT $IP AS #h
                        IF ERR THEN MSGBOX "Unable to open in button 1":EXIT SUB
                        FOR LineNumber = 1 TO 2
                          TCP SEND #h, "This is line" + STR$(LineNumber)
                          IF ERR THEN MSGBOX "Send error" + STR$(ERR):EXIT FOR
                          SLEEP 250
                        NEXT
                        TCP CLOSE #h
                      END SUB
                      SUB BUTTON2 'Read text
                          MSGBOX "Removed code"
                      END SUB
                      SUB BUTTON3  'Send file
                        'Send file(s) from another thread
                        LOCAL x AS LONG, hThread AS DWORD
                        THREAD CREATE MyThread(BYVAL x) TO hThread???
                        SLEEP 500
                        THREAD CLOSE hThread TO x
                      END SUB
                      
                      
                      SUB BUTTON4  'Execute file
                        MSGBOX "Removed code"
                      END SUB
                      '------------------------------------------------------------------------------
                      '   ** CallBacks **
                      '------------------------------------------------------------------------------
                      CALLBACK FUNCTION ShowDIALOG1Proc()
                          LOCAL PortNumber AS LONG
                          LOCAL s AS STRING
                          LOCAL sBuffer AS STRING
                          LOCAL sPacket AS STRING
                          STATIC Counter_STATIC AS LONG
                          STATIC hServer_STATIC AS LONG
                          STATIC hEcho_STATIC AS LONG
                          SELECT CASE AS LONG CBMSG
                              CASE %WM_INITDIALOG
                                  ' Initialization handler
                                  hServer_STATIC = FREEFILE
                                  PortNumber = %PortNumber 'global not needed, but used in this example
                                  DIM hPort AS LONG
                                  TCP OPEN SERVER PORT PortNumber AS hServer_STATIC
                                  'IF ERR THEN
                                  '  hServer_STATIC = 0
                                  '  DIALOG POST CBHNDL, %WM_USER + 999, 0,0  'don't use dialog end in initdialog per manual
                                  'ELSE
                                    TCP NOTIFY hServer_STATIC, ACCEPT TO CBHNDL AS %TCP_ACCEPT
                                  'END IF
                                  'hEcho_STATIC = %INVALID_SOCKET    'why invalid here, so doesn't go through CASE statement?
                                  'FUNCTION = 1
                              CASE %WM_USER + 999
                                  DIALOG END CBHNDL
                                  FUNCTION =1
                              CASE %TCP_ACCEPT
                                   'each time an html page is loaded a cgi program will send request through here
                                   SELECT CASE CBLPARAM
                                    CASE %FD_ACCEPT
                                       hEcho_STATIC = FREEFILE
                                       TCP ACCEPT hServer_STATIC AS hEcho_STATIC
                                       TCP NOTIFY hEcho_STATIC, RECV CLOSE TO CBHNDL AS %TCP_ECHO
                                     END SELECT
                                FUNCTION = 1
                              CASE %TCP_ECHO
                                SELECT CASE CBLPARAM
                                  CASE %FD_READ
                       
                                    IF hEcho_STATIC <> %INVALID_SOCKET THEN
                                      sBuffer = ""
                                      sPacket = ""
                                      DO
                                        TCP RECV hEcho_STATIC, 1024, sBuffer
                                        sPacket = sPacket & sBuffer
                                      LOOP UNTIL sBuffer = "" OR ISTRUE EOF(hEcho_STATIC) OR ISTRUE ERR
                      
                                      IF LEN(sBuffer) THEN
                                        'This is where you check for a command like <START>/<END> <Bytes being sent 1,393,293>
                                        s = sPacket          'packet complete, may not be entire file.
                                        INCR gPacketCounter  '
                                        MSGBOX "Recibido!",,"LEN Received = " & STR$(LEN(s))     '
                                      END IF
                                    END IF
                                END SELECT
                                
                      '---------------------------------------------------------------------------------------------
                              CASE %WM_NCACTIVATE
                                  STATIC hWndSaveFocus AS DWORD
                                  IF ISFALSE CBWPARAM THEN
                                      ' Save control focus
                                      hWndSaveFocus = GetFocus()
                                  ELSEIF hWndSaveFocus THEN
                                      ' Restore control focus
                                      SetFocus(hWndSaveFocus)
                                      hWndSaveFocus = 0
                                  END IF
                              CASE %WM_COMMAND
                                  ' Process control notifications
                                  SELECT CASE AS LONG CBCTL
                                      CASE %BUTTON1
                                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                          BUTTON1
                                        END IF
                                      CASE %BUTTON2
                                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                          BUTTON2
                                        END IF
                                      CASE %BUTTON3
                                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                          BUTTON3
                                        END IF
                                      CASE %BUTTON4
                                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                          BUTTON4
                                      END IF
                                  END SELECT
                              CASE %WM_CLOSE
                              CASE %WM_QUERYENDSESSION
                                  'FUNCTION = 1
                          END SELECT
                      END FUNCTION
                      '------------------------------------------------------------------------------
                      '------------------------------------------------------------------------------
                      '   ** Dialogs **
                      '------------------------------------------------------------------------------
                      FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
                          LOCAL lRslt AS LONG
                      #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
                          LOCAL hDlg  AS DWORD
                          DIALOG NEW hParent, "CGIListen2", 145, 132, 286, 25, %WS_POPUP OR _
                              %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR _
                              %WS_VISIBLE OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT OR _
                              %DS_CENTER, %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
                              %WS_EX_RIGHTSCROLLBAR OR %WS_EX_CONTROLPARENT, TO hDlg
                          CONTROL ADD BUTTON, hDlg, %BUTTON1, "TCP Send Text", 0, 5, 65, 15
                          CONTROL ADD BUTTON, hDlg, %BUTTON2, "", 72, 5, 65, 15
                          CONTROL ADD BUTTON, hDlg, %BUTTON3, "Send File", 144, 5, 65, 15
                          CONTROL ADD BUTTON, hDlg, %BUTTON4, "One more Test", 216, 5, 65, 15
                      #PBFORMS END DIALOG
                          DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
                      #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
                      #PBFORMS END CLEANUP
                          FUNCTION = lRslt
                      END FUNCTION
                      
                      
                      FUNCTION MyThread(BYVAL x AS DWORD) AS DWORD
                        LOCAL hFile AS LONG
                        LOCAL sBuffer AS STRING
                        LOCAL sFileName AS STRING
                        hFile = FREEFILE
                        TCP OPEN PORT %PortNumber AT $IP AS #hFile
                        IF ERR THEN
                          MSGBOX "Unable to open port"+ STR$(%PortNumber) + " in MyThread"
                        ELSE
                          LOCAL xx AS LONG
                          LOCAL sDummy AS STRING
                          LOCAL SleepTime AS LONG
                            sDummy = REPEAT$(700000, CHR$(26))
                            TCP SEND #hFile, sDummy
                            SLEEP SleepTime
                          IF ERR THEN MSGBOX "Send error" + STR$(ERR)
                        END IF
                        TCP CLOSE #hFile
                        MSGBOX "Thread completed"
                      END FUNCTION
                      
                      FUNCTION Exist(File$) AS LONG
                        LOCAL Dummy&
                        Dummy& = GETATTR(File$)
                        FUNCTION = (ERRCLEAR = 0)  '0 or -1
                      END FUNCTION

                      How:

                      Insert my timer code in an existing dialog, compile it and run it. It should start looking for servers right away.

                      Then just compile and run the modified code from mike. no need to push buttons, just open it. It should receive the info reaaaaally slow. And idk why.
                      Last edited by Elias Montoya; 27 Dec 2008, 02:52 PM.

                      Comment


                      • #12
                        How long does this take?
                        This might make debugging a little easier by separating out the client.
                        Also, sent you a private email with a server to test against (if you want.)

                        Code:
                        #COMPILE EXE   'SimpleTCPClient.bas
                        #DIM ALL
                         
                        FUNCTION PBMAIN AS LONG
                          LOCAL PortNumber&,RemoteHost$
                          PortNumber& = 443
                          RemoteHost$ = "192.168.0.100"
                          SimpleTCPClient PortNumber, RemoteHost
                        END FUNCTION
                         
                        SUB SimpleTCPClient(PortNumber AS LONG, RemoteHost AS STRING)
                          LOCAL s AS STRING
                          LOCAL h AS LONG
                          LOCAL counter AS LONG
                         
                          h = FREEFILE
                          TCP OPEN PORT PortNumber& AT RemoteHost$ AS #h
                          IF ERR THEN ? "TCP OPEN error" + STR$(ERRCLEAR):EXIT SUB
                          DO
                            INCR counter
                            s = INPUTBOX$("Enter anything","TCP Client","How now brown cow" + STR$(counter))
                            IF LEN(s) = 0 THEN EXIT DO
                            TCP SEND #h, s
                            IF ERR THEN ? "Send error" + STR$(ERRCLEAR)
                          LOOP WHILE LEN(s)
                          TCP CLOSE #h
                        END SUB
                        Last edited by Mike Doty; 27 Dec 2008, 03:26 PM.
                        How long is an idea?

                        Comment


                        • #13
                          Quite fast. No time at all.

                          Strange but i think i know whats happening. the:

                          Code:
                          TCP CLOSE #h
                          cause the information to go trough really fast. But i dont want to close the connection yet!

                          What else can i try?

                          Comment


                          • #14
                            Really fast:

                            Code:
                            hServer = FreeFile
                             ErrClear 
                             Tcp Open Port 8912 At "192.168.1.100" As hServer   ' Try to open it
                             
                             Tcp Send #hServer, Repeat$(1000, "?????????????????")
                             Tcp Send #hServer, "."
                             
                             Tcp Close #hServer
                            Really Slow:

                            Code:
                            hServer = FreeFile
                             ErrClear 
                             Tcp Open Port 8912 At "192.168.1.100" As hServer   ' Try to open it
                             
                             Tcp Send #hServer, Repeat$(1000, "?????????????????")
                             Tcp Send #hServer, "."
                             
                             'Tcp Close #hServer ' REMARKED

                            From help file:
                            FLUSH statement
                            Purpose
                            Flush file buffers to disk, to ensure that the disk information is up-to-date.

                            Syntax
                            FLUSH [[#] filenum& [, [#] filenum&] ...]

                            Remarks
                            FLUSH ensures that all data you have written to disk files has actually been written to disk. The CLOSE statement also flushes the buffers, but FLUSH has the advantage of leaving the files open.

                            filenum&
                            The file number of an OPEN file. If filenum& is specified, only the data for that file is flushed. Otherwise, data for all open files is flushed. The Number (#) symbol is optional, but recommended for the purposes of clarity.

                            See also
                            CLOSE, OPEN
                            But Flush didnt made it fast...

                            Comment


                            • #15
                              Try changing port number and IP to my server and see how long it takes.
                              If that is fast we have a server problem, anti-virus, firewall, etc.
                              How long is an idea?

                              Comment


                              • #16
                                ok

                                Comment


                                • #17
                                  Didnt made any diference. Slow.

                                  I have a test for you, notice that in the following code, there is a remark in the "MyThread" function. toggling that code makes it fast or slow.

                                  Remarked the close statement = fast.
                                  Unremark the close statement = slow.

                                  try it... as it is, it is slow here:

                                  Code:
                                  #PBFORMS CREATED V1.51
                                  'CgiListen2.Bas
                                  #COMPILE EXE
                                  #DIM ALL
                                  '******************************************************************************************************
                                  'This is a server and a client.  The code was based upon EchoServer.
                                  'The TCP SEND FILE runs in another thread so packets are received at random times.
                                  '
                                  'Send file sends data using a random sleep statement so packet
                                  'reconstruction can be seen in the MSGBOX.  Remove when done.
                                  'Placed CHR$(26) in the sent data to show any characters can be sent.
                                  'To send files a TCP SEND might start with <SENDING MYFILE >
                                  'When done, server might look for          <FILE COMPLETE>
                                  'Since you control both ends, even <SENDING 9,999,999 bytes>
                                  'Change these 2 variables
                                  %PortNumber = 443
                                  $IP        = "192.168.1.100"   '<--- Change as needed
                                  '*********************************************************************************
                                  #INCLUDE "win32api.inc"
                                  '------------------------------------------------------------------------------
                                  '   ** Includes **
                                  '------------------------------------------------------------------------------
                                  #PBFORMS BEGIN INCLUDES
                                  #IF NOT %DEF(%WINAPI)
                                      #INCLUDE "WIN32API.INC"
                                  #ENDIF
                                  #PBFORMS END INCLUDES
                                  '------------------------------------------------------------------------------
                                  #INCLUDE "WS2_32.INC"
                                  '------------------------------------------------------------------------------
                                  '   ** Constants **
                                  '------------------------------------------------------------------------------
                                  #PBFORMS BEGIN CONSTANTS
                                  %IDD_DIALOG1 =  101
                                  %BUTTON1     = 1001
                                  %BUTTON2     = 1002
                                  %BUTTON3     = 1003
                                  %BUTTON4     = 1004
                                  #PBFORMS END CONSTANTS
                                  %INVALID_SOCKET = &HFFFFFFFF???
                                  '------------------------------------------------------------------------------
                                  %TCP_ACCEPT = %WM_USER + 4093  ' Any value larger than %WM_USER + 500
                                  %TCP_ECHO   = %WM_USER + 4094  ' Any value larger than %WM_USER + 500
                                  '------------------------------------------------------------------------------
                                  '   ** Declarations **
                                  '------------------------------------------------------------------------------
                                  DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
                                  DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
                                  #PBFORMS DECLARATIONS
                                  '------------------------------------------------------------------------------
                                  '   ** Main Application Entry Point **
                                  '------------------------------------------------------------------------------
                                  GLOBAL gPacketCounter AS DWORD
                                  FUNCTION PBMAIN()
                                      ShowDIALOG1 %HWND_DESKTOP
                                  END FUNCTION
                                  '------------------------------------------------------------------------------
                                  
                                  SUB BUTTON1        'SendText
                                    LOCAL LineNumber AS LONG
                                    LOCAL h AS LONG
                                    h = FREEFILE
                                    TCP OPEN PORT %PortNumber AT $IP AS #h
                                    IF ERR THEN MSGBOX "Unable to open in button 1":EXIT SUB
                                    FOR LineNumber = 1 TO 2
                                      TCP SEND #h, "This is line" + STR$(LineNumber)
                                      IF ERR THEN MSGBOX "Send error" + STR$(ERR):EXIT FOR
                                      SLEEP 250
                                    NEXT
                                    TCP CLOSE #h
                                  END SUB
                                  SUB BUTTON2 'Read text
                                      MSGBOX "Removed code"
                                  END SUB
                                  SUB BUTTON3  'Send file
                                    'Send file(s) from another thread
                                    LOCAL x AS LONG, hThread AS DWORD
                                    THREAD CREATE MyThread(BYVAL x) TO hThread???
                                    SLEEP 500
                                    THREAD CLOSE hThread TO x
                                  END SUB
                                  
                                  
                                  SUB BUTTON4  'Execute file
                                    MSGBOX "Removed code"
                                  END SUB
                                  '------------------------------------------------------------------------------
                                  '   ** CallBacks **
                                  '------------------------------------------------------------------------------
                                  CALLBACK FUNCTION ShowDIALOG1Proc()
                                      LOCAL PortNumber AS LONG
                                      LOCAL s AS STRING
                                      LOCAL sBuffer AS STRING
                                      LOCAL sPacket AS STRING
                                      STATIC Counter_STATIC AS LONG
                                      STATIC hServer_STATIC AS LONG
                                      STATIC hEcho_STATIC AS LONG
                                      SELECT CASE AS LONG CBMSG
                                          CASE %WM_INITDIALOG
                                              ' Initialization handler
                                              hServer_STATIC = FREEFILE
                                              PortNumber = %PortNumber 'global not needed, but used in this example
                                              DIM hPort AS LONG
                                              TCP OPEN SERVER PORT PortNumber AS hServer_STATIC
                                              IF ERR THEN
                                                hServer_STATIC = 0
                                                DIALOG POST CBHNDL, %WM_USER + 999, 0,0  'don't use dialog end in initdialog per manual
                                              ELSE
                                                TCP NOTIFY hServer_STATIC, ACCEPT TO CBHNDL AS %TCP_ACCEPT
                                              END IF
                                              hEcho_STATIC = %INVALID_SOCKET    'why invalid here, so doesn't go through CASE statement?
                                              FUNCTION = 1
                                          CASE %WM_USER + 999
                                              DIALOG END CBHNDL
                                              FUNCTION =1
                                          CASE %TCP_ACCEPT
                                               'each time an html page is loaded a cgi program will send request through here
                                               SELECT CASE CBLPARAM
                                                CASE %FD_ACCEPT
                                                   hEcho_STATIC = FREEFILE
                                                   TCP ACCEPT hServer_STATIC AS hEcho_STATIC
                                                   TCP NOTIFY hEcho_STATIC, RECV CLOSE TO CBHNDL AS %TCP_ECHO
                                                 END SELECT
                                            FUNCTION = 1
                                          CASE %TCP_ECHO
                                            SELECT CASE CBLPARAM
                                              CASE %FD_READ
                                  
                                                IF hEcho_STATIC <> %INVALID_SOCKET THEN
                                                  sBuffer = ""
                                                  sPacket = ""
                                                  DO
                                                    TCP RECV hEcho_STATIC, 1024, sBuffer
                                                    sPacket = sPacket & sBuffer
                                                  LOOP UNTIL sBuffer = "" OR ISTRUE EOF(hEcho_STATIC) OR ISTRUE ERR
                                  
                                                  IF LEN(sBuffer) THEN
                                                    'This is where you check for a command like <START>/<END> <Bytes being sent 1,393,293>
                                                    s = sPacket          'packet complete, may not be entire file.
                                                    INCR gPacketCounter  '
                                                    MSGBOX "Recibido!",,"LEN Received = " & STR$(LEN(s))     '
                                                  END IF
                                                END IF
                                            END SELECT
                                  
                                  '---------------------------------------------------------------------------------------------
                                          CASE %WM_NCACTIVATE
                                              STATIC hWndSaveFocus AS DWORD
                                              IF ISFALSE CBWPARAM THEN
                                                  ' Save control focus
                                                  hWndSaveFocus = GetFocus()
                                              ELSEIF hWndSaveFocus THEN
                                                  ' Restore control focus
                                                  SetFocus(hWndSaveFocus)
                                                  hWndSaveFocus = 0
                                              END IF
                                          CASE %WM_COMMAND
                                              ' Process control notifications
                                              SELECT CASE AS LONG CBCTL
                                                  CASE %BUTTON3
                                                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                      BUTTON3
                                                    END IF
                                              END SELECT
                                          CASE %WM_CLOSE
                                          CASE %WM_QUERYENDSESSION
                                              'FUNCTION = 1
                                      END SELECT
                                  END FUNCTION
                                  '------------------------------------------------------------------------------
                                  '------------------------------------------------------------------------------
                                  '   ** Dialogs **
                                  '------------------------------------------------------------------------------
                                  FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
                                      LOCAL lRslt AS LONG
                                  #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
                                      LOCAL hDlg  AS DWORD
                                      DIALOG NEW hParent, "CGIListen2", 145, 132, 70, 25, %WS_POPUP OR _
                                          %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR _
                                          %WS_VISIBLE OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT OR _
                                          %DS_CENTER, %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
                                          %WS_EX_RIGHTSCROLLBAR OR %WS_EX_CONTROLPARENT, TO hDlg
                                      CONTROL ADD BUTTON, hDlg, %BUTTON3, "Test Speed", 5, 5, 65, 15
                                  #PBFORMS END DIALOG
                                      DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
                                  #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
                                  #PBFORMS END CLEANUP
                                      FUNCTION = lRslt
                                  END FUNCTION
                                  
                                  
                                  FUNCTION MyThread(BYVAL x AS DWORD) AS DWORD
                                    LOCAL hFile AS LONG
                                    LOCAL sBuffer AS STRING
                                    LOCAL sFileName AS STRING
                                    hFile = FREEFILE
                                    TCP OPEN PORT %PortNumber AT $IP AS #hFile
                                    IF ERR THEN
                                      MSGBOX "Unable to open port"+ STR$(%PortNumber) + " in MyThread"
                                    ELSE
                                      LOCAL xx AS LONG
                                      LOCAL sDummy AS STRING
                                      LOCAL SleepTime AS LONG
                                        sDummy = REPEAT$(700000, CHR$(26))
                                        TCP SEND #hFile, sDummy
                                        SLEEP SleepTime
                                      IF ERR THEN MSGBOX "Send error" + STR$(ERR)
                                    END IF
                                  '=====================================================================================================================================
                                  '=====================================================================================================================================
                                  '=====================================================================================================================================
                                  '=====================================================================================================================================
                                    ' TCP CLOSE #hFile         '                       NOTE THAT I DONT WANT TO CLOSE THE CONNECTION AFTER SENDING A PACKET!!!!
                                  '=====================================================================================================================================
                                  '=====================================================================================================================================
                                  '=====================================================================================================================================
                                  '=====================================================================================================================================
                                    MSGBOX "Thread completed"
                                  END FUNCTION
                                  
                                  FUNCTION Exist(File$) AS LONG
                                    LOCAL Dummy&
                                    Dummy& = GETATTR(File$)
                                    FUNCTION = (ERRCLEAR = 0)  '0 or -1
                                  END FUNCTION

                                  Comment


                                  • #18
                                    Nothing made it to my server.
                                    Sending 700,000 bytes? Try 700 bytes.
                                    How long is an idea?

                                    Comment


                                    • #19
                                      700 bytes pass trough nicely.

                                      Try waiting a long time... eventually everything comes trough.

                                      Comment


                                      • #20
                                        You never hit my server.
                                        How long is an idea?

                                        Comment

                                        Working...
                                        X