// accept a new SOCK_Stream connection
int
SOCK_Connector::connect(SOCK_Stream &new_stream,
			const char *serverName,
			unsigned short serverPort)
{
    // init server address
    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0x00, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(serverPort);
    if (inet_pton(AF_INET, serverName, &serverAddr.sin_addr) < 0)
    {
	return -1;
    }
    // init the stream
    int sockfd;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
	return -1;
    }
    // connect to server
    if (::connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
	return -1;
    }
    new_stream.setHandle(sockfd);
    return 0;
}
void SynchronousEventDemultiplexerSock::prepFdsSet()
{
	FD_ZERO(&readfds);
	FD_ZERO(&writefds);
	FD_ZERO(&Errorfds);

	FD_SET(*acceptorPtr->getSocket(), &readfds);

	for (std::list<SOCK_Stream*>::iterator it = socketList_->begin(); it != socketList_->end(); it++)
	{
		SOCK_Stream* value = *it;
		FD_SET(value->get_handle(), &readfds);
		FD_SET(value->get_handle(), &writefds);
		FD_SET(value->get_handle(), &Errorfds);
	}

}
Exemple #3
0
int main(int argc, char **argv)
{
	char buff[MAXLINE];
	time_t ticks;

	Inet_Addr addr(80, INADDR_ANY);/* Init the Address struct */
	SOCK_Acceptor acc(addr);/* Accept connection */
	
	for(;;){
		SOCK_Stream *s = new SOCK_Stream();
		acc.Accept(*s);
		ticks = time(NULL);
		snprintf(buff, sizeof(buff), "<html><body>%.24s\r\n</body></html>", ctime(&ticks));
		s->send(buff, strlen(buff), 0);
		
		//s->recv(buff, MAXLINE, 0);
		//printf("%s", buff);
		delete s;
	}

	return 0;
}
Exemple #4
0
void SOCK_Connector::connect(SOCK_Stream& server, INET_Address& addr)
{
	int ret = 0;
	const sockaddr* ad = addr.addr();
	SOCKET s = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (s == INVALID_SOCKET)
	{
		ret = WSAGetLastError();
		throw std::runtime_error("Socket() failed.");
	}

	ret = ::connect(s, ad, addr.size());
	if (ret != 0)
	{
		ret = WSAGetLastError();
		throw std::runtime_error("Connect() failed.");
	}
	else
	{
		server.set_handle(s);
	}
}
Exemple #5
0
void SOCK_Acceptor::accept(SOCK_Stream& s)
{
	s.set_handle(_WINSOCKAPI_::accept(handle_, 0, 0));
}
NetworkEvent SynchronousEventDemultiplexerSock::getNetworkEvent()
{
	struct timeval tv;
	NetworkEvent Nevent; 
	prepFdsSet();
	// wait until either socket has data ready to be recv()d (timeout 10.5 secs)
	tv.tv_sec = 10;
	tv.tv_usec = 500000;

	// to make it work with special sockets 
	int n = socketList_->size() > 0 ? (socketList_->back())->get_handle() + 1 : *acceptorPtr->getSocket() + 1;

	int rv = select(n, &readfds, &writefds, &Errorfds, &tv);

	if (rv == -1) {
		Nevent.setEventType(0); 
	}
	else if (rv == 0) {
		Nevent.setEventType(0);
	}
	else 
	{
		int response = FD_ISSET(*(acceptorPtr->getSocket()), &readfds);
		if ( response > 0)
		{
			if (c_conect == false)
			{
				c_conect = true;
				Nevent.setEventType(6);
				Nevent.setHandle(nullptr);
				std::cout << "Client connect event" << std::endl;
				return Nevent;
			}
		}
		else
		{
			c_conect = false; 
		}
		try{
		for (; it != socketList_->end(); it++)
			{
				SOCK_Stream* value = *it;

				//Create Read Events
				if (FD_ISSET(value->get_handle(), &readfds)) {
					// can be disconnect 
					// can be data 
				
						char networkdata[100]; 
						int resived = value->recv(networkdata, 100, 0);
						if (resived == 0)
						{
							Disconnect(value);
						}
						else
						{
							std::cout << "Networkdata: " << networkdata << std::endl;
							char ev = networkdata[0]; 
							NetworkHandle* handle = new NetworkHandle(value);
							Nevent.setEventType(ev-48);
							std::string data(networkdata + 1, resived-1);
							handle->setData(data);
							Nevent.setHandle(handle); 
							it++;
							return Nevent; 
						}
				

				}


				//Create write Event 
				if (FD_ISSET(value->get_handle(), &writefds)) {
					NetworkHandle* handle = new NetworkHandle(value);
					Nevent.setEventType(4);
					Nevent.setHandle(handle);
					it++;
					return Nevent;
				}
			}
		}
		catch (exception e)
		{
			//Disconnect(value);
		}
	}

	it = socketList_->begin();
	return Nevent; 
}