示例#1
0
Socket & Socket::operator=(Socket const & rhs)
{
    close(GetFD());
    socketDescriptor = rhs.socketDescriptor;
    SetFD(dup(rhs.GetFD()));
    open = rhs.open;
}
示例#2
0
Socket::Socket(std::string const & ipAddress, unsigned int port)
    : Blockable(),open(false)
{
    // First, call socket() to get a socket file descriptor
    SetFD(socket(AF_INET, SOCK_STREAM, 0));
    if (GetFD() < 0)
        throw std::string("Unable to initialize socket server");

    // Start by zeroing out the socket descriptor
     bzero((char*)&socketDescriptor,sizeof(sockaddr_in));

    // Now try to map the IP address, as provided, into the socket Descriptor
    if (!inet_aton(ipAddress.c_str(),&socketDescriptor.sin_addr))
        throw std::string("IP Address provided is invalid");
    socketDescriptor.sin_family = AF_INET;
    socketDescriptor.sin_port = htons(port);
}
示例#3
0
void GCNetLogic::OnRegisterApp(const NetMessage* nmsg, int fd)
{
	MsgRegisterApp msg;
	if (!MsgFromBytes(nmsg, msg))
		return;
	auto app = theAppMgr->AddObject(msg.app_id());
	if (app == NULL)
		return;//有重复的,暂时不断开连接,除非有必要

	app->SetFD(fd);
	app->SetAppID(msg.app_id());
	app->SetAppType(msg.app_type());
	AttachSession(app);

	MsgRespondApp res;
	res.mutable_base()->set_type(Msg_RespondApp);
	res.mutable_base()->set_result(1);
	app->SendToApp(&res);

	LOG(INFO) << "RegisterApp" << LogValue("apptype", msg.app_type()) << LogValue("appid", msg.app_id());
}
    SocketServer::SocketServer(int port)
    {
        // The first call has to be to socket(). This creates a UNIX socket.
        int socketFD = socket(AF_INET, SOCK_STREAM, 0);
        if (socketFD < 0)
            throw std::string("Unable to open the socket server");

        // The second call is to bind().  This identifies the socket file
        // descriptor with the description of the kind of socket we want to have.
        bzero((char*)&socketDescriptor,sizeof(sockaddr_in));
        socketDescriptor.sin_family = AF_INET;
        socketDescriptor.sin_port = htons(port);
        socketDescriptor.sin_addr.s_addr = INADDR_ANY;
        if (bind(socketFD,(sockaddr*)&socketDescriptor,sizeof(socketDescriptor)) < 0)
            throw std::string("Unable to bind socket to requested port");

        // Set up a maximum number of pending connections to accept
        listen(socketFD,5);
        SetFD(socketFD);
        // At this point, the object is initialized.  So return.
    }