News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

confuse with winsock programming

Started by elmo, November 21, 2010, 12:59:09 PM

Previous topic - Next topic

elmo

This is the first time I making program about SOCKET PROGRAMMING.
I think SOCKET PROGRAMMING only for making chat program.
Is it right?
If right, the following code is to show string "hi there" on the computer who have IP="127.0.0.1" via port 256.
But it fail to show string "hi there" on computer "127.0.0.1"
would you like to help me to solve this problem? Thank you..



.data
port dd 256

.data?
wsdata WSADATA <>
sa sockaddr_in <>
sock dd ?



.code
start:
WndProc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam :DWORD
.if uMsg == WM_CREATE
;SOCKET PROGRAMMING
;1.INITIALIZE WINSOCK LIB
INVOKE WSAStartup,101h,addr wsdata ;USE WINSOCK VERSION1.1
;wsdata: CURRENT WINSOCK LIB WILL FILL WSADATA STRUCTURE
;INVOKE WSAStartup,200h,addr wsdata ;USE WINSOCK VERSION2
.if eax!=NULL
;ERROR
ret
.else
;INITIALIZE SUCCESS
;2.CREATE SOCKET
;CREATE AT LEAST 1 SOCKET TO BE USED AS THE COMMUNICATION DEVICE FROM YOUR END
invoke socket,PF_INET,SOCK_STREAM,0 ;af=PF_INET
;CREATE SOCKET WITH TYPE=STREAM
;BECAUSE WE USE af=PF_INET,WE USE PROTOCOL=0
;invoke socket,PF_INET,SOCK_DGRAM,0 ;CREATE SOCKET WITH TYPE=DATAGRAM
.if eax!=INVALID_SOCKET
mov sock,eax ;RETURN THE SOCKET DESCRIPTOR
;THIS SOCKET OPERATE IN BLOCKING MODE (DEFAULT)
;3. OPERATE SOCKET IN NON-BLOCKING MODE
invoke WSAAsyncSelect, sock, hWin, WM_USER+100, FD_CONNECT or FD_READ or FD_ACCEPT or FD_OOB or FD_WRITE or FD_CLOSE
;hWin WILL RECEIVE NOTIFICATION OF WINSOCK EVENT (FD_READ/FD_WRITE/FD_OOB/FD_ACCEPT/FD_CONNECT/FD_CLOSE)
;FD_READ=READY FOR READ
;FD_WRITE=READY FOR WRITE
;FD_OOB=THE ARRIVAL OF OUT-OF-BAND DATA
;FD_ACCEPT=INCOMING CONNECTION
;FD_CONNECT=COMPLETED CONNECTION
;FD_CLOSE=CLOSE SOCKET
.if eax==SOCKET_ERROR
INVOKE WSAGetLastError
.else
;SUCCESFULL TO OPERATE SOCKET IN NON-BLOCKING MODE
;4.CONNECT TO A REMOTE SOCKET
;FILL SOCKADDR_IN STRUCTURE
mov sa.sin_family, AF_INET ;4a.MUST USE AF_INET HERE
invoke htons, port ;4b.CONVERT PORT VALUE TO NETWORK BYTE ORDER (BIG ENDIAN)
mov sa.sin_port,ax ;THE PORT THAT THE SOCKET WILL USE TO COMMUNICATE WITH THE REMOTE SOCKET
szText szIp,"127.0.0.1"
invoke inet_addr, addr szIp ;4c.CONVERT IPADDRESS TO NETWORK BYTE ORDER
mov sa.sin_addr, eax ;IPADDRESS OF THE REMOTE HOST

;IF WANT TO CONNECT TO URL
;mov sa.sin_family, AF_INET ;4a.MUST USE AF_INET HERE
;invoke htons, port ;4b.CONVERT PORT VALUE TO NETWORK BYTE ORDER (BIG ENDIAN)
;mov sa.sin_port,ax ;THE PORT THAT THE SOCKET WILL USE TO COMMUNICATE WITH THE REMOTE SOCKET
;invoke gethostbyname,SADD("www.cat22.net) ;4c.RETRIEVE THE IPADDRESS OF THE HOST
;mov eax,[eax+12]
;mov eax,[eax]
;mov eax,[eax]
;mov sa.sin_addr, eax ;IPADDRESS OF THE REMOTE HOST


invoke connect, sock, addr sa, sizeof sa
.if eax==SOCKET_ERROR ;BECAUSE WE USE NON-BLOCKING MODE, IT WILL RETURN SOCKET_ERROR
invoke WSAGetLastError
.if eax==WSAEWOULDBLOCK
;THE OPERATION IS IN PROGRESS
.else
;REAL ERROR
.endif
.endif
.endif
.else
;CREATE SOCKET FAILED
invoke WSAGetLastError
.endif
.endif






.elseif uMsg==WM_USER+100 ;MESSAGE THAT SPECIFIED IN WASAsynSelect
mov eax,lParam
.if ax==FD_CONNECT ;THE LOW WORD OF LPARAM CONTAIN WINSOCK EVENT
shr eax,16 ;THE ERROR CODE IS IN THE HIGH WORD OF LPARAM
.if ax==NULL
szText sendstring,"hi there"
invoke lstrlen, addr sendstring
invoke send, sock, addr sendstring, eax, 0 ;FLAG PARAMETER=0 BECAUSE WE NOT USE MSG_DONTROUTE NOR MSG_OOB
;WE USE "SEND" FUNC BECAUSE WE USE SOCKET TYPE=STREAM (invoke socket,PF_INET,SOCK_STREAM,0)
;"SEND" FUNC WILL SEND DATA ON STREAM SOCKET TO REMOTE SOCKET IN (STEP4.CONNECT TO A REMOTE SOCKET)
invoke MessageBox,0,addr sendstring,SADD("WINSOCK"),0
;invoke sendto, sock, addr szSockStr, eax, 0 ;FLAG PARAMETER=0 BECAUSE WE NOT USE MSG_DONTROUTE NOR MSG_OOB
; ;WE USE "SENDTO" FUNC BECAUSE WE USE SOCKET TYPE=DATAGRAM (invoke socket,PF_INET,SOCK_DGRAM,0)
; ;"SENDTO" FUNC WILL SEND DATA ON DATAGRAM SOCKET TO REMOTE SOCKET IN (STEP4.CONNECT TO A REMOTE SOCKET)
.if eax==SOCKET_ERROR
.else
;THE ACTUAL NUMBER OF BYTE SENT IS RETURNED IN EAX
;NB: THIS NUMBER MAY NOT EQUAL TO LEN OF szSockStr
.endif
.else
;AN ERROR OCCURED
.endif
.elseif ax==FD_READ
shr eax,16 ;THE ERROR CODE IS IN THE HIGH WORD OF LPARAM
.if ax==NULL
;
.else
;AN ERROR OCCURED
.endif
.elseif ax==FD_READ
shr eax,16 ;THE ERROR CODE IS IN THE HIGH WORD OF LPARAM
.if ax==NULL
;
.else
;AN ERROR OCCURED
.endif
.elseif ax==FD_ACCEPT
shr eax,16 ;THE ERROR CODE IS IN THE HIGH WORD OF LPARAM
.if ax==NULL
;
.else
;AN ERROR OCCURED
.endif
.elseif ax==FD_OOB
shr eax,16 ;THE ERROR CODE IS IN THE HIGH WORD OF LPARAM
.if ax==NULL
;
.else
;AN ERROR OCCURED
.endif
.elseif ax==FD_WRITE
shr eax,16 ;THE ERROR CODE IS IN THE HIGH WORD OF LPARAM
.if ax==NULL
;
.else
;AN ERROR OCCURED
.endif
.elseif ax==FD_CLOSE
shr eax,16 ;THE ERROR CODE IS IN THE HIGH WORD OF LPARAM
.if ax==NULL
;
.else
;AN ERROR OCCURED
.endif
.endif
.endif
invoke DefWindowProc,hWin,uMsg,wParam,lParam

ret
WndProc endp
end start
be the king of accounting programmer world!

dedndave

127.0.0.1 is local host
try using a valid IPA