Announcement

Collapse
No announcement yet.

simple capture tcpip port 9100 - used for viewing internet printing in raw mode

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

  • simple capture tcpip port 9100 - used for viewing internet printing in raw mode

    Here is a quick and dirty program to catch what is sent over port 9100.
    It will keep rewriting over the same file of what comes in over that port.
    This is not tested much, actually a first jab at capturing printed material
    I just altered the echo server program from powerbasic sample programs.
    I just thought it would nice to catch what is sent to a raw printer port.
    Just a little more work is need to save multiple print jobs to multiple files.
    The output file is echoprnt.prn
    I ran this program on the same computer sending the printout from to a standard tcp/ip port number 9100 and it seemed to work fine.

    This program can also be used to eat up printed data for print testing purposes i would think.


    a powerbasic console program
    pbcc40 compiler used

    echoprnt.bas

    Code:
    'altered from the sample program called "echo server" from powerbasic
    
    
    #COMPILE CON
    #DIM ALL
    
    '------------------------------------------------------------------------------
    ' Include files and settings
    '
    %CCWIN = 1                ' Include GUI API calls
    %USEMACROS = 1
    #INCLUDE "WIN32API.INC"
    #INCLUDE "WS2_32.INC"
    
    %TCP_ACCEPT = %WM_USER + 4093  ' Any value larger than %WM_USER + 500
    %TCP_ECHO   = %WM_USER + 4094  ' Any value larger than %WM_USER + 500
    
    '------------------------------------------------------------------------------
    ' Global variables
    '
    GLOBAL hEcho    AS LONG
    GLOBAL hServer  AS LONG
    GLOBAL hwndTCP  AS LONG
    GLOBAL hThread  AS LONG
    
    
    '------------------------------------------------------------------------------
    ' Event logging routine
    '
    SUB LogEvent (BYVAL Buffer AS STRING)
        
        OPEN "echoprnt.prn" FOR BINARY AS #1000 BASE = 0
        PUT$ #1000,buffer
        CLOSE #1000
        PRINT "received data "+STR$(LEN(buffer))
    END SUB
    
    
    '------------------------------------------------------------------------------
    ' Callback function to handle events for the GUI window
    '
    FUNCTION TcpProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                      BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG
    
        STATIC hServer AS LONG
        STATIC hEcho   AS LONG
        LOCAL  sBuffer AS STRING
        LOCAL  sPacket AS STRING
    
        SELECT CASE wMsg
    
        CASE %WM_CREATE
            hServer = FREEFILE
            TCP OPEN SERVER PORT 9100 AS hServer TIMEOUT 5000
            IF ERR THEN
                sBuffer = "Couldn't create socket!"
            ELSE
                TCP NOTIFY hServer, ACCEPT TO hWnd AS %TCP_ACCEPT
                PRINT "Connected to Port 9100"
            END IF
            hEcho = %INVALID_SOCKET
            FUNCTION = 1
    
        CASE %TCP_ACCEPT
            SELECT CASE LO(WORD, lParam)
    
            CASE %FD_ACCEPT
                hEcho = FREEFILE
                TCP ACCEPT hServer AS hEcho
                TCP NOTIFY hEcho, RECV CLOSE TO hWnd AS %TCP_ECHO
    
            END SELECT
            FUNCTION = 1
    
        CASE %TCP_ECHO
            SELECT CASE LO(WORD, lParam)
    
            CASE %FD_READ
                IF hEcho <> %INVALID_SOCKET THEN
                    ' Perform a receive-loop until there is no data left (ie, the end of stream)
                    sBuffer = ""
                    sPacket = ""
                    DO
                      TCP RECV hEcho, 1024, sBuffer
                      sPacket = sPacket & sBuffer
                    LOOP UNTIL sBuffer = "" OR ISTRUE EOF(hEcho) OR ISTRUE ERR
    
                    ' Send it back!
               '     IF LEN(sBuffer) THEN TCP SEND hEcho, sPacket & " -> Received Ok!"
    
                    IF LEN(spacket) THEN LogEvent sPacket
                     ELSE
                    PRINT  "* FD_READ Error!"
                END IF
    
            CASE %FD_CLOSE
                TCP CLOSE hEcho
                hEcho = %INVALID_SOCKET
    
            END SELECT
            FUNCTION = 1
    
        CASE %WM_DESTROY
            TCP CLOSE hServer
    
        END SELECT
    
        FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
    
    END FUNCTION
    
    
    '------------------------------------------------------------------------------
    ' Create the GUI window to receive TCP event notification messages
    '
    FUNCTION MakeWindow () AS LONG
    
        LOCAL wce         AS WndClassEx
        LOCAL szClassName AS ASCIIZ * 64
        LOCAL hWnd        AS LONG
        LOCAL hInst       AS LONG
    
        STATIC registered AS LONG
    
        hInst = GetModuleHandle(BYVAL %NULL)
    
        IF ISFALSE registered THEN
            szClassName          = "PBTCPCOMM"
            wce.cbSize           = SIZEOF(wce)
            wce.style            = %CS_HREDRAW OR %CS_VREDRAW
            wce.lpfnWndProc      = CODEPTR(TcpProc)
            wce.cbClsExtra       = 0
            wce.cbWndExtra       = 0
            wce.hInstance        = hInst
            wce.hIcon            = %NULL
            wce.hCursor          = %NULL
            wce.hbrBackground    = %NULL
            wce.lpszMenuName     = %NULL
            wce.lpszClassName    = VARPTR(szClassName)
            wce.hIconSm          = %NULL
            RegisterClassEx wce
            registered = %TRUE
        END IF
    
        hWnd = CreateWindow(szClassName, _
                            "TCP Handler", _
                            %WS_OVERLAPPEDWINDOW, _
                            5, 5, 10, 10, _
                            %NULL, _
                            %NULL, _
                            hInst, _
                            BYVAL %NULL)
    
        IF ISFALSE hWnd THEN
            hWnd = GetLastError
        ELSE
            ShowWindow hWnd, %SW_HIDE
            UpdateWindow hWnd
        END IF
    
        FUNCTION = hWnd
    
    END FUNCTION
    
    
    '------------------------------------------------------------------------------
    ' Spawn a thread to create/own the GUI window and run a thread message pump.
    ' Each thread must operate it's own pump the GUI windows it owns.
    '
    FUNCTION WindowThread (BYVAL nIgnored AS LONG) AS LONG
    
        LOCAL Msg AS tagMsg
    
        hwndTCP = MakeWindow
    
        DO WHILE IsWindow(hwndTCP) AND GetMessage(Msg, %NULL, 0, 0)
            TranslateMessage Msg
            DispatchMessage Msg
        LOOP
    
    END FUNCTION
    
    
    '------------------------------------------------------------------------------
    ' Main program entry point...
    '
    FUNCTION PBMAIN () AS LONG
    
        ' Process socket messages in a separate thread
    
        THREAD CREATE WindowThread(%NULL) TO hThread
    
        ' If the user presses the Esc key, exit the server
        PRINT "Press ESC to end the ECHO Server"
        DO UNTIL WAITKEY$ = $ESC
        LOOP
    
        SendMessage hwndTCP, %WM_CLOSE, 0, 0
    
    END FUNCTION
    Attached Files
    Last edited by Paul Purvis; 11 Aug 2009, 08:16 PM.
    p purvis

  • #2
    This program is not very cpu friendly, maybe some sleep statements in it would improve.

    No alteration yet on my part, just warning those that use it.
    p purvis

    Comment

    Working...
    X