ClientSocket::ClientSocket (std::string const& _host, t_uint const _port) { if (!Socket::create()) { throw socketException("Could not create client socket."); } if (!Socket::connect(_host, _port)) { std::stringstream s; s << "Could not connect to " << _host << ":" << _port << "."; throw socketException(s.str()); } }
void inSocket::connect(const char* host,unsigned int port) { hostent* he; DBG("Trying connection with "<<host<<" on port "<< port); if ((addr.sin_addr.s_addr=inet_addr(host))==-1) if ((he=gethostbyname(host)) != 0) memcpy((char*)&addr.sin_addr,he->h_addr,he->h_length); else throw(socketException("connect:gethostbyname failed.")); addr.sin_port=htons(port); if(::connect(fd,(sockaddr*)&addr,sizeof(addr)) ==-1) throw (socketException("connect failed.")); }
void inSocket::accept(inSocket &other) { int len=sizeof(addr); DBG("Starting accept"); if((fd=::accept(other.fd,(sockaddr*)&addr,&len))==-1) { throw socketException("accept failed"); } DBG("Got Connection"); }
void inSocket::create(void) { addr.sin_addr.s_addr=INADDR_ANY; addr.sin_family=AF_INET; //if ((fd=WSASocket(AF_INET,type,0,NULL,0,/*WSA_FLAG_OVERLAPPED*/0))==SOCKET_ERROR) if ((fd=socket(AF_INET,type,0))==SOCKET_ERROR) throw (socketException("socket creation failed.")); }
void inSocket::bind(int port) { int namelen=sizeof(addr); addr.sin_port=htons(port); if (::bind(fd,(sockaddr*)&addr,sizeof(addr))==-1) throw (socketException("bind failed")); getsockname(fd,(sockaddr*)&addr,&namelen); DBG("bind: fd="<<fd<<" Port="<<port); }
void inSocket::connect(const char* host,unsigned int port) { hostent* he; char er[255]; DBG("Trying connection with "<<host<<" on port "<< port); //if ((addr.sin_addr.s_addr=inet_addr(host))==-1) { Silke: if ((addr.sin_addr.s_addr=inet_addr(host))!=INADDR_NONE) { if ((he=gethostbyname(host)) != 0) { memcpy((char*)&addr.sin_addr,he->h_addr,he->h_length); } else { sprintf(er,"connect: gethostbyname(%s) failed.",host); throw (socketException((const char *)er)); } } addr.sin_port=htons(port); if(::connect(fd,(sockaddr*)&addr,sizeof(addr)) ==-1) throw (socketException("connect failed.")); }
inSocket::inSocket(unsigned int port,addrType ad, int theType) { hostent * he; type = theType; create(); if (ad==local) { if ((he=gethostbyname("localhost")) != 0) memcpy((char*)&addr.sin_addr,he->h_addr,he->h_length); else throw(socketException("inSocket:gethostbyname failed.")); DBG("Got local name"); } this->bind(port); }
void inSocket::bind(int port) { int namelen=sizeof(addr); addr.sin_port=htons(port); #ifndef _WIN32 BOOL val=TRUE; if(port) setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char FAR *)&val,sizeof(BOOL)); #endif if (::bind(fd,(sockaddr*)&addr,sizeof(addr))==-1) throw (socketException("bind failed")); getsockname(fd,(sockaddr*)&addr,&namelen); DBG("bind: fd="<<fd<<" Port="<<port); }
void inSocket::listen(int anz) { DBG("listen: fd="<<fd<<" anz="<<anz); if (::listen(fd,anz)==-1) throw(socketException("listen failed")); }
void inSocket::create(void) { addr.sin_addr.s_addr=INADDR_ANY; addr.sin_family=AF_INET; if ((fd=socket(AF_INET,type,0))==-1) throw (socketException("socket creation failed.")); }