                .386
                .MODEL FLAT,STDCALL
                locals
                jumps

                UNICODE = 0             ; Not Unicode

                include \w32\w32.inc

                IDC_STATIC      equ    -1
                IDD_DIALOG1     equ   100
                IDC_TARGETIP    equ  3000       ; Target IP
                IDC_PORT        equ  3004       ; Target PORT

                IDC_SERVE       equ  3002
                IDC_CONNECT     equ  3003

                IDC_REMOTE      equ  3100       ; Remote Text
                IDC_LOCAL       equ  3001       ; Local Text




.DATA

;[--------==========================================================--------];

_hInst          dd ?                    ;the hInst
_hWnd           dd ?                    ;Window Handle
_hIcon		dd ?

fmat1           db "%lu", 0             ; Unsigned decimal long.
fmat2           db "%lX", 0             ; Hexadecimal long.


_msg            MSG             <?>     ;The message Structure
_pnt            PAINTSTRUCT     <?>
DlgRect         RECT            <>
DlgWidth        dd              ?
DlgHeight       dd              ?
DesktopRect     RECT            <>

hndDlg          dd 0
sockh           dd 0
sockaddr        dw AF_INET              ; Family
sockport        db 0, 110               ; TCP Port
sockip          db 194,205,202,10       ; IP Address
                db 8 dup(0)             ; zero stuff.

socklen2        dd 16
sockaddr2       dw AF_INET              ; Family
                db 0, 0                 ; TCP Port
                db 0,0,0,0              ; IP Address
                db 8 dup(0)             ; zero stuff.

wsadata         db 1024 dup(0)

hostname        db 128 dup(0)
arg1            dd 0

inbuff          db 65535 dup(0)
inbuffp         dd 0
outbuff         db 65535 dup(0)

exitr           dd 0

header1         db 'ERROR!', 0

.CODE

;[--------==========================================================--------];

main:
        mov eax, offset founds


        call GetModuleHandle, NULL          ; Get the handle to our app
        mov _hinst, eax

        call DialogBoxParamA, _hinst, IDD_DIALOG1, 0, offset DlgProc, 0

        call ExitProcess                    ; Exit the main process

;[--------==========================================================--------];

        PUBLIC DlgProc

DlgProc proc STDCALL, __hwnd:DWORD, wmsg:DWORD, _wparam:DWORD, _lparam:DWORD
        USES ebx, edi, esi
        mov eax, wmsg
        cmp ax, WM_DESTROY
         je _wmdestroy
        cmp ax, WM_CLOSE
         je _wmdestroy
        cmp ax, WM_COMMAND
         je _wmcommand
        cmp ax, WM_INITDIALOG
         je _initdlg
        cmp ax, WM_PAINT
         je _paint
        cmp ax, 9999
         je netw
        xor eax,eax
        ret

netw:   call recv, sockh, inbuffp, 65535, 0
        add inbuffp, eax
        mov esi, inbuffp
        mov byte ptr [esi], 0
        call SetDlgItemText, __hwnd, IDC_REMOTE, offset inbuff

        mov ecx, inbuffp
        sub ecx, offset inbuff

        call SendDlgItemMessage, __hwnd, IDC_REMOTE, EM_SETSEL, ecx, ecx

        call SendDlgItemMessage, __hwnd, IDC_REMOTE, EM_SCROLLCARET, NULL, NULL

        xor eax, eax
        ret

;---------------------------------------;
_wmdestroy:
        mov exitr, 1
        call EndDialog, __hWnd, 0           ; End the Dialog
        call ExitProcess                    ; Exit Process
        ret
;---------------------------------------;
_wmcommand:
        cmp _wparam, IDC_CONNECT
         jne nconn
        mov eax, __hWnd
        mov hndDlg, eax
        call j_connect
        xor eax, eax
        ret
nconn:  cmp _wparam, IDC_SERVE
         jne nserv
        mov eax, __hWnd
        mov hndDlg, eax
        call j_serve
        xor eax, eax
        ret
nserv:
        cmp word ptr [offset _wparam], IDC_LOCAL
         jne nlocc
        cmp word ptr [offset _wparam + 2], EN_UPDATE
         jne nlocc
        call GetDlgItemText, __hwnd, IDC_LOCAL, offset outbuff, 65535

        add eax, offset outbuff
        cmp byte ptr [eax - 1], 10
         jne noentyet
        dec eax                         ; Must be a CR/LF at least!
        mov ecx, 1
loopk:  cmp eax, offset outbuff
         je founds
        cmp byte ptr [eax - 1], 10
         je founds
        dec eax
        inc ecx
        jmp loopk
founds:
        call send, sockh, eax, ecx, 0   ; Send last line

noentyet:
        xor eax, eax
        ret
nlocc:
        xor eax, eax
        ret


;---------------------------------------; Center dialog, put icon
_initdlg:

        call GetWindowRect, __hWnd, offset DlgRect                  ; Get DlgRect

        call GetDesktopWindow
        call GetWindowRect, eax, offset DesktopRect     ; Get Desktop rect

        mov eax, DlgRect.rc_bottom
        sub eax, DlgRect.rc_top
        mov DlgHeight, eax                  ; Dialog box height
        mov eax, DlgRect.rc_right
        sub eax, DlgRect.rc_left
        mov DlgWidth, eax                   ; Dialog box width
        mov ebx, DesktopRect.rc_bottom
        sub ebx, DlgHeight
        shr ebx, 1                          ; ebx = Start y
        mov ecx, DesktopRect.rc_right
        sub ecx, DlgWidth
        shr ecx, 1                          ; ecx = Start x

        call MoveWindow, __hWnd, ecx, ebx, DlgWidth, DlgHeight, 0   ; Centre

        ret
;---------------------------------------;
_paint:
        call BeginPaint, __hWnd, offset _pnt

        call EndPaint, __hwnd, offset _pnt
        ret
DlgProc endp

;[--------==========================================================--------];
j_connect proc
        call WSAStartup, 0101h, offset wsadata
        or eax, eax
         jnz erria

        call GetDlgItemInt, hndDlg, IDC_PORT, 0, FALSE
        xchg al, ah
        mov word ptr [offset sockport], ax

        call GetDlgItemText, hndDlg, IDC_TARGETIP, offset hostname, 128
        mov byte ptr [offset hostname + eax], 0
        call gethostbyname, offset hostname
        or eax, eax
         jz erria
        mov eax, dword ptr [eax + 12]
        mov eax, dword ptr [eax]
        mov ecx, dword ptr [eax]
        mov dword ptr [offset sockip], ecx

        call socket, AF_INET, Sock_stream, 0
        cmp eax, -1
         je erria
        mov sockh, eax
        call connect, sockh, offset sockaddr, 16
        cmp eax, -1
         je erria

        mov inbuffp, offset inbuff

        call WSAAsyncSelect, sockh, hndDlg, 9999, FD_READ
        ret

erria:  call MessageBox, 0, offset header1, offset header1, 0
        ret
j_connect endp

j_serve proc
        call WSAStartup, 0101h, offset wsadata
        or eax, eax
         jnz errib

        call GetDlgItemInt, hndDlg, IDC_PORT, 0, FALSE
        xchg al, ah
        mov word ptr [offset sockport], ax

        call gethostname, offset hostname, 128
        call gethostbyname, offset hostname
        or eax, eax
         jz errib
        mov eax, dword ptr [eax + 12]
        mov eax, dword ptr [eax]
        mov ecx, dword ptr [eax]
        mov dword ptr [offset sockip], ecx

        call socket, AF_INET, Sock_stream, 0
        cmp eax, -1
         je errib
        mov sockh, eax
        call bind, sockh, offset sockaddr, 16
        call listen, sockh, 1

trycon: call accept, sockh, offset sockaddr2, offset socklen2
        cmp eax, -1
         je trycon

        push eax
         call closesocket, sockh
        pop eax
        mov sockh, eax

        mov inbuffp, offset inbuff

        call WSAAsyncSelect, sockh, hndDlg, 9999, FD_READ
        ret

errib:  call MessageBox, 0, offset header1, offset header1, 0

        ret
j_serve endp

;[--------==========================================================--------];
jprintf proc p1:dword, p2:dword, p3:dword
        push p3
        push p2
        push p1
        call _wsprintf
        add esp, 12
        ret
jprintf endp
;---------------------------------------;
ends
        end main
