Esempio n. 1
0
bool			TCPLinServSocket::SearchNewClients(Selector &sel)
{
	SOCKET		newclientsocket;
	int			highestid = 1;
	struct sockaddr_in	s_in;
	socklen_t		s_size;

	s_size = sizeof(s_in);
	if ((sel.Is_readable(_fathersocket)) == true)
	{
		if ((newclientsocket = accept(_fathersocket, (struct sockaddr *)&s_in, &s_size)) == INVALID_SOCKET)
		{
			std::cerr << "Couldn't accept a new client" << std::endl;
			return (false);
		}
		sel.Add_to_checkread(newclientsocket);
		sel.Add_to_checkwrite(newclientsocket);
		for (size_t i = 0; i < _clients.size(); i++)
		if (_clients[i].get_id() >= highestid)
			highestid = _clients[i].get_id() + 1;

		Client	newclient(newclientsocket, highestid);
		_clients.push_back(newclient);
	}
	return (true);
}
Esempio n. 2
0
bool		TCPWinServSocket::SearchNewClients(Selector &sel)
{
	SOCKET	newclientsocket;
	int		highestid = 1;

	if ((sel.Is_readable(_fathersocket)) == true)
	{
		if ((newclientsocket = WSAAccept(_fathersocket, NULL, NULL, NULL, NULL)) == INVALID_SOCKET)
		{
			std::cerr << "Couldn't accept a new client" << std::endl;
			return (false);
		}
		sel.Add_to_checkread(newclientsocket);
		sel.Add_to_checkwrite(newclientsocket);

		for (size_t i = 0; i < _clients.size(); i++)
			if (_clients[i].get_id() >= highestid)
				highestid = _clients[i].get_id() + 1;
		
		Client	newclient(newclientsocket, highestid);

		_clients.push_back(newclient);
	}
	return (true);
}
Esempio n. 3
0
bool			TCPLinServSocket::Connect(int port, Selector &sel)
{
	struct sockaddr_in    s_in;
	struct protoent	*pe;

	if ((pe = getprotobyname("TCP")) == NULL)
		return (false);
	// Socket creation
	if ((_fathersocket = socket(AF_INET, SOCK_STREAM, pe->p_proto)) == INVALID_SOCKET)
		return (false);
	//Socket binding
	s_in.sin_family = AF_INET;
	s_in.sin_port = htons(port);
	s_in.sin_addr.s_addr = INADDR_ANY;

	if ((bind(_fathersocket, (const struct sockaddr *)&s_in, sizeof(s_in))) == INVALID_SOCKET)
		return (false);

	//Socket listening
	if ((listen(_fathersocket, 42)) == INVALID_SOCKET)
		return (false);
	//Add to selector for monitoring
	sel.Add_to_checkread(_fathersocket);
	return (true);
}
Esempio n. 4
0
bool		TCPWinServSocket::Connect(int port, Selector &sel)
{
	struct sockaddr_in    s_in;
	u_short				  ret;

	// Socket creation
	_fathersocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, NULL, WSA_FLAG_OVERLAPPED);
	if (_fathersocket == INVALID_SOCKET)
		return (false);
	//Socket binding
	s_in.sin_family = AF_INET;
	s_in.sin_addr.s_addr = INADDR_ANY;
	if ((WSAHtons(_fathersocket, port, &ret)) == SOCKET_ERROR)
		return (false);
	
	s_in.sin_port = ret;
	if ((bind(_fathersocket, (const struct sockaddr *)&s_in, sizeof(s_in))) == SOCKET_ERROR)
		return (false);

	//Socket listening
	if ((listen(_fathersocket, SOMAXCONN)) == SOCKET_ERROR)
		return (false);
	//Add to selector for monitoring
	sel.Add_to_checkread(_fathersocket);
	return (true);
}
Esempio n. 5
0
bool			TCPLinSocket::ConnectToServer(std::string &hostname, std::string port,
	Selector &sel)
{
	struct protoent	*pe;
	struct sockaddr_in    s_in;

	//socket creation
	if ((pe = getprotobyname("TCP")) == NULL)
		return (false);
	if ((_fathersocket = socket(AF_INET, SOCK_STREAM, pe->p_proto)) == -1)
		return (false);

	//socket connection
	s_in.sin_family = AF_INET;
	s_in.sin_port = htons(atoi(port.c_str()));
	s_in.sin_addr.s_addr = inet_addr(hostname.c_str());

	if ((connect(_fathersocket, (struct sockaddr *)&s_in, sizeof(s_in))) == -1)
		return (false);

	sel.Add_to_checkread(_fathersocket);
	sel.Add_to_checkwrite(_fathersocket);
	return (true);
}