简体   繁体   English

Windows上的C套接字编程在侦听时出错,但没有errno

[英]C Socket programming on windows errors on listen but no errno

I've done socket programming in other languages but never dealt with doing it in C directly. 我已经用其他语言完成了套接字编程,但从未处理过直接用C语言进行编程的事情。 I'm trying to set up a simple server to listen on port 8080 under Windows. 我正在尝试设置一个简单的服务器来侦听Windows下的8080端口。 I'm using the gcc that comes with mingw to compile the code. 我正在使用mingw随附的gcc来编译代码。



    #include <stdio.h>
    #include <string.h>
    #include <WinSock2.h>
    #include <stdint.h>
    #include <time.h>

        int main(void) {

        int s;      
        int len;
        char  buffer[1025];  
        struct sockaddr_in servAddr; 
        struct sockaddr_in clntAddr; 

        int clntAddrLen; //length of client socket addre

    //Build local (server) socket add

        memset(&servAddr, 0, sizeof(servAddr));
        servAddr.sin_family = AF_INET;
        servAddr.sin_port = htons(8080);
        servAddr.sin_addr.s_addr = htonl(INADDR_ANY);

       //create socket
        if((s=socket(AF_INET, SOCK_STREAM, 0) %lt; 0 )){
            perror("Error: Socket Failed!");
                printf("%d\n", errno);
            exit(1);
        }

    //bind socket to local address and port
        if((bind(s,(struct sockaddr*)&servAddr, sizeof(servAddr)) %lt; 0))
        {
            perror("Error:bind failed!");
                printf("%d\n", errno);
            exit(1);
        }

        for(;;)
        {
            len = recvfrom(s,buffer, sizeof(buffer),0,(struct sockaddr*)&clntAddr, &clntAddrLen);

            //send string
            sendto(s, buffer, len, 0, (struct sockaddr*)&clntAddr, sizeof(clntAddr));
        }

    }

When I run the code it errors out binding but there's no error. 当我运行代码时,它会错误地绑定,但是没有错误。 This code was taken and slightly modified from this thread: Windows Socket Programming in C 该代码是从该线程中获取并稍作修改的: Windows C套接字编程

But there's no error code. 但是没有错误代码。 The output prints that there's no error. 输出显示没有错误。 It fails at the bind and I'm not sure why. 它在绑定时失败,我不确定为什么。 I've been piecing together a few different sources. 我一直在拼凑一些不同的资料。 I literally just want something that can do a hello world/3 way handshake in C on Windows. 我实际上只是想要一些可以在Windows上的C语言中进行hello world / 3方式握手的东西。

Thanks in advance. 提前致谢。

This is incorrect due to operator precedence : 由于运算符优先级,这是不正确的:

if((s=socket(AF_INET, SOCK_STREAM, 0) < 0 )){

as < has higher precedence than = . 因为<优先级高于= This means that the code will assign 1 or 0 to s depending on result of socket(AF_INET, SOCK_STREAM, 0) < 0 . 这意味着码将分配10s取决于结果socket(AF_INET, SOCK_STREAM, 0) < 0 Change to: 改成:

if((s=socket(AF_INET, SOCK_STREAM, 0)) < 0 ){

Use WSAGetLastError() to obtain failure reason, not errno . 使用WSAGetLastError()获取失败原因,而不是errno From the MSDN bind() reference page: 从MSDN bind()参考页:

If no error occurs, bind returns zero. 如果没有错误发生,则绑定返回零。 Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError. 否则,它将返回SOCKET_ERROR, 并且可以通过调用WSAGetLastError来检索特定的错误代码。

On Windows WSAStartup() must be invoked before any socket functions are called. 在Windows上,必须在调用任何套接字函数之前调用WSAStartup()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM