2010-10-05 5 views
0

Je suis avec un didacticiel de winsock dans la syntaxe de MASM appelé: Iczelion's Guide to Winsock ProgrammingErreur MASC winsock?

Je suis coincé, je reçois une erreur mais je ne sais pas comment la réparer. Chaque fois que j'essaie de me connecter à un serveur avec mon socket, je reçois une erreur WSANOTSOCK (une opération de socket est effectuée sur un autre socket)

Mais il n'y avait pas d'erreur lors de l'appel de WSAStartup() ou Socket(). Alors, comment cette erreur peut-elle être ici maintenant?

Voici le code que je suis actuellement à l'aide (je l'ai dit que je suivais le Guide de Iczelion Winsock programmation, mais je l'ai modefied pour répondre à mes besoins, je suis en train de créer un bot irc)

.386 
.model flat, stdcall 
option casemap: none 


include \masm32\include\windows.inc 
include \masm32\include\user32.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\shell32.inc 
include \masm32\include\wsock32.inc 
include \masm32\include\masm32.inc 
includelib \masm32\lib\shell32.lib 
includelib \masm32\lib\user32.lib 
includelib \masm32\lib\kernel32.lib 
includelib \masm32\lib\wsock32.lib 
includelib \masm32\lib\masm32.lib 




.data 

txt db "An error occured while calling WSAStartup",0 
txt1 db "An error occured while creating a socket",0 
txt2 db "An error occured while connecting",0 
capt db "SCHiM",0 
wsadata WSADATA <> 
hostname db "irc.corruptcode.org",0 
Port dd 6667 
NICK db "NICK SCHiMBot",0 
USER db "USER SCHIMR 8 * :SCHMRR",0 
CHANNEL db "/join #botts",0 
sin sockaddr_in <?> 

.data? 
sock dd ? 
;ErrorCode dd ? 
ErrorCode dd ? 

.code 
start: 
invoke WSAStartup, 101h,addr wsadata 

.if eax!=NULL ;An error occured if eax != null, because there's no return value for this api, if there's return, there's an error 

mov ErrorCode, eax 

push MB_OK 
push offset capt 
push offset txt 
push 0 
call MessageBoxA 


.else 

invoke socket,AF_INET,SOCK_STREAM,0  ; Create a stream socket for internet use 
invoke WSAGetLastError 


.if eax!=INVALID_SOCKET 
    mov sock,eax 
.else 
    invoke WSAGetLastError 

push MB_OK 
push offset capt 
push offset txt1 
push 0 
call MessageBoxA 
.endif 

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 
;Now we have a socket ready for use, we still have to be able to connect to somewere though... 
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 



mov sin.sin_family, AF_INET 
invoke htons, Port     ; convert port number into network byte order first 
mov sin.sin_port,ax     ; note that this member is a word-size param. 
invoke gethostbyname, addr hostname 
mov eax,[eax+12]    ; move the value of h_list member into eax 
mov eax,[eax]      ; copy the pointer to the actual IP address into eax 
mov eax,[eax]   
mov sin.sin_addr,eax ; copy IP address into eax 


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 
;Now That's done we can connect to a site! (an irc channel in this case) 
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 


invoke connect,socket,addr sin,sizeof sin 



.if eax==SOCKET_ERROR   

      invoke WSAGetLastError           



mov ErrorCode, eax 

push MB_OK 
push offset capt 
push offset txt2 
push 0 
call MessageBoxA 

invoke ExitProcess, NULL 



    .endif 

invoke send, socket,addr USER, 100, 0 
.if eax==SOCKET_ERROR 
push MB_OK 
push offset capt 
push offset txt2 
push 0 
call MessageBoxA 
invoke ExitProcess, NULL 
.else 

invoke send, socket,addr NICK, 100, 0 
invoke send, socket,addr CHANNEL, 100, 0 

.endif 








.endif 




     invoke ExitProcess, NULL 
end start 

Merci à l'avance

-Rick

+0

Ne pas supprimer vos questions lorsque vous avez reçu une réponse. Laissez la question intacte pour que la personne suivante avec un problème similaire puisse en bénéficier. – meagar

+0

J'avais déjà repéré mon erreur avant de voir la solution affichée, ce qui était correct. Je me souviendrai de laisser mes questions intactes à l'avenir – Rick

Répondre

0

Tout au long de cette partie:

invoke send, socket,addr USER, 100, 0 
.if eax==SOCKET_ERROR 
push MB_OK 
push offset capt 
push offset txt2 
push 0 
call MessageBoxA 
invoke ExitProcess, NULL 
.else 

invoke send, socket,addr NICK, 100, 0 
invoke send, socket,addr CHANNEL, 100, 0 

où vous avez "socket", vous avez l'intention d'avoir "sock" - tel quel, je crois qu'il utilise l'adresse (ou peut-être le premier mot de) la fonction socket au lieu de la valeur de socket que vous avez sauvegardé dans sock.

Si cela ne vous dérange pas de le dire, je pense que le code est un peu en désordre. Nettoyage un peu, j'ai obtenu ceci:

.code 
show_error proc caption:ptr byte, err_txt:ptr byte 
    invoke WSAGetLastError 
    mov ErrorCode, eax 
    invoke MessageBoxA, MB_OK, caption, err_txt, 0 
    ret 
show_error endp 

main proc 
    invoke WSAStartup, 101h,addr wsadata 

    .if eax==0 ; An error occured if eax != 0, because there's no return value for this api, if there's return, there's an error 
     invoke socket,AF_INET,SOCK_STREAM,0  ; Create a stream socket for internet use 
     .if eax!=INVALID_SOCKET 
      mov sock,eax 

      ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 
      ;Now we have a socket ready for use, we still have to be able to connect to somewere though... 
      ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 

      mov sin.sin_family, AF_INET 
      invoke htons, Port ; convert port number into network byte order first 
      mov sin.sin_port,ax ; note that this member is a word-size param. 
      invoke gethostbyname, addr hostname 

      mov eax,[eax+12] ; move the value of h_list member into eax 
      mov eax,[eax]  ; copy the pointer to the actual IP address into eax 
      mov eax,[eax]  ; copy IP address into eax 
      mov sin.sin_addr,eax 

      ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 
      ;Now That's done we can connect to a site! (an irc channel in this case) 
      ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 

      invoke connect, sock, addr sin, sizeof sin 
      .if eax!=SOCKET_ERROR 
       invoke send, sock, addr USER, 100, 0 
       .if eax!=SOCKET_ERROR 
        invoke send, sock, addr NICK, 100, 0 
        invoke send, sock, addr CHANNEL, 100, 0 
       .else 
        invoke show_error, offset capt, offset txt2 
       .endif 
      .else 
       invoke show_error, offset capt, offset txt2 
      .endif 
     .else 
      invoke show_error, offset capt, offset txt1 
     .endif 
    .else 
     invoke show_error, offset capt, offset txt 
    .endif 
    invoke ExitProcess, 0 
main endp 
end main