コード例 #1
0
ファイル: testserver.c プロジェクト: Zhouxiaoqing/kendylib
void process_new_connection(datasocket_t s)
{
	set_recv_timeout(s,10*1000);
	set_send_timeout(s,10*1000);
	add_client(s);
	++count;
	printf("%d\n",count);
}
コード例 #2
0
ファイル: net_interfaces.c プロジェクト: colding/CompareHosts
int
connect_to_listening_socket(const char * const interface,
			    const uint16_t port,
			    const int pf_family,
			    const int socket_type,
                            const time_t timeout)
{
        int res;
        int sock;
        struct addrinfo hint;
        struct addrinfo *ai;
        struct addrinfo *ai_current;
        char pstr[32] = { '\0' };
        int close_socket = 1;

        sock = -1;

        if (SOCK_STREAM != socket_type)
                return sock;

        memset(&hint, 0, sizeof(struct addrinfo));
        hint.ai_flags = AI_NUMERICSERV;
        hint.ai_family = pf_family;
        hint.ai_socktype = socket_type;
        hint.ai_protocol = IPPROTO_TCP;

        sprintf(pstr, "%u", port);
        res = getaddrinfo(interface, pstr, &hint, &ai);
        if (res) {
                return sock;
        }

        for (ai_current = ai; ai_current; ai_current = ai_current->ai_next) {
		if (-1 == sock) {
			close(sock);
                        sock = -1;
		}
                sock = socket(ai_current->ai_family, ai_current->ai_socktype, ai_current->ai_protocol);
                if (-1 == sock)
                        continue;

                if (!set_send_timeout(sock, timeout)) {
                        continue;
                }
                do {
                        if (!connect(sock, ai_current->ai_addr, ai_current->ai_addrlen)) {
				close_socket = 0;
                                goto done;
			}

                        switch (errno) {
			case EAGAIN:
                        case ETIMEDOUT:
				goto done;
                        default:
				;
                        }
                        break;
                } while (1);
        }
done:
        freeaddrinfo(ai);
	if (close_socket) {
		close(sock);
		sock = -1;
	}
        return sock;
}
コード例 #3
0
ファイル: sas.cpp プロジェクト: ClearwaterCore/sas-client
bool SAS::Connection::connect_init()
{
  if (_socket_callback)
  {
    _sock = _socket_callback(_sas_address.c_str(), SAS_PORT);
  }
  else
  {
    _sock = get_local_sock(_sas_address.c_str(), SAS_PORT);
  }

  if (_sock < 0)
  {
    return false;
  }
 
  SAS_LOG_DEBUG("Connected SAS socket to %s:%s", _sas_address.c_str(), SAS_PORT);
  set_send_timeout(_sock, SEND_TIMEOUT);

  // Send an init message to SAS.
  std::string init;
  std::string version("v0.1");

  // The resource version is part of the binary protocol but is not currently
  // exposed over the C++ API.
  std::string resource_version("");

  int init_len = INIT_HDR_SIZE +
                 sizeof(uint8_t) + _system_name.length() +
                 sizeof(uint32_t) +
                 sizeof(uint8_t) + version.length() +
                 sizeof(uint8_t) + _system_type.length() +
                 sizeof(uint8_t) + _resource_identifier.length() +
                 sizeof(uint8_t) + resource_version.length();
  init.reserve(init_len);
  write_hdr(init, init_len, SAS_MSG_INIT, get_current_timestamp());
  write_int8(init, (uint8_t)_system_name.length());
  write_data(init, _system_name.length(), _system_name.data());
  int endianness = 1;
  init.append((char*)&endianness, sizeof(int)); // Endianness must be written in machine order.
  write_int8(init, version.length());
  write_data(init, version.length(), version.data());
  write_int8(init, (uint8_t)_system_type.length());
  write_data(init, _system_type.length(), _system_type.data());
  write_int8(init, (uint8_t)_resource_identifier.length());
  write_data(init, _resource_identifier.length(), _resource_identifier.data());
  write_int8(init, (uint8_t)resource_version.length());
  write_data(init, resource_version.length(), resource_version.data());

  SAS_LOG_DEBUG("Sending SAS INIT message");

  int rc = ::send(_sock, init.data(), init.length(), 0);
  if (rc < 0)
  {
    SAS_LOG_ERROR("SAS connection to %s:%s failed: %d %s", _sas_address.c_str(), SAS_PORT, errno, ::strerror(errno));
    ::close(_sock);
    _sock = -1;
    return false;
  }

  SAS_LOG_STATUS("Connected to SAS %s:%s", _sas_address.c_str(), SAS_PORT);

  return true;
}
コード例 #4
0
ファイル: sas.cpp プロジェクト: ClearwaterCore/sas-client
int SAS::Connection::get_local_sock(const char* sas_address, const char* sas_port)
{
  int rc;
  struct addrinfo hints, *addrs;

  SAS_LOG_STATUS("Attempting to connect to SAS %s", sas_address);

  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;

  rc = getaddrinfo(sas_address, sas_port, &hints, &addrs);

  if (rc != 0)
  {
    SAS_LOG_ERROR("Failed to get addresses for SAS %s:%s : %d %s",
                  sas_address, sas_port, errno, ::strerror(errno));
    return -1;
  }

  struct addrinfo *p;

  int sock;

  // Reset the return code to error
  rc = 1;

  for (p = addrs; p != NULL; p = p->ai_next)
  {
    if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
    {
      // There was an error opening the socket - try the next address
      SAS_LOG_DEBUG("Failed to open socket");
      continue;
    }
  
    if (!set_send_timeout(sock, SEND_TIMEOUT))
    {
      SAS_LOG_ERROR("Failed to set send timeout on SAS connection : %d %d %s",
                                                 rc, errno, ::strerror(errno));
      ::close(sock);
      sock = -1;
      continue;
    }

    rc = ::connect(sock, p->ai_addr, p->ai_addrlen);

    if (rc < 0)
    {
      // There was an error connecting - try the next address
      SAS_LOG_DEBUG("Failed to connect to address: %s", p->ai_addr);
      ::close(sock);
      sock = -1;
      continue;
    }

    // Connection successful at this point
    break;
  }

  if (rc != 0)
  {
    SAS_LOG_ERROR("Failed to connect to SAS %s:%s : %d %s", sas_address, sas_port, errno, ::strerror(errno));
    return -1;
  }

  freeaddrinfo(addrs);

  return sock;
}