注意看 MSDN说明
accept Function
The accept function permits an incoming connection attempt on a socket.
Syntax
C++
SOCKET accept(
__in
SOCKET s,
__out
struct sockaddr *addr,
__inout
int *addrlen
);
Parameters
s [in]
A descriptor that identifies a socket that has been placed in a listening state with the listen function. The connection is actually made with the socket that is returned by accept.
addr [out]
An optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the sockaddr structure was created.
-------------------------------------------------------------------------------------------------------
addrlen [in, out] ;
in , out !!!!!!!!
An optional pointer to an integer that contains the length of structure pointed to by the addr parameter.
-----------------------------------------------------------------------------------------------------------
_bind proc ;绑定
local @stSin : sockaddr_in
local @stRetSin:sockaddr_in
local @dwRetSize:DWORD
pushad
invoke
socket,AF_INET,SOCK_STREAM,0
mov
hSocket,eax
mov
@stSin.sin_family,AF_INET
invoke
htons,8888
mov
@stSin.sin_port,ax
mov
@stSin.sin_addr,INADDR_ANY
invoke
bind,hSocket,addr @stSin,sizeof @stSin
.if
eax == SOCKET_ERROR
.else
invoke listen,hSocket,50
.While TRUE
mov @dwRetSize,sizeof sockaddr_in
invoke accept,hSocket,
addr @stRetSin,addr @dwRetSize
.break
.if
eax==INVALID_SOCKET
push ecx
invoke
CreateThread,NULL,0,offset _ServiceThread,eax,NULL,
esp
pop ecx
invoke
CloseHandle,eax
mov eax,@stRetSin.sin_addr
invoke inet_ntoa,eax
invoke OutputDebugString,eax
.endw
invoke
closesocket,hSocket
.endif
popad
ret
_bind
endp
注:
accept
如果不需要返回信息 直接
invoke accept,hSocket,NULL,NULL
如果需要返回信息 必须指定返回缓冲区 缓冲区是一个 sockaddr_in 结构 还必须指定 缓冲区大小
但是不可以
invoke accept,hSocket,addr @szBuff,sizeof @szBuff
因为 addrlen 必须是一个整型指针 int *addrlen
也就是说 必须赐予此指针 返回缓冲区的大小
然后函数在此指针返回 返回信息数据结构的大小
正确使用的情况如下:
Local @szBuff:sockaddr_in
Local @dwRetSize:DWORD
mov @dwRetSize,sizeof @szBuff
invoke accept,hSocket,addr @szBuff,addr @dwRetSize
[
本帖最后由 sll0807 于 2009-11-2 01:44 编辑 ]