0

次のコードで問題が発生しています:

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

私はこの時点で完全に迷子になっています。私はグーグルを広範囲に使用し、他のいくつかの問題を自分で解決しましたが、私はこれで立ち往生しています。誰かがこの問題で私を助けることができますか?

4

3 に答える 3

3

変数hostEntryは型ですhostentが、関数は構造体へのポインターgethostbynameのみを返します。変数のポインタ型は、である必要があります。hostent*

于 2012-07-08T00:32:36.530 に答える
0
于 2012-07-08T00:47:54.260 に答える