// noWait: Boolean containing the endpoint (3, 4, & 5)
bool ZeroMQHandler::setSlotNoWait(const Basic::Boolean* const msg)
{
   // Save the nowait definition for use in the initialization of the
   // socket
   bool ok = false;
   if (msg != nullptr) ok = setNoWait(*msg);
   return ok;
}
//------------------------------------------------------------------------------
// listenForConnections() -- puts the socket into listen mode
//------------------------------------------------------------------------------
bool TcpServerSingle::acceptConnection()
{
   if (socketNum == INVALID_SOCKET) return 0;

   struct sockaddr_in clientAddr;
   socklen_t cAddrSize = sizeof(clientAddr);
   if (isMessageEnabled(MSG_INFO)) {
       std::cout << "Waiting to accept connection on " << getPort() << " ... " << std::flush;
   }
   LcSocket tcpSocket = ::accept(socketNum, reinterpret_cast<struct sockaddr*>(&clientAddr), &cAddrSize);
   if (tcpSocket == INVALID_SOCKET) {
      if (isMessageEnabled(MSG_INFO)) {
          std::cout << " failed!" << std::endl;
      }
      return false;
   }

   if (isMessageEnabled(MSG_INFO)) {
       std::cout << "Accepted";
       char* ip = ::inet_ntoa(clientAddr.sin_addr);
       if (ip != nullptr) {
           std::cout << " connection from " << ip;
       }
       std::cout << std::endl;
   }

   // After accepting a connection we close the original opened socket and
   // we then assign socketNum to our local tcpSocket.
#if defined(WIN32)
   if (::closesocket(socketNum) == SOCKET_ERROR) {
#else
   if (::shutdown(socketNum, SHUT_RDWR) == SOCKET_ERROR) {
#endif
      std::perror("TcpServerSingle::acceptConnection(): shutdown original error! \n");
   }

   socketNum = tcpSocket;
   connected = true;
   connectionTerminated = false;

   // Set blocked or no-wait
   if (noWait) setNoWait();
   else setBlocked();

   if (isMessageEnabled(MSG_INFO)) {
       std::cout << "TcpServerSingle::acceptConnection: new socketNum = " << socketNum << std::endl;
   }

   return true;
}

}
Esempio n. 3
0
//------------------------------------------------------------------------------
// connectToServer() -- attempt to connect to the server
//------------------------------------------------------------------------------
bool TcpClient::connectToServer()
{
   connected = false;
   connectionTerminated = false;

   if (ipAddr == nullptr) return false;

   if (socketNum == INVALID_SOCKET) return false;

   struct sockaddr_in addr;        // Working address structure
   bzero(&addr, sizeof(addr));
   addr.sin_family = AF_INET;
   addr.sin_addr.s_addr = getNetAddr();
   addr.sin_port = htons(getPort());

   if (isMessageEnabled(MSG_INFO)) {
      std::cout << "Connecting to TCP server at " << ipAddr << ":" << getPort() << " ... " << std::flush;
   }

   if (::connect(socketNum, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)) == SOCKET_ERROR) {
      if (isMessageEnabled(MSG_INFO)) {
          std::cout << "Failed!" << std::endl;
      }
   }
   else {
      if (isMessageEnabled(MSG_INFO)) {
          std::cout << "Connected!" << std::endl;
      }
      connected = true;
   }
   if (isMessageEnabled(MSG_INFO)) {
      std::cout << "TcpClient::connectToServer: socketNum = " << socketNum << std::endl;
   }

   // Set blocked or no-wait
   if (noWait) setNoWait();
   else setBlocked();

   return connected;
}