コード例 #1
0
ファイル: zhttpd.c プロジェクト: zinkem/zhttpd
int main(int argc, char* argv[]){
  // 1. create socket
  // 2. bind/listen
  // 3. accept new connections

	int socketfd = newTcpSocket();
	bindAndListen(socketfd, 3000, 1000);
	handleIncomingRequests(socketfd);

	return 0;
}
コード例 #2
0
void triviaServer::serve(struct addrinfo* a)
{
	bindAndListen(a);
	std::thread(&triviaServer::handleRecievedMessages, this).detach();
	while (true)
	{
		try
		{
			_accept(listenSocket);
		}
		catch (std::exception &e)
		{
			std::cout << e.what() << std::endl;
		}
	}
}
コード例 #3
0
void SocketTransport::listenForClientConnection() {
  // If there was a previous connection in the base class, shut it down.
  // This will close the old transport and wait for both the send and receive
  // worker threads to terminate before proceeding.
  if (getTransportFd() >= 0) {
    shutdown();
  }

  VSDebugLogger::Log(
    VSDebugLogger::LogLevelInfo,
    "SocketTransport worker thread listening for connections..."
  );

  int abortFd;
  {
    Lock lock(m_lock);
    abortFd = m_abortPipeFd[0];
    assertx(abortFd >= 0);
  }

  struct addrinfo hint;
  struct addrinfo* ai = nullptr;
  std::vector<int> socketFds;

  memset(&hint, 0, sizeof(hint));
  hint.ai_family = AF_UNSPEC;
  hint.ai_socktype = SOCK_STREAM;
  hint.ai_flags = AI_PASSIVE;

  SCOPE_EXIT {
    if (ai != nullptr) {
      freeaddrinfo(ai);
    }

    for (auto it = socketFds.begin(); it != socketFds.end(); it++) {
      close(*it);
    }

    close(abortFd);
    m_abortPipeFd[0] = -1;

    VSDebugLogger::Log(
      VSDebugLogger::LogLevelInfo,
      "SocketTransport connection polling thread exiting."
    );
  };

  // Share existing DebuggerDisableIPv6 configuration with hphpd.
  if (RuntimeOption::DebuggerDisableIPv6) {
    hint.ai_family = AF_INET;
  }

  const auto name = RuntimeOption::DebuggerServerIP.empty()
    ? "localhost"
    : RuntimeOption::DebuggerServerIP.c_str();
  if (getaddrinfo(name, std::to_string(m_listenPort).c_str(), &hint, &ai)) {
    VSDebugLogger::Log(
      VSDebugLogger::LogLevelError,
      "Failed to call getaddrinfo: %d.",
      errno
    );

    return;
  }

  // Attempt to listen on the specified port on each of this host's available
  // addresses.
  struct addrinfo* address;
  bool anyInterfaceBound = false;
  for (address = ai; address != nullptr; address = address->ai_next) {
    if (bindAndListen(address, socketFds)) {
      anyInterfaceBound = true;
    }
  }

  if (!anyInterfaceBound) {
    VSDebugLogger::Log(
      VSDebugLogger::LogLevelWarning,
      "Debugger failed to bind to any interface!"
    );
    return;
  } else {
    VSDebugLogger::Log(
      VSDebugLogger::LogLevelInfo,
      "Debugger bound to at least one interface."
    );
  }

  waitForConnection(socketFds, abortFd);
}