Linux bind:非socket上的socket操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5602296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:35:05  来源:igfitidea点击:

bind: Socket operation on non-socket

c++linuxsocketsserversocket

提问by Ted Spradley

I writing a server and a client and keep getting 'bind: Socket operation on non-socket'.

我编写了一个服务器和一个客户端,并不断收到“绑定:非套接字上的套接字操作”。

I've researched the heck out of this, have other code that runs in another application and have exhausted 8 hours trying to find this bug.

我已经研究了这个问题,有其他代码在另一个应用程序中运行,并且已经用了 8 个小时试图找到这个错误。

The code is:

代码是:

void TCPSocket::buildTCPSocket(int port)
{
    initializeSocket1();
    getSocket();
    bindSocket();
    listenToSocket();
    acceptSocket();
         // now you can send() and recv() with the
        // connected client via socket connectedTCPSocket
}

void TCPSocket::getSocket()
{
        // Get an internet domain socket AF_INET
    if(socket1 = socket(AF_INET, SOCK_STREAM,0) == -1)
    {
        perror("socket");
        exit(1);
    }    
}


void TCPSocket::bindSocket()
{
  // Bind to a port on the host
    int myAddressSize = sizeof(myAddress);
    int bindReturnValue = bind(socket1, (struct sockaddr *) &myAddress, AddressSize);
    if (bindReturnValue == -1)
    {
        perror("bind");  // <== Error message generated here
        exit(1);
    }
    printf("Socket for TCP bound to port %d\n", port);    
}

Also, prior to this, I memset the memory block with this function.

另外,在此之前,我用这个函数来设置内存块。

void TCPSocket::initializeSocket1()
{
    // Fill tcpSocket struct with 0's

    memset(&myAddress, '
public:
    struct sockaddr_in myAddress, clientAddress;

    void buildTCPSocket(int newPort);

private:
    int port;
    int socket1, socket2;

    socklen_t clientAddressLength;
', sizeof(myAddress)); myAddress.sin_family = AF_INET; myAddress.sin_addr.s_addr = INADDR_ANY; // Conver PORT to big-endian if necessary myAddress.sin_port = htons(this->port); }

Variables are declared in the header file of the class.

变量在类的头文件中声明。

void TCPSocket::getSocket()
{
        // Get an internet domain socket AF_INET
    if((socket1 = socket(AF_INET, SOCK_STREAM,0)) == -1)
    {
        perror("socket");
        exit(1);
    }    
}

-- Edit the code should be a little more clear now. socket1 is initialized in getSocket().

-- 编辑代码现在应该更清楚了。socket1 在 getSocket() 中初始化。

I've seen where a bunch of guys have missed the parens in the if but I think I eliminated that error by declaring myAddressSize and bindReturnValue.

我已经看到很多人在 if 中错过了括号,但我认为我通过声明 myAddressSize 和 bindReturnValue 消除了该错误。

Any input is appreciated.
Thank you, Ted S

任何输入表示赞赏。
谢谢你,泰德 S

Ok, problem solved. Of course the problem is never where you are looking are you would have found it. Here is the corrected code. The problem was in a missing set of parens in the call to socket().

好的,问题解决了。当然,问题永远不在于您正在寻找的地方,您会发现它。这是更正后的代码。问题在于对 socket() 的调用中缺少一组括号。

 int socket1 = socket(AF_INET, SOCK_STREAM, 0);
 bind(socket1, ...);

Thanks again!

再次感谢!

采纳答案by Mike Bailey

I can almost guarantee you that you're getting that error because you never initialized socket1.

我几乎可以向您保证您会收到该错误,因为您从未初始化过 socket1。

Typically you have to do something like this:

通常,您必须执行以下操作:

if ((foo = bar()) == ERROR)
{
   // handle me
}

I don't see any code anywhere in there for setting up socket1. This is what the error message is telling you, after all. socket1 isn't a socket, so it's failing.

我在那里没有看到任何用于设置 socket1 的代码。毕竟,这就是错误消息告诉您的内容。socket1 不是套接字,所以它失败了。

Edit: As a follow up, this is one of the reasons why I try to avoid using the syntax

编辑:作为跟进,这是我尝试避免使用语法的原因之一

void TCPSocket::getSocket()
{
        // Get an internet domain socket AF_INET
    socket1 = socket(AF_INET, SOCK_STREAM, 0);
    if (socket == -1)
    {
        perror("socket");
        exit(1);
    }    
}

And instead stick with:

而是坚持:

##代码##