문제

im getting issues with the folowing code:

#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;
}

I get the following output:

/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 ===|

I'm totally lost at this point. Ive used Google extensively and solved several other problems myself, but I'm stuck at this. can anyone help me with this issue?

도움이 되었습니까?

해결책

Your variable hostEntry is of type hostent but the function gethostbyname only returns a pointer to a structure. Your variable should have a pointer type, hostent*.

다른 팁

The first error is telling you that the compiler is trying to apply the equals operator and is failing. The error provides additional detail that the candidate implementation of the equals operator that it is trying to apply is operator=(const hostent&) and that the reason this candidate isn't working is that the compiler has no known conversion for argument 1 from ‘hostent*’ to ‘const hostent&’.

We know that gethostname returns hostent *, why isn't the compiler just storing that pointer in the variable hostEntry? Because the variable isn't declared as a pointer to hostent.

Try hostent * hostEntry;

WSA, for Windows Socket API, can't be used on linux. So your WSA macros and WSAGetLastError() can't work

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top