:::=== CLIENT.ASM ===:::
Template using RES file
include 'win32ax.inc'
IDD_MAINDLG = 101 All our resource identifiers
IDC_IP = 1015
IDC_CONNECT = 1016
IDC_MSG = 1017
IDC_SEND = 1018
IDC_VERSION = 1019
.data
hInstance dd ? Our instance handle
iPort equ 7272d Our port.. remember to change this in the server
iIP rb 17 Buffer for getting IP
wsaData WSADATA To store Winsock data
hSocket dd 0 Socket handle variable
sockAddr dw AF_INET sockAddr
sockAddrp dw ?
sockAddrip dd 0
sockAddrZ rb 8
sockAddrSize = $-sockAddr
randomBuf rb 1024 Random buffer for shit
sendBuf rb 1024 Send buffer
recvBuf rb 1024 Recieve buffer
.code
start:
invoke GetModuleHandle, 0 Should know all this shit..
mov [hInstance], eax
invoke DialogBoxParam, [hInstance], IDD_MAINDLG, 0, dlgProc, 0
invoke ExitProcess, 0
.end start
proc dlgProc, hDlg, uMsg, wParam, lParam
push ebx esi edi
cmp [uMsg], WM_COMMAND
je .wmcommand
cmp [uMsg], WM_CLOSE
je .wmclose
cmp [uMsg], WM_INITDIALOG
je .wminitdlg
cmp [uMsg], WM_DESTROY
je .wmclose
xor eax, eax
jmp .finish
.wminitdlg:
mov eax, 1
jmp .finish
.wmclose:
invoke EndDialog, [hDlg], 0
mov eax, 1
jmp .finish
.wmcommand:
mov eax, [wParam]
and eax, 0x0FFFF To get the LOWORD of wParam
cmp eax, IDC_CONNECT
je .connect
cmp eax, IDC_SEND
je .sendmessage
cmp eax, IDC_VERSION
je .version
mov eax, 0
jmp .finish
.connect:
invoke WSAStartup, 0002h, wsaData Same as in the server
test eax, eax
jnz .error
invoke socket, AF_INET, SOCK_STREAM, 0
cmp eax, -1
je .error
mov [hSocket], eax
invoke htons, iPort
mov [sockAddrp], ax
invoke GetDlgItemText, [hDlg], IDC_IP, iIP, 16 Get our IP from the text box
invoke inet_addr, iIP Convert it so our PCs can use it
mov [sockAddrip], eax Store it
invoke connect, [hSocket], sockAddr, 16 Connect to that IP
test eax, eax Did it work?
jnz .error No, display that we can't connect
invoke MessageBox, [hDlg], "Connection Successful..", "Message :", 0 Yup, show a success message
jmp .finish Connection finished
.error:
invoke MessageBox, [hDlg], "A connection could not be established", "Error :", 0 Error message
jmp .finish
.sendmessage:
cmp dword [hSocket], 0 Have we got a socket handle?
je .error No, show that we aren't connected
invoke GetDlgItemText, [hDlg], IDC_MSG, randomBuf, 1024 Yes, so get our message to be sent
invoke lstrcpy, sendBuf, "msgb" Put the msgb identifier at the beginning of it
invoke lstrcat, sendBuf, randomBuf And then put the message on
invoke send, [hSocket], sendBuf, 1024, 0 Send the identifier + message
jmp .finish Carry on...
.version:
cmp dword [hSocket], 0
je .error
invoke lstrcpy, sendBuf, "vers" Send a version identifier
invoke send, [hSocket], sendBuf, 1024, 0 Sent it
invoke recv, [hSocket], recvBuf, 1024, 0 Recieve the version string
invoke MessageBox, [hDlg], recvBuf, "Version Checker", 0 Then display it
jmp .finish Carry On
.finish:
pop edi esi ebx
ret
endp
section '.rsrc' data readable resource from 'resource.res' Import external .res file (created by a different program, such as VC++)
:::=== SERVER.ASM ===:::
include 'win32ax.inc'
.data
iPort equ 7272d Port to connect on, remember to change this in client too
wsaData WSADATA For holding our Winsock information
hSocket dd ? Variable to hold our listening socket handle
hRemoteSocket dd ? Variable to hold our connected socket handle
sockAddr dw AF_INET sockAddr Structure .. check API reference..
sockAddrp dw ?
sockAddrip dd 0
sockAddrZ rb 8
sockAddrSize = $-sockAddr Size of struct (16 bytes)
remoAddr dw ? Another sockAddr Structure
remoAddrp dw ?
remoAddrip dd ?
remoAddrZ rb 8
remoAddrSize dd ?
recvBuf rb 1024 Buffer to store recieved information
sendBuf rb 1024 Buffer to store information before it's sent
.code
start:
.bind_listen:
invoke WSAStartup, 0002h, wsaData Initiate Winsock to check version
test eax, eax Have we got the right version installed?
jnz .exit Nope, leave :/
invoke socket, AF_INET, SOCK_STREAM, 0 Create a socket for us
cmp eax, -1 Was it successfully created?
je .exit Nope, leave :/
mov [hSocket], eax Store the socket's handle
invoke htons, iPort Convert our Port to a value that our computer understands :P
mov [sockAddrp], ax And then store it..
invoke bind, [hSocket], sockAddr, sockAddrSize Bind our socket to this port
test eax, eax Did we bind it successfully?
jnz .exit Nope, leave :/
invoke listen, [hSocket], 5 Listen on this socket for incoming connections
test eax, eax Are we listening?
jnz .exit Nope, leave :/
.accept_loop:
mov [remoAddrSize], sockAddrSize Store our sockAddr structures size
invoke accept, [hSocket], remoAddr, remoAddrSize Accept any incoming connections
cmp eax, -1 Did we get one?
je .accept_loop Nah, keep trying
mov [hRemoteSocket], eax Yes we did, so store the new sockets handle
.recv_loop:
xor eax, eax Clear eax (mov eax, 0)
mov dword [recvBuf], eax Zero recvBuf (otherwise it keeps thinking we have data)
invoke recv, [hRemoteSocket], recvBuf, 1024, 0 Try and recieve any data being sent
cmp eax, 0 Did we get any?
je .end_recv_loop Nope, keep trying
cmp eax, -1 Is the connection lost?
je .conn_lost Yes, so reset everything
jmp .commands Check for commands
.end_recv_loop:
invoke closesocket, [hRemoteSocket] Close the remote socket
jmp .accept_loop Wait for connections again
.conn_lost:
invoke closesocket, [hSocket] Close our listening socket
invoke WSACleanup Cleanup our Winsock info up so we can reset everything
jmp .bind_listen Start all our connection shit again
.commands:
cmp dword [recvBuf], "msgb" Did we get a MessageBox command?
je .msgb We sure did, display it
cmp dword [recvBuf], "vers" Did we get a version check command?
je .vers Indeed, tell the client our version
jmp .recv_loop None of the above commands, so wait for the next one
.msgb: MESSAGEBOX COMMAND
mov eax, recvBuf+4 Skip past the "msgb" identifier
invoke MessageBox, 0, eax, "Message :", 0 Display the messagebox
jmp .recv_loop Wait for next command
.vers: VERSION CHECK COMMAND
invoke lstrcpy, sendBuf, "Version: DST v1.0b" Copy our version to our send buffer
invoke send, [hRemoteSocket], sendBuf, 1024, 0 Send the information to the client who we're connected to
jmp .recv_loop Wait for next command
.exit:
invoke closesocket, [hSocket] Close our listening socket
invoke closesocket, [hRemoteSocket] Close our connected socket
invoke WSACleanup Cleanup winsock
invoke ExitProcess, 0 Exit the server
.end start
:::=== RES_TEMP.ASM ===:::
Template using RES file
include 'win32ax.inc'
IDD_MAINDLG = 101 Control IDs
IDC_EXIT = 40001 Retrieved from the .h file automatically made from VC++
.data
hInstance dd ? Our instance handle
.code
start:
invoke GetModuleHandle, 0
mov [hInstance], eax
invoke DialogBoxParam, [hInstance], IDD_MAINDLG, 0, dlgProc, 0
invoke ExitProcess, 0
.end start
proc dlgProc, hDlg, uMsg, wParam, lParam
push ebx esi edi
cmp [uMsg], WM_COMMAND
je .wmcommand
cmp [uMsg], WM_CLOSE
je .wmclose
cmp [uMsg], WM_INITDIALOG
je .wminitdlg
cmp [uMsg], WM_DESTROY
je .wmclose
cmp [uMsg], WM_COMMAND
je .wmcommand
xor eax, eax
jmp .finish
.wminitdlg:
mov eax, 1
jmp .finish
.wmclose:
invoke EndDialog, [hDlg], 0
mov eax, 1
jmp .finish
.wmcommand:
mov eax, [wParam]
and eax, 0x0FFFF To get the LOWORD of wParam
cmp eax, IDC_EXIT
je .wmclose
mov eax, 0
jmp .finish
.finish:
pop edi esi ebx
ret
endp
section '.rsrc' data readable resource from 'resource.res' Import external .res file (created by a different program, such as VC++)