📄 client.asm
字号:
;***********************************
;文件:Client.asm *
;功能:演示线程的使用的Socket编程 *
; ——客户端 *
;***********************************
.386p
.model flat,stdcall
extrn MessageBoxA:proc
extrn ExitProcess:proc
extrn WSAStartup:proc
extrn htons:proc
extrn gethostbyname:proc
extrn getprotobyname:proc
extrn socket:proc
extrn connect:proc
extrn recv:proc
extrn closesocket:proc
AF_INET = 2
PF_INET = AF_INET
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_RDM = 4
SOCK_SEQPACKET= 5
NULL = 0
MB_OK = 0
PORT = 1357
hostent struc
h_name dd ?
h_aliases dd ?
h_addrtype dw ?
h_length dw ?
h_addr_list dd ?
;h_addr h_addr_list[0] /* address, for backward compat */
ends
protoent struc
p_name dd ?
p_aliases dd ?
p_proto dw ?
dw ?
ends
sockaddr_in struc
sin_family dw ?
sin_port dw ?
sin_addr dd ?
sin_zero db 8 dup (?)
ends
WSADESCRIPTION_LEN = 256
WSASYS_STATUS_LEN = 128
WSADATA struc
wVersion dw ?
wHighVersion dw ?
szDescription db WSADESCRIPTION_LEN+1 dup (0)
szSystemStatus db WSASYS_STATUS_LEN+1 dup (0)
iMaxSockets dw ?
iMaxUdpDg dw ?
lpVendorInfo dd ?
ends
.data
Caption db 'Client',0
proto db 'tcp',0
host db 'localhost',0 ;20 dup (0)
buf db 1000 dup (0)
align 4
ptrh dd ? ;hostent<>
ptrp dd ? ;protoent<>
sd dd ?
port dd ?
align 4
sad sockaddr_in<>
align 4
wsaData WSADATA<>
.code
main:
;为使用WS2_32.DLL初始化
call WSAStartup,0101H,offset wsaData
mov sad.sin_family,AF_INET
mov port,PORT
mov eax,port
push eax
call htons
mov sad.sin_port,ax
call gethostbyname,offset host ;取服务器
mov ptrh,eax
mov ebx,offset sad
add ebx,sin_addr
movsx ecx,[eax].h_length
add eax,h_addr_list
mov eax,[eax]
mov eax,[eax]
call CopyMemory,ebx,eax,ecx
call getprotobyname, offset proto ;取协议
mov ptrp,eax
movzx eax,[eax].p_proto
call socket,PF_INET,SOCK_STREAM,eax
mov sd,eax
call connect,eax,offset sad,size sad
mov esi,offset buf
recvnext:
push esi
call recv,sd,offset esi,1000,0 ;接收
pop esi
add esi,eax
or eax,eax
jnz recvnext
call MessageBoxA,NULL,offset buf,offset Caption,MB_OK
MainEnd:
call closesocket,sd
call ExitProcess,0
CopyMemory proc des:DWORD,src:DWORD,len:DWORD
mov edi,des
mov esi,src
mov ecx,len
cld
rep movsb
ret
CopyMemory endp
end main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -