Monday, August 13, 2007

Conexión winsock a un puerto (masm9).

;sock0h.asm
;11AGO2007



; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

.586p ; create 32 bit code
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\wsock32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\wsock32.lib



.data
caption db "Caption",0
text db "Text",0
wsadata WSADATA <>
sock dd 0
sockstruct sockaddr_in <>
ipaddress db "127.0.0.1",0
port dd 80


.code

start:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
;
invoke WSAStartup, 2h, addr wsadata
.if eax!=NULL
jmp exitprog
.else
invoke socket, AF_INET, SOCK_STREAM, 0
.if eax!=INVALID_SOCKET
mov sock,eax
mov sockstruct.sin_family,AF_INET
invoke htons, port
mov sockstruct.sin_port,ax
invoke inet_addr, addr ipaddress
mov sockstruct.sin_addr,eax
invoke connect, sock, addr sockstruct, sizeof sockstruct
.if eax==SOCKET_ERROR
jmp closesock
.else
invoke MessageBox, NULL, addr text, addr caption, MB_OK
.endif
.else
jmp wscleanup
.endif
.endif

closesock:
invoke closesocket, sock
.if eax==SOCKET_ERROR
jmp wscleanup
.endif

wscleanup:
invoke WSACleanup
.if eax==SOCKET_ERROR
jmp exitprog
.endif

exitprog:
invoke ExitProcess, NULL
;
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start



;EOS