示例#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);
}
示例#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);
}
示例#3
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);
}