Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP/IP Example Demo for LOCAL HOST/CLIENT Applications
#15
@Spriggsy

There has to be a better way?!

I thought for fun I'd try to put in a Windows API method of making the window ready to send the message active. The receiving window would become active when it received the message. So now the user just types. No need to click to activate the window anymore, before typing.

So SetActiveWindow and AllowSetForegroundWindow didn't work in this case, so I used a hack that hides, min. and restores the window to mimic the effect. Surely there must be some simple method I'm misssing that could accomplish the some thing without the unwanted special effects. Any ideas?

Name and save as messenger_host
Code: (Select All)
DECLARE DYNAMIC LIBRARY "user32"
    FUNCTION FindWindowA%& (BYVAL ClassName AS _OFFSET, WindowName$) 'handle by title
    FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG) 'maximize process
    FUNCTION SetForegroundWindow%& (BYVAL hwnd AS _OFFSET) 'set foreground window process(focus)
    REM FUNCTION SetActiveWindow& (BYVAL hwnd AS _OFFSET)
    FUNCTION GetForegroundWindow%& 'Find currently focused process handle
END DECLARE

_SCREENMOVE 0, 0
title$ = "Messenger_Host"
_TITLE (title$)
_DELAY .1

_SCREENMOVE 0, 0 ' Set up this host window to the left of your desktop.
WIDTH 60, 25
DIM AS STRING host_msg, client_msg
DO
    IF initiate = 0 THEN ' This only needs to be performed once, to open the client window.
        DO UNTIL x ' Stay in loop until window determines if it is the host or client window.
            x = _OPENCLIENT("TCP/IP:1234:localhost") ' Used to establish a TCP/IP routine.
            IF x = 0 THEN
                x = _OPENHOST("TCP/IP:1234") ' Note the host and clinet must have the same 1234 I.D. number.
                a$ = "Opening as host." ' x channel is now open and this window becomes the host.
            ELSE
                a$ = "Opening as client." ' Should not go here for this demo.
            END IF
            PRINT a$
        LOOP
        SHELL _DONTWAIT "messenger_client.exe" ' Open the client window.
        initiate = -1 ' Switches this block statement off for all subsequent loops.
    END IF

    IF z = 0 THEN ' Initiates an open channel number when zero.
        DO
            z = _OPENCONNECTION(x) ' Checks if host is available to transfer data.
        LOOP UNTIL z
        PRINT "Connection established."
        _DELAY 1
        LOCATE 2: PRINT SPACE$(_WIDTH * 2) ' Remove these lines.
        LOCATE 3, 1
        GOSUB focus ' Sends focus back to host window.
    END IF

    ' Okay, time to input something on the host that will be communicated to the client.
    LINE INPUT "Message to client: "; host_msg: PRINT

    PUT #z, , host_msg ' Input is now entered into TCP/IP routine.

    DO
        GET #z, , client_msg
    LOOP UNTIL LEN(client_msg) ' Exits loop when a return msg is received.

    PRINT "Message from client: "; client_msg: PRINT

    host_msg = "": PUT #z, , host_msg$ ' Now put our client value back into the routine. Failure to do so would result in the client not waiting in the GET #x DO/LOOP.
    _KEYCLEAR ' Prevents typing before ready.

    GOSUB focus
LOOP

focus:
DO UNTIL hwnd%&
    _LIMIT 10
    hwnd%& = FindWindowA(0, title$ + CHR$(0))
LOOP
FGwin%& = GetForegroundWindow%& 'get current process in focus.
_DELAY .1
IF FGwin%& <> hwnd%& THEN
    y& = ShowWindow&(hwnd%&, 0)
    y& = ShowWindow&(hwnd%&, 2)
    y& = ShowWindow&(hwnd%&, 9)
    DO
        _LIMIT 10
        REM focus%& = SetForegroundWindow%&(hwnd%&) 'Set focus, but not needed with ShowWindows.
        FGwin%& = GetForegroundWindow%&
    LOOP UNTIL FGwin%& = hwnd%&
END IF
RETURN

Name and save as messenger_client
Code: (Select All)
DECLARE DYNAMIC LIBRARY "user32"
    FUNCTION FindWindowA%& (BYVAL ClassName AS _OFFSET, WindowName$) 'handle by title
    FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG) 'maximize process
    FUNCTION SetForegroundWindow%& (BYVAL hwnd AS _OFFSET) 'set foreground window process(focus)
    REM FUNCTION SetActiveWindow& (BYVAL hwnd AS _OFFSET)
    FUNCTION GetForegroundWindow%& 'Find currently focused process handle
END DECLARE

title$ = "Messenger_Client"
_TITLE (title$)
_DELAY .1

DIM AS STRING host_msg, client_msg
_SCREENMOVE 600, 0 ' Set up this client window next to your host window.
WIDTH 50, 25
x = _OPENCLIENT("TCP/IP:1234:localhost") ' Used to establish a TCP/IP routine.
PRINT "Opened as client.": PRINT
DO UNTIL x = 0 ' Prevents running if this app is opened without using host.
    DO
        _LIMIT 30
        GET #x, , host_msg ' Waits until it receives message sent from the host.
    LOOP UNTIL LEN(host_msg)

    PRINT "Message from host: "; host_msg
    PRINT
    _KEYCLEAR ' Prevents typing before ready.

    GOSUB focus

    LINE INPUT "Message to host: "; client_msg: PRINT

    PUT #x, , client_msg
LOOP
END

focus:
DO UNTIL hwnd%&
    _LIMIT 10
    hwnd%& = FindWindowA(0, title$ + CHR$(0))
LOOP
FGwin%& = GetForegroundWindow%& 'get current process in focus.
_DELAY .1
IF FGwin%& <> hwnd%& THEN
    y& = ShowWindow&(hwnd%&, 0)
    y& = ShowWindow&(hwnd%&, 2)
    y& = ShowWindow&(hwnd%&, 9)
    DO
        _LIMIT 10
        FGwin%& = GetForegroundWindow%&
    LOOP UNTIL FGwin%& = hwnd%&
END IF
RETURN

Pete
Reply


Messages In This Thread
RE: TCP/IP Example Demo for LOCAL HOST/CLIENT Applications - by Pete - 10-28-2022, 10:43 AM



Users browsing this thread: 6 Guest(s)