| 4900 | } |
| 4901 | |
| 4902 | void StreamingListener::SocketWriter::MakeConnection() { |
| 4903 | GTEST_CHECK_(sockfd_ == -1) |
| 4904 | << "MakeConnection() can't be called when there is already a connection."; |
| 4905 | |
| 4906 | addrinfo hints; |
| 4907 | memset(&hints, 0, sizeof(hints)); |
| 4908 | hints.ai_family = AF_UNSPEC; // To allow both IPv4 and IPv6 addresses. |
| 4909 | hints.ai_socktype = SOCK_STREAM; |
| 4910 | addrinfo* servinfo = nullptr; |
| 4911 | |
| 4912 | // Use the getaddrinfo() to get a linked list of IP addresses for |
| 4913 | // the given host name. |
| 4914 | const int error_num = |
| 4915 | getaddrinfo(host_name_.c_str(), port_num_.c_str(), &hints, &servinfo); |
| 4916 | if (error_num != 0) { |
| 4917 | GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: " |
| 4918 | << gai_strerror(error_num); |
| 4919 | } |
| 4920 | |
| 4921 | // Loop through all the results and connect to the first we can. |
| 4922 | for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != nullptr; |
| 4923 | cur_addr = cur_addr->ai_next) { |
| 4924 | sockfd_ = socket(cur_addr->ai_family, cur_addr->ai_socktype, |
| 4925 | cur_addr->ai_protocol); |
| 4926 | if (sockfd_ != -1) { |
| 4927 | // Connect the client socket to the server socket. |
| 4928 | if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) { |
| 4929 | close(sockfd_); |
| 4930 | sockfd_ = -1; |
| 4931 | } |
| 4932 | } |
| 4933 | } |
| 4934 | |
| 4935 | freeaddrinfo(servinfo); // all done with this structure |
| 4936 | |
| 4937 | if (sockfd_ == -1) { |
| 4938 | GTEST_LOG_(WARNING) << "stream_result_to: failed to connect to " |
| 4939 | << host_name_ << ":" << port_num_; |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | // End of class Streaming Listener |
| 4944 | #endif // GTEST_CAN_STREAM_RESULTS__ |