'operator ='和sockaddr成员的问题

时间:2012-07-08 00:28:20

标签: c++ sockets operator-keyword hostent

我遇到以下代码的问题:

#include <netdb.h>
#include "socket.h"

#define INVALID_SOCKET -1
#define SOCKET_ERROR   -1

int SenderAddrSize = sizeof(sockaddr);
sockaddr CSocket::SenderAddr;
bool CSocket::tcpconnect(char *address, int port, int mode)
{
    sockaddr_in addr;
    hostent hostEntry;
    if((sockid = socket(AF_INET,SOCK_STREAM, 0)) == SOCKET_ERROR)
        return false;
    if((hostEntry = gethostbyname(address)) == NULL)
    {
        closesocket(sockid);
        return false;
    }
    addr.sin_family = AF_INET;
    addr.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    addr.sin_port = htons((u_short)port);
    if(mode ==2)setsync(1);
    if(connect(sockid, (LPSOCKADDR)&addr, sizeof(sockaddr)) == SOCKET_ERROR)
    {
        if(WSAGetLastError() != WSAEWOULDBLOCK)
        {
            closesocket(sockid);
            return false;
        }
    }
    if(mode ==1)setsync(1);
    return true;
}

我得到以下输出:

/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘bool CSocket::tcpconnect(char*, int, int)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|15|error: no match for ‘operator=’ in ‘hostEntry = gethostbyname(((const char*)address))’|
/home/nick/.projects/39dll-4-linux/socket.cpp|15|note: candidate is:|
/usr/include/netdb.h|101|note: hostent& hostent::operator=(const hostent&)|
/usr/include/netdb.h|101|note:   no known conversion for argument 1 from ‘hostent*’ to ‘const hostent&’|
/home/nick/.projects/39dll-4-linux/socket.cpp|17|error: ‘closesocket’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|21|error: ‘LPIN_ADDR’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|21|error: base operand of ‘->’ has non-pointer type ‘hostent’|
/home/nick/.projects/39dll-4-linux/socket.cpp|24|error: ‘LPSOCKADDR’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|26|error: ‘WSAGetLastError’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|26|error: ‘WSAEWOULDBLOCK’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|28|error: ‘closesocket’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘bool CSocket::tcplisten(int, int, int)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|40|error: ‘struct sockaddr’ has no member named ‘sin_family’|
/home/nick/.projects/39dll-4-linux/socket.cpp|41|error: ‘struct sockaddr’ has no member named ‘sin_addr’|
/home/nick/.projects/39dll-4-linux/socket.cpp|42|error: ‘struct sockaddr’ has no member named ‘sin_port’|
/home/nick/.projects/39dll-4-linux/socket.cpp|44|error: ‘LPSOCKADDR’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|46|error: ‘closesocket’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|51|error: ‘closesocket’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|57|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/.projects/39dll-4-linux/socket.cpp||In destructor ‘CSocket::~CSocket()’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|75|error: ‘closesocket’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘CSocket* CSocket::tcpaccept(int)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|80|error: ‘SOCKET’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|80|error: expected ‘;’ before ‘sock2’|
/home/nick/.projects/39dll-4-linux/socket.cpp|81|error: ‘sock2’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|81|error: ‘SOCKADDR’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|81|error: expected primary-expression before ‘)’ token|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘char* CSocket::tcpip()’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|93|error: ‘SOCKADDR’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|93|error: expected primary-expression before ‘)’ token|
/home/nick/.projects/39dll-4-linux/socket.cpp|94|error: ‘struct sockaddr’ has no member named ‘sin_addr’|
/home/nick/.projects/39dll-4-linux/socket.cpp|94|error: ‘inet_ntoa’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘void CSocket::setnagle(bool)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|100|error: ‘TCP_NODELAY’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘bool CSocket::tcpconnected()’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|108|error: ‘WSAGetLastError’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|108|error: ‘WSAEWOULDBLOCK’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘int CSocket::setsync(int)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|116|error: ‘FIONBIO’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|116|error: ‘ioctlsocket’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘bool CSocket::udpconnect(int, int)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|121|error: ‘SOCKADDR_IN’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|121|error: expected ‘;’ before ‘addr’|
/home/nick/.projects/39dll-4-linux/socket.cpp|124|error: ‘addr’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|128|error: ‘SOCKADDR’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|128|error: expected primary-expression before ‘)’ token|
/home/nick/.projects/39dll-4-linux/socket.cpp|130|error: ‘closesocket’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘int CSocket::sendmessage(char*, int, CBuffer*)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|142|error: ‘SOCKADDR_IN’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|142|error: expected ‘;’ before ‘addr’|
/home/nick/.projects/39dll-4-linux/socket.cpp|145|error: ‘min’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|145|note: suggested alternative:|
/usr/include/c++/4.6/bits/stl_algobase.h|233|note:   ‘std::min’|
/home/nick/.projects/39dll-4-linux/socket.cpp|146|error: ‘addr’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|148|error: ‘inet_addr’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|149|error: ‘SOCKADDR’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp|149|error: expected primary-expression before ‘)’ token|
/home/nick/.projects/39dll-4-linux/socket.cpp|168|error: ‘WSAGetLastError’ was not declared in this scope|
/home/nick/.projects/39dll-4-linux/socket.cpp||In member function ‘int CSocket::receivemessage(int, CBuffer*)’:|
/home/nick/.projects/39dll-4-linux/socket.cpp|198|error: ‘SOCKADDR’ was not declared in this scope|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 0 warnings ===|

我在这一点上完全迷失了。我已广泛使用谷歌并自己解决了其他几个问题,但我坚持这一点。任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:3)

您的变量hostEntry的类型为hostent,但函数gethostbyname仅将指针返回给结构。您的变量应该具有指针类型hostent*

答案 1 :(得分:0)

第一个错误告诉您编译器正在尝试应用equals运算符并且失败。该错误提供了其尝试应用的equals运算符的候选实现是operator=(const hostent&)的额外细节,并且该候选不起作用的原因是编译器具有no known conversion for argument 1 from ‘hostent*’ to ‘const hostent&’

我们知道gethostname返回hostent *,为什么编译器只是将该指针存储在变量hostEntry中?因为变量未声明为指向hostent的指针。

尝试hostent * hostEntry;

答案 2 :(得分:-1)

WSA,对于Windows Socket API,不能在linux上使用。 所以你的WSA宏和WSAGetLastError()不能工作