我在获取Microsoft平台sdk提供的示例lsp中wspsend函数的目标端口号时遇到问题。
这是我正在使用的代码。如下所示,未输入if语句。我用调试函数验证了这一点。
我正在尝试使用目标端口80识别此函数中传出的http数据包。

int WSPAPI
WSPSend(
    SOCKET          s,
    LPWSABUF        lpBuffers,
    DWORD           dwBufferCount,
    LPDWORD         lpNumberOfBytesSent,
    DWORD           dwFlags,
    LPWSAOVERLAPPED lpOverlapped,
    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
    LPWSATHREADID   lpThreadId,
    LPINT           lpErrno
    )
{
    INT                 ret = SOCKET_ERROR;
    SOCK_INFO          *SocketContext = NULL;
    LPWSAOVERLAPPEDPLUS ProviderOverlapped = NULL;

    *lpErrno = NO_ERROR;

    //
    // Find our provider socket corresponding to this one
    //
    SocketContext = FindAndRefSocketContext(s, lpErrno);
    if ( NULL == SocketContext )
    {
        dbgprint( "WSPSend: FindAndRefSocketContext failed!" );
        goto cleanup;
    }

    // My code starts here!!!
    SOCKET app = SocketContext->LayeredSocket;
    struct sockaddr FAR name;
    int FAR namelen;
    getpeername(app, &name, &namelen);
    struct sockaddr_in sin;
    sin =* (const struct sockaddr_in *) (&name);
    if(sin.sin_port == htons(80))
    {
        // This code is not executed after sending HTTP packets!!
    }
 }

有什么想法吗?

最佳答案

getpeername有效吗?在使用结果之前,您的代码需要检查返回代码。
如果没有错误发生,getpeername
返回零。否则,值
返回套接字错误,并且
可以检索特定的错误代码
打电话给wsagetlasterror。
除此之外,在进行此调用之前,您需要指定namelen作为输出结构的大小-这是我打赌这里有什么问题,因为namelen没有初始化。仔细阅读WinSock documentation很重要-windows中充斥着这些api使用规则,如果不遵循它们,可能会浪费很多时间。
在调用时,namelen参数
包含
名称缓冲区。作为回报
参数包含实际大小,in
返回的name参数的字节数。

关于windows - 在WSPSend中获取目标端口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4177402/

10-10 21:23