Good day all am a complete novice to the world of windows socket programing. i recently
wrote an application that should help me connect, read, and write to and from socket.
thereafter, each time i build the compiler complains of:
1.see the declaration 'getservbyname'
2.see the declaration getservbyport
3.see the declaration gethostname
4.'gethostname' : redefinition; different linkage
5.see declaration of 'setsockopt'
and a host of other error messages.
To the best of my ability and knowlege, i have tried viewing the header files for
most of the socket APIs that microsoft exposes(including winsock2.h) but can find no error from them. pls does anyone know what could be responsible for this? as the compiler always refer me to these header files thus giving me a hard time. here is an extract of some of my work
//header file
#include <windows.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> /* This is the header file needed for gethostbyname() */
#include <string.h>
#include <tchar.h>
//SocketBoss is the base class for client and server
//but its not listed here.
class ClientSocket : protected SocketBoss
{
public:
ClientSocket(int addfam, int soktype, int pro, HWND hwnd = 0);
ClientSocket(int addfam, int soktype, int pro, HWND hwnd = 0, int flag = WSA_FLAG_OVERLAPPED);
bool Konnect(const char *hostname, int port);
bool SendBytesTO(const char *msg, sockaddr_in addr);
bool RecvBytesFrom(string & recvdata, sockaddr_in addr);
protected:
struct hostent *ptrhost; //Stores host's name or IP Address
IN_ADDR addr; //used by IPv4 internet addressing to resolve host IP
SOCKADDR_IN SockAddre;
bool resolveHostname(const char* name);
unsigned short portNO;
int adfam;
};
//CPP file
ClientSocket::ClientSocket(int addfam, int soktype, int pro, HWND hwnd)
:SocketBoss(addfam, soktype, pro, hwnd), port(80), adfam(addfam)
{
}
bool ClientSocket::Konnect(const char *hostname, unsigned short port)
{
portNO = (port == portNO ? portNO : port);
if(!resolveHostname(hostname))
return false;
SockAddre.sin_port = htons(portNO);
SockAddre.sin_family = adfam;
SockAddre.sin_addr.s_addr = *((unsigned long*)ptrhost->h_addr);
if(connect(getSocket(), (LPSOCKADDR)(&SockAddre), sizeof(SockAddre)) == SOCKET_ERROR)
return false;
}
bool ClientSocket::resolveHostname(const char *name)
{
if(isalpha(name[0])) //if user entered alphabets as hostname
ptrhost = ::gethostbyname(name); //prepare to connect using numerals as hostname
else //otherwise
{
addr.S_un = ::inet_addr(name); //Ensure the digits are in proper IPv4 format
if(addr.S_un == INADDR_NONE)
return false;
}
ptrhost = ::gethostbyaddr((char*)&addr, 4, adfam);
if(ptrhost == NULL)
return false;
}
bool ClientSocket::SendBytesTO(const char *msg, sockaddr_in addr)
{
if(::sendto(getSocket(), msg, strlen(msg), 0, (SOCKADDR *)&addre, sizeof(addre) == SOCKET_ERROR))
return false;
}
please if anyone can i'll be most greatful. However, if you need the complete source to be sure of what am actually facing by this compilation error, u ca mail me on osgiedeprof@yahoo.com and i'll send it to you.
sinserely yours
Odigie Osagie