Author Topic: Basic Win32 API Socket Server Client  (Read 11293 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Basic Win32 API Socket Server Client
« on: July 24, 2013, 12:46:56 PM »
Hello!

This is Basic Win32 API Socket Server Client.

Things you find here:

  • Create Basic Win32 API Socket Client
  • And that's it :D

This post goes along with previous post named "Basic Win32 API Socket Server",
this client will connect to 127.0.0.1:36665 and expects a message from server,
and will display it upon receival.

Code is managed into seperate files, they are:

  • client.asm
  • WSAStartup.asm
  • socket.asm
  • connect.asm
  • recv.asm
  • closesocket.asm
  • WSACleanup.asm

Main compile file is "client.asm",
all others are include files.



Create Basic Win32 API Socket Client

Create file "client.asm" and put there following code:

Code: [Select]
[BITS 32]

; Import function macro
%MACRO dllimport 2
IMPORT %2 %1
EXTERN %2
%ENDMACRO

; Import some function
dllimport kernel32.dll, ExitProcess
dllimport kernel32.dll, Sleep
dllimport user32.dll, MessageBoxA
dllimport Ws2_32.dll, socket
dllimport Ws2_32.dll, WSAStartup
dllimport Ws2_32.dll, WSACleanup
dllimport Ws2_32.dll, WSAGetLastError
dllimport Ws2_32.dll, inet_addr
dllimport Ws2_32.dll, htons
dllimport Ws2_32.dll, closesocket
dllimport Ws2_32.dll, connect
dllimport Ws2_32.dll, recv
dllimport user32.dll, wsprintfA

; Socket constants
AF_INET equ 2
SOCK_STREAM equ 1
IPPROTO_TCP equ 6

; MessageBox constants
MB_OK  equ 0x00000000
MB_ICONEXCLAMATION equ 0x00000030

; Macro to display WSA error code and custom error message
%macro display_wsa_error 1
call [WSAGetLastError]
push dword eax
push dword wsaErrorFormat
push dword wsaErrorBuffer
call [wsprintfA]
pop eax
pop eax
pop eax

push dword MB_OK + MB_ICONEXCLAMATION
push dword wsaErrorBuffer
push dword %1
push dword 0
call [MessageBoxA]
%endmacro

[SEGMENT .BSS USE32]

wsaData resb 400
socketHandle resb 4
sockAddr resb 16
wsaErrorBuffer resb 256
acceptHandle resb 4

txtRecvBuffer resb 256

[SEGMENT .DATA USE32]

text_error_WSAStartup db "WSAStartup error",0
text_error_socket db "socket error",0
text_error_WSACleanup db "WSACleanup error",0
text_error_closesocket db "closesocket error",0
text_error_connect db "connect error",0
text_error_recv db "recv error",0

text_received_title db "Received message from server:",0

startupSuccessState db 0
socketCreationState db 0

wsaErrorFormat db "wsaErrorCode: %d",0

ipAddress db "127.0.0.1",0

[SEGMENT .TEXT USE32]

..start:

%include "WSAStartup.asm"

%include "socket.asm"

%include "connect.asm"

%include "recv.asm"

; Display received message

push dword 0
push dword text_received_title
push dword txtRecvBuffer
push dword 0
call [MessageBoxA]

quitProgram:

%include "closesocket.asm"

%include "WSACleanup.asm"

push dword 0
call [ExitProcess]

Create file "WSAStartup.asm" and put there following code:

Code: [Select]


; # WSAStartup - initiates use of the Windows Sockets DLL by a process
push dword wsaData
push dword 0x202
call [WSAStartup]
cmp eax,0
je WSAStartupSuccess

; ERROR --------------------------------
display_wsa_error text_error_WSAStartup
jmp quitProgram

WSAStartupSuccess:
mov byte [startupSuccessState],byte 1

Create file "socket.asm" and put there following code:

Code: [Select]

; # socket - creates a socket which is bound to a specific service provider
push dword IPPROTO_TCP
push dword SOCK_STREAM
push dword AF_INET
call [socket]
mov [socketHandle],eax
cmp eax,dword 0
jne socketSuccess

; ERROR --------------------------
display_wsa_error text_error_socket
jmp quitProgram

socketSuccess:
mov byte [socketCreationState], byte 1

Create file "connect.asm" and put there following code:

Code: [Select]

; # connect - establishes a connection to a peer

; Family
mov [sockAddr],dword AF_INET

; Port
push dword 36665
call [htons]
mov [sockAddr+2],eax

; Address
push dword ipAddress
call [inet_addr]
mov [sockAddr+4],eax

push dword 16
push dword sockAddr
push dword [socketHandle]
call [connect]

cmp eax,0
je .connectSuccess

; ERROR --------------------------
display_wsa_error text_error_connect
jmp quitProgram

.connectSuccess:

Create file "recv.asm" and put there following code:

Code: [Select]

; # recv - receives data from a socket

push dword 0
push dword 256
push dword txtRecvBuffer
push dword [socketHandle]
call [recv]
cmp eax,0
jge .receiveSuccess

; ERROR --------------------------
display_wsa_error text_error_recv
jmp quitProgram

.receiveSuccess:

Create file "closesocket.asm" and put there following code:

Code: [Select]

; # closesocket - closes a socket
cmp byte [socketCreationState],byte 1
jne .noSocket

push dword [socketHandle]
call [closesocket]
cmp eax,0
je .closeSocketSuccess

; ERROR --------------------------
display_wsa_error text_error_closesocket

.noSocket:
.closeSocketSuccess:

Create file "WSACleanup.asm" and put there following code:

Code: [Select]

; # WSACleanup - terminates use of the Windows Sockets DLL
cmp byte [startupSuccessState],byte 1
jne .noStartup

call [WSACleanup]
cmp eax,0
je .cleanupSuccess

; ERROR --------------------------
display_wsa_error text_error_WSACleanup

.noStartup:
.cleanupSuccess:



And that's it :D

NASM cmd: "nasm -fobj client.asm"
ALINK cmd: "alink -c -oPE -subsys windows client.obj"

Now start server.exe, run client.exe, and you should receive a message from server!

Tested, works!

Done, Bye! :)

Edit: Added attachment, available for download
« Last Edit: July 25, 2013, 03:06:05 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.