2010-10-05 42 views
0

我跟着MASM的語法中的一個winsock教程叫:Iczelion的Winsock編程指南MASM winsock錯誤?

我卡住了,我收到一個錯誤,但我不知道如何解決它。 的事情是,每次我嘗試連接到我的插座的服務器,我收到一個WSANOTSOCK錯誤(套接字opperation是在一些預製不是一個套接字)

但沒有調用WSAStartup時錯誤()或Socket()。那麼現在這個錯誤怎麼會在這裏呢?

這裏是我目前使用的代碼(我說我也跟着Iczelion指南編程的Winsock,但我modefied它來滿足我的需求,我試圖創建一個IRC bot)

.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 

在此先感謝

-Rick

+0

當你收到答案時不要刪除你的問題。保持問題不變,以便下一個有類似問題的人可以從中受益。 – meagar 2010-10-05 17:53:53

+0

我在看到發佈的解決方案之前就已經發現了我的錯誤,但是這是正確的。我會記住將來會保留我的問題 – Rick 2010-10-05 18:30:31

回答

0

在本部分:

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 

你有「套接字」,你打算有「襪子」 - 我相信它使用socket功能的地址(或者可能是第一個雙字),而不是保存在sock中的套接字值。

如果你不介意我這麼說,我認爲代碼是一團糟。清理一下,我得到了這個:

.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