MASM winsock 错误?

发布于 2024-09-26 04:02:10 字数 3316 浏览 3 评论 0原文

我正在遵循 MASM 语法中的 winsock 教程,名为:Iczelion 的 Winsock 编程指南

我陷入困境,收到错误,但我不知道如何修复它。 问题是,每次我尝试使用套接字连接到服务器时,都会收到 WSANOTSOCK 错误(在非套接字上执行套接字操作),

但调用 WSAStartup() 或 Socket() 时没有错误。那么现在怎么会出现这个错误呢?

这是我当前正在使用的代码(我说过我遵循了 Iczelion 的 Winsock 编程指南,但我已经对其进行了修改以满足我的需要,我正在尝试创建一个 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 

提前致谢

-Rick

I'm following along with a winsock tutorial in MASM's syntax called: Iczelion's Guide to Winsock Programming

I'm stuck, I receive an error but I don't know how to fix it.
The thing is that everytime I try to connect to a server with my socket, I receive a WSANOTSOCK error (a socket opperation is preformed on something that is not a socket)

But there was no error when calling WSAStartup() or Socket(). So how can this error be here now?

Here's the code I'm currently using (I said I followed Iczelion's Guide to Winsock Programming, but I've modefied it to suit my needs, I'm trying to create an 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 

Thanks in advance

-Rick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-10-03 04:02:10

在这一部分中:

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”——按原样,我相信它使用的是 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

Throughout this part:

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

where you have "socket", you intended to have "sock" -- as-is, I believe it's using the address (or maybe the first dword of) the socket function instead of the socket value you saved in sock.

If you don't mind my saying so, I think the code is kind of a mess. Cleaning it up a bit, I got this:

.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文