Announcement

Collapse
No announcement yet.

TCP OPEN error

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

  • TCP OPEN error

    Hi,

    The following line fails with Error 57.

    [ code]
    TCP OPEN SERVER PORT 400 AS hSocket
    [ /code]

    How do i fix it?

    Also, how do i bind it to specific IP.. ie: 127.0.0.1

    Thanks.

    - Nathan.

  • #2
    Code:
    Tcp Open Server Addr ipAddr& Port 400 As hSocket
    where ipAddr& is a long integer representing the dot noted ip address.

    Best Regards,
    Don


    [This message has been edited by Don Dickinson (edited June 28, 2001).]
    Don Dickinson
    www.greatwebdivide.com

    Comment


    • #3
      It just pauses the program.
      It's based off a sample from the helpfile. I click the button,
      and the program pauses.

      It stops responding, and win2k automatically kills it.

      What could be wrong?

      Thanks.

      Code:
      #COMPILE EXE
      #INCLUDE "DDT.INC"
      
      $SERVER = ""
      %PORTNUM=400
      
      CALLBACK FUNCTION Go()
      
        LOCAL hSocket AS LONG
        LOCAL buffer  AS STRING
      
        hSocket = FREEFILE
        TCP OPEN SERVER PORT %PORTNUM AS hSocket
        IF ERR THEN
          MSGBOX "OPEN Error" & STR$(ERR)
          EXIT FUNCTION
        END IF
      
        CONTROL GET TEXT CBHNDL, 100 TO buffer
      
        IF LEN(buffer) = 0 THEN
          TCP SEND #hSocket, "Hello from PB/DLL v6.0!"
        ELSE
          TCP SEND #hSocket, buffer
        END IF
      
        TCP RECV #hSocket, 1024, buffer
        IF ERR THEN
      
          MSGBOX "RECV Error" & STR$(ERR)
          CLOSE #hSocket
          EXIT FUNCTION
        END IF
      
        CLOSE #hSocket
      
        MSGBOX buffer,, "Result"
      
      END FUNCTION
      
      FUNCTION PBMAIN() AS LONG
      
        LOCAL hDlg    AS LONG
      
        DIALOG NEW 0, "ECHO Client", ,, 115, 75, %DS_DDT_DEFAULT _
               OR %WS_SYSMENU TO hDlg
      
        CONTROL ADD LABEL, hDlg, -1, "Enter text", 5, 5, 100, 10
        CONTROL ADD TEXTBOX, hDlg&, 100, "", 5, 20, 102, 12
        CONTROL ADD BUTTON, hDlg&, 1, "Go", 35, 40, 40, 12 CALL Go
      
        DIALOG SHOW MODAL hDlg&
      
      END FUNCTION
      [This message has been edited by Nathan Evans (edited June 29, 2001).]

      Comment


      • #4
        Any one have some PB/DLL 6 example source for a simple TCP Server?

        Thanks.

        -Nath.

        Comment


        • #5
          Yes, there is one on my code page at http://dickinson.basicguru.com - it's a remote binary file server.

          Best Regards,
          Don

          ------------------
          dickinson.basicguru.com
          Don Dickinson
          www.greatwebdivide.com

          Comment


          • #6
            Thanks.

            I'm not very good with winsock API...

            Is it essential to create a Window for winsock?

            Comment


            • #7
              For client stuff, no ... but for server stuff, i think so - otherwise how do you handle the receive notifications? In pure winsock (not the PB abstraction, this might not be the case).

              --Don


              ------------------
              dickinson.basicguru.com
              Don Dickinson
              www.greatwebdivide.com

              Comment


              • #8
                Ok i've got the code together. It's a bit scrappy at the moment :P

                I'm still getting used to the PB code editor, and infact the new language oddities

                Thanks for your help with this topic.

                Code:
                #COMPILE EXE
                #DIM ALL
                
                #INCLUDE "WIN32API.INC"
                #INCLUDE "WSCONST.INC"
                #INCLUDE "RSERVER.INC"
                
                GLOBAL gDlgMain AS LONG
                GLOBAL hSock     AS LONG
                GLOBAL gSock AS LONG
                %TCP_CONNECT = %WM_USER + 4093
                %lblStatus  = 801
                
                CALLBACK FUNCTION DlgProc
                
                   DIM sInBuffer AS STRING
                
                   SELECT CASE CBMSG
                
                        CASE %WM_INITDIALOG
                
                         hSock = FREEFILE
                         TCP OPEN SERVER PORT 400 AS hSock
                            IF ERR THEN
                               MSGBOX "Couldn't create socket."
                            ELSE
                               TCP NOTIFY hSock, Accept TO gDlgMain AS %TCP_CONNECT
                               MSGBOX "Connected to Port " & STR$(400)
                            END IF
                         FUNCTION = 1
                
                
                'CONTROL SET TEXT gDlgMain, %lblStatus, FORMAT$(CBCTLMSG)
                
                      CASE %TCP_CONNECT
                
                        SELECT CASE LOWRD(CBLPARAM)
                     
                        CASE %FD_ACCEPT
                         'MSGBOX "accepted"
                         TCP Accept hSock AS gSock
                         TCP NOTIFY gSock, RECV CLOSE TO gDlgMain AS %TCP_CONNECT
                '         TCP NOTIFY gSock, CLOSE TO gDlgMain AS %TCP_CONNECT
                         'MSGBOX "Connected.."
                         'TCP SEND hSock, "Welcome.."
                
                
                        CASE %FD_READ
                         TCP RECV gSock, 2 , sInBuffer
                         MSGBOX sInBuffer
                
                
                        CASE %FD_CLOSE
                         TCP CLOSE gSock
                         'MSGBOX "Closed by remote.."
                
                        END SELECT
                
                        CASE %WM_DESTROY
                         TCP CLOSE gSock
                
                
                        CASE ELSE
                         CONTROL SET TEXT gDlgMain, %lblStatus, "Socket event received - %TCP_CONNECT LOWRD(lparam) =" + STR$(LOWRD(CBLPARAM))
                   END SELECT
                
                END FUNCTION
                
                
                FUNCTION PBMAIN AS LONG
                
                    DIALOG NEW 0, "simple tcp",,, 200, 200, %WS_MINIMIZEBOX OR %WS_CAPTION OR %WS_SYSMENU, 0 TO gDlgMain
                    CONTROL ADD LABEL, gDlgMain, %lblStatus, "[Ready]", 1, 1, 120, 40, %SS_RIGHT
                
                    DIALOG SHOW MODAL gDlgMain CALL DlgProc
                
                    'TCP CLOSE hSock
                
                    'CLOSE
                
                END FUNCTION


                [This message has been edited by Nathan Evans (edited June 29, 2001).]

                Comment


                • #9
                  I'm having a new problem now

                  I'm using TCP as a form of IPC, so i can communicate with another program
                  we are developing in VB. This PB program (below) will be the basis of the
                  IPC server. As this is the backend of the VB program, it needs to be hidden
                  or atleast not visible in the taskbar nor have a visible form.

                  The winsock code is working okay.. but as soon as i add the 'NOT %WS_VISIBLE'
                  to the DIALOG NEW's flags - the TCP server partially stops working. It will
                  accept connections okay, but it won't Echo anymore. (

                  I've experimented with Modeless forms, but they seem to terminate immediately?

                  Suggestions?

                  Code:
                  #COMPILE EXE
                  #DIM ALL
                  
                  #INCLUDE "WIN32API.INC"
                  #INCLUDE "WSCONST.INC"
                  
                  GLOBAL gDlgMain AS LONG
                  GLOBAL hSock    AS LONG
                  GLOBAL gSock    AS LONG
                  
                  %TCP_CONNECT = %WM_USER + 4093
                  %lblStatus  = 801
                  $msgTitle = "NC_IPC (TCP) Service"
                  
                  CALLBACK FUNCTION DlgProc
                  
                     LOCAL sInBuffer AS STRING
                  
                     SELECT CASE CBMSG
                  
                        
                        CASE %WM_INITDIALOG
                  
                           hSock = FREEFILE
                           TCP OPEN SERVER PORT 400 AS hSock TIMEOUT 90
                              IF ERR THEN
                                 MSGBOX "Couldn't create socket.",, $msgTitle
                              ELSE
                                 TCP NOTIFY hSock, Accept TO gDlgMain AS %TCP_CONNECT
                                 MSGBOX "Connected to Port " & STR$(400),,$msgTitle
                              END IF
                           FUNCTION = 1
                  
                        
                           
                        CASE %TCP_CONNECT
                  
                              SELECT CASE LOWRD(CBLPARAM)
                  
                                  CASE %FD_ACCEPT
                                  TCP Accept hSock AS gSock
                                   TCP NOTIFY gSock, RECV CLOSE TO gDlgMain AS %TCP_CONNECT
                                   'TCP SEND hSock, "Welcome.."
                  
                  
                                  CASE %FD_READ
                                  TCP RECV gSock, 1024, sInBuffer
                                   'MSGBOX sInBuffer
                                   TCP SEND gSock, sInBuffer '"Welcome.."
                  
                                  CASE %FD_CLOSE
                                  TCP CLOSE gSock
                  
                               END SELECT
                               FUNCTION = 1
                  
                        CASE %WM_DESTROY
                         
                         TCP CLOSE gSock
                         FUNCTION = 1
                         
                     END SELECT
                  
                  END FUNCTION
                  
                  
                  FUNCTION PBMAIN AS LONG
                  
                      DIALOG NEW 0, "NC_IPC (TCP) Service",,, 200, 200, %WS_MINIMIZEBOX OR %WS_CAPTION OR %WS_SYSMENU OR NOT %WS_VISIBLE, 0 TO gDlgMain
                      DIALOG SHOW MODAL gDlgMain CALL DlgProc
                      DIALOG DOEVENTS
                      
                  END FUNCTION

                  Comment


                  • #10
                    Try removing the NOT %WS_VISIBLE flag in DIALOG NEW, and make the window hidden by calling DIALOG SHOW STATE CBHNDL, %SW_HIDE in %WM_INITDIALOG.

                    When using a modeless dialog, you have to use a message pump that runs for the duration of the dialog... your code does just one DIALOG DOEVENTS call and then immediately ends!

                    Basically, a message pump for a modeless dialog would look like this:
                    Code:
                    'pseudocode
                    DIALOG SHOW MODELESS hDlg,...
                    DO
                      DIALOG DOEVENTS TO nCount&
                    LOOP WHILE nCOUNT&
                    For more information, please review the section in the help file on modeless dialogs.



                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment


                    • #11
                      Lance,

                      I tried removing the OR NOT %WS_VISIBLE, and add
                      DIALOG SHOW STATE gDlgMain, %SW_HIDE
                      to the %WM_INITDIALOG event.
                      This still left the dialog visible, however the TCP remained to work.

                      Next i tried setting the Dialog to MODELESS on the second line of my DDT code.
                      Then added the Doevents Loop code just after that - executed that, and the program
                      aborted immediately..

                      So i moved the Doevents Loop into the %WM_INITDIALOG in some desperate attempt,
                      but that failed too..

                      I'm feeling so dumb right now


                      - Nathan

                      [This message has been edited by Nathan Evans (edited June 30, 2001).]

                      Comment


                      • #12
                        Nathan,

                        I *believe* that, when you use a MODELESS dialog that
                        uses DIALOG DOEVENTS, it's CRITICAL that you put

                        FUNCTION = 1

                        at the end of the %WM_INITDIALOG section in your DIALOG's
                        callback function. Doing so removed the same problem with
                        some of my code.

                        G'luck!

                        P.S. There were a couple of my programs where I had to put
                        that code at the end of EACH of the SELECT CASE CBMSG
                        blocks...I don't know why those particular codes required
                        that, whereas others didn't.


                        ------------------
                        Clay C. Clear

                        http://www.v3space.com/a/a39/202/

                        [email protected]
                        [email protected]

                        Comment


                        • #13
                          Clay,

                          Thank you for your suggestion. It seems to have fixed it partly.
                          However, the program is only hidden from the screen, it is still visible
                          in the Taskbar. And the program constantly uses 100% CPU usage, because
                          of the Dialog Doevents Loop

                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          
                          #INCLUDE "WIN32API.INC"
                          #INCLUDE "WSCONST.INC"
                          
                          GLOBAL gDlgMain AS LONG
                          GLOBAL hSock    AS LONG
                          GLOBAL gSock    AS LONG
                          
                          %TCP_CONNECT = %WM_USER + 4093
                          %lblStatus  = 801
                          $msgTitle = "NC_IPC (TCP) Service"
                          
                          CALLBACK FUNCTION DlgProc
                          
                             LOCAL sInBuffer AS STRING
                          
                             SELECT CASE CBMSG
                          
                          
                                CASE %WM_INITDIALOG
                          
                                  DIALOG SHOW STATE gDlgMain, %SW_HIDE
                          
                                   hSock = FREEFILE
                                   TCP OPEN SERVER PORT 400 AS hSock TIMEOUT 90
                                      IF ERR THEN
                                         MSGBOX "Couldn't create socket.",, $msgTitle
                                      ELSE
                                         TCP NOTIFY hSock, Accept TO gDlgMain AS %TCP_CONNECT
                                         MSGBOX "Connected to Port " & STR$(400),,$msgTitle
                                      END IF
                                   FUNCTION = 1
                          
                                CASE %TCP_CONNECT
                          
                                      SELECT CASE LOWRD(CBLPARAM)
                          
                                          CASE %FD_ACCEPT
                                          TCP Accept hSock AS gSock
                                           TCP NOTIFY gSock, RECV CLOSE TO gDlgMain AS %TCP_CONNECT
                          
                                          CASE %FD_READ
                                          TCP RECV gSock, 1024, sInBuffer
                                           'MSGBOX sInBuffer
                                           TCP SEND gSock, sInBuffer '"Welcome.."
                          
                                          CASE %FD_CLOSE
                                          TCP CLOSE gSock
                          
                                       END SELECT
                                       FUNCTION = 1
                          
                                CASE %WM_DESTROY
                          
                                 TCP CLOSE gSock
                                 FUNCTION = 1
                          
                             END SELECT
                             FUNCTION = 1
                             
                          END FUNCTION
                          
                          
                          FUNCTION PBMAIN AS LONG
                          
                              DIALOG NEW 0, "NC_IPC (TCP) Service",,, 200, 200, %WS_MINIMIZEBOX OR %WS_CAPTION OR %WS_SYSMENU, 0 TO gDlgMain
                              DIALOG SHOW MODELESS gDlgMain CALL DlgProc
                              'DIALOG DOEVENTS
                              
                              LOCAL nCount AS LONG
                              DO
                                 DIALOG DOEVENTS TO nCount
                              LOOP WHILE nCOUNT
                              
                          END FUNCTION
                          I don't think a constantly running Loop is the way to go.. It is supposed
                          to be a Callback..

                          Thanks again,

                          -Nathan.

                          Comment


                          • #14
                            Nathan,

                            When I want to hide my DIALOG, and I'm using a MODELESS one, I
                            use the following in my WINMAIN:

                            GLOBAL hInst AS LONG
                            GLOBAL DB AS LONG
                            GLOBAL Flag1 AS LONG
                            FUNCTION WINMAIN(BYVAL CurInst AS LONG, _
                            BYVAL PrvInst AS LONG, _
                            CmdLine AS ASCIIZ PTR, _
                            BYVAL CmdShow AS LONG) AS LONG

                            hInst = CurInst

                            DIALOG NEW 0, "",,,1,1,,TO DB
                            DIALOG SHOW MODELESS DB CALL Proc()
                            DIALOG SHOW STATE DB, 0
                            START:
                            IF ISFALSE Flag1 THEN
                            DIALOG DOEVENTS
                            ELSE
                            DIALOG END DB
                            EXIT FUNCTION
                            END IF
                            GOTO START
                            END FUNCTION

                            Note that you probably won't be getting any better CPU usage
                            ratings, BUT, the code works...for me, at least.

                            Note that, the Taskbar will VERY briefly flash with the dialog's
                            opening, but after that will be hidden.

                            Also note that Flag1 is nothing but what it says - a Flag. It is
                            set to TRUE when the user selects whatever option (in my
                            programs, it's the "Close" option in the System Tray's Popup
                            Menu) is designated by the program to kill the DIALOG, in the
                            DIALOG's CALLBACK FUNCTION.

                            Regards,

                            Clay




                            ------------------
                            Clay C. Clear

                            http://www.v3space.com/a/a39/202/

                            [email protected]
                            [email protected]

                            Comment


                            • #15
                              I can't use Modeless dialogs at all if it excerts 100% cpu usage.

                              Do you know how to Hide a MODAL dialog completely? (taskbar and screen)

                              When run, I would prefer it not to flash on screen, then flash off - it looks bad.



                              Thanks,

                              -Nath.

                              ------------------

                              Comment


                              • #16
                                Nathan,

                                Sorry, can't help ya there. My own softwares are pretty much
                                designed to run in the System Tray, yet, based on the nominal
                                research I've done, it's impossible to have a System Tray icon
                                that responds to mouse clicks without having a Dialog. And, I
                                don't know of any way to create a Dialog that can be completely
                                hidden right away. there have been a number of posts on this BBS
                                regarding that issue - you might want to look them up and read
                                more about it from them.

                                P.S. Refer to the TRAY.BAS example also that ships with PB/DLL
                                6.0 - it enables you to hide a MODAL DIALOG so it just sits as
                                an icon in the Tray. But, even that code requires the DIALOG
                                to be initially displayed. However, it'll show you how to hide
                                a MODAL Dialog, once it's been displayed.


                                ------------------
                                Clay C. Clear

                                http://www.v3space.com/a/a39/202/

                                [email protected]
                                [email protected]

                                Comment


                                • #17
                                  Clay,

                                  THANK YOU!! Tray.bas had just the thing!

                                  If anyone interested: here's how i made my program run completely invisible,
                                  and not even flash on the taskbar.

                                  Thank you so much Clay!

                                  Code:
                                  #COMPILE EXE
                                  #DIM ALL
                                  
                                  #INCLUDE "WIN32API.INC"
                                  #INCLUDE "WSCONST.INC"
                                  
                                  GLOBAL gDlgMain AS LONG
                                  GLOBAL hSock    AS LONG
                                  GLOBAL gSock    AS LONG
                                  
                                  %TCP_CONNECT = %WM_USER + 4093
                                  %lblStatus  = 801
                                  $msgTitle = "NC_IPC (TCP) Service"
                                  
                                  CALLBACK FUNCTION DlgProc
                                  
                                     LOCAL sInBuffer AS STRING
                                  
                                     SELECT CASE CBMSG
                                  
                                      ShowWindow gDlgMain, %SW_HIDE
                                  
                                        CASE %WM_INITDIALOG
                                  
                                           hSock = FREEFILE
                                           TCP OPEN SERVER PORT 400 AS hSock TIMEOUT 90
                                              IF ERR THEN
                                                 MSGBOX "Couldn't create socket.",, $msgTitle
                                              ELSE
                                                 TCP NOTIFY hSock, Accept TO gDlgMain AS %TCP_CONNECT
                                                 'MSGBOX "Connected to Port " & STR$(400),,$msgTitle
                                              END IF
                                           FUNCTION = 1
                                  
                                        CASE %TCP_CONNECT
                                  
                                              SELECT CASE LOWRD(CBLPARAM)
                                  
                                                  CASE %FD_ACCEPT
                                                  TCP Accept hSock AS gSock
                                                   TCP NOTIFY gSock, RECV CLOSE TO gDlgMain AS %TCP_CONNECT
                                                    FUNCTION = 1
                                  
                                                  CASE %FD_READ
                                                  TCP RECV gSock, 1024, sInBuffer
                                                   'MSGBOX sInBuffer
                                                   TCP SEND gSock, sInBuffer '"Welcome.."
                                                    FUNCTION = 1
                                                    
                                                  CASE %FD_CLOSE
                                                  TCP CLOSE gSock
                                                    FUNCTION = 1
                                                    
                                               END SELECT
                                               FUNCTION = 1
                                  
                                        CASE %WM_DESTROY
                                  
                                         TCP CLOSE gSock
                                         FUNCTION = 1
                                  
                                     END SELECT
                                    
                                  END FUNCTION
                                  
                                  
                                  FUNCTION PBMAIN AS LONG
                                  
                                      DIALOG NEW 0, "NC_IPC (TCP) Service",,, 200, 200, NOT %WS_VISIBLE, 0 TO gDlgMain
                                      DIALOG SHOW MODAL gDlgMain CALL DlgProc
                                      ShowWindow gDlgMain, %SW_HIDE
                                      
                                  END FUNCTION
                                  ------------------

                                  Comment


                                  • #18
                                    Ummm... the ShowWindow call that is located in PBMAIN is not executed until your dialog is destroyed because the DIALOG SHOW MODEL statement is a "blocking" call...

                                    However, your code now hides the window for every single message that is send thorough the callback... that is not efficient.


                                    ------------------
                                    Lance
                                    PowerBASIC Support
                                    mailto:[email protected][email protected]</A>
                                    Lance
                                    mailto:[email protected]

                                    Comment


                                    • #19
                                      Yeah i have since removed that line, as it is not required.

                                      I was getting so frustrated, i just went Overkill in getting the darn thing to be invisible

                                      The line that really does the job is:

                                      Code:
                                      CALLBACK FUNCTION DlgProc
                                      
                                         LOCAL sInBuffer AS STRING
                                      
                                         SELECT CASE CBMSG
                                      
                                          ShowWindow gDlgMain, %SW_HIDE
                                      ...
                                      Thanks.

                                      -Nath

                                      Comment


                                      • #20
                                        Got another problem now..

                                        Just realised that when i succesfully hide the dialog..

                                        The TCP service stops responding, again..

                                        Comment

                                        Working...
                                        X