void UDPSocket::disconnect() throw(SocketException) {
  sockaddr_in nullAddr;
  memset(&nullAddr, 0, sizeof(nullAddr));
  nullAddr.sin_family = AF_UNSPEC;

  // Try to disconnect
  if (::connect(sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0) {
   #ifdef WIN32
    if (errno != WSAEAFNOSUPPORT) {
   #else
    if (errno != EAFNOSUPPORT) {
   #endif
      throw SocketException("Disconnect failed (connect())", true);
    }
  }
}

void UDPSocket::sendTo(const void *buffer, int bufferLen,
    const string &foreignAddress, unsigned short foreignPort)
    throw(SocketException) {
  sockaddr_in destAddr;
  fillAddr(foreignAddress, foreignPort, destAddr);

  // Write out the whole buffer as a single message.
  if (sendto(sockDesc, (raw_type *) buffer, bufferLen, 0,
             (sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen) {
    throw SocketException("Send failed (sendto())", true);
  }
}
예제 #2
0
void CommunicatingSocket::connect(const string &foreignAddress,unsigned short foreignPort) throw(SocketException) {
  //   Get the address of the requested host
  sockaddr_in destAddr;
  fillAddr(foreignAddress, foreignPort, destAddr);

  //   Try to connect to the given port
  if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0)   throw SocketException("Connect failed (connect())", true);
}
void Socket::setLocalAddressAndPort(const string &localAddress,
    unsigned short localPort) throw(SocketException) {
  // Get the address of the requested host
  sockaddr_in localAddr;
  fillAddr(localAddress, localPort, localAddr);

  if (::bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
    throw SocketException("Set of local address and port failed (bind())", true);
  }
}
예제 #4
0
파일: socket.cpp 프로젝트: alyst/zorba
void Socket::setLocalAddressAndPort(const std::string &localAddress,
    unsigned short localPort) {
  // Get the address of the requested host
  sockaddr_in localAddr;
  fillAddr(localAddress, localPort, localAddr);
  int opt = 1;
  setsockopt(theDescriptor, SOL_SOCKET,SO_REUSEADDR, (char *)&opt, (socklen_t)sizeof(opt)); 
  if (bind(theDescriptor, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
    throw DebuggerSocketException("Set of local address and port failed (bind())", true);
  }
}
예제 #5
0
파일: socket.cpp 프로젝트: alyst/zorba
void CommunicatingSocket::connect(const std::string &foreignAddress,
    unsigned short foreignPort) {
  // Get the address of the requested host
  sockaddr_in destAddr;
  fillAddr(foreignAddress, foreignPort, destAddr);

  // Try to connect to the given port
  if (::connect(theDescriptor, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
    std::stringstream lMsg;
    lMsg << "Connection @" << foreignAddress << ":" << foreignPort << " failed.";
    throw DebuggerSocketException( lMsg.str(), true);
  }
}
예제 #6
0
void CSocket::setLocalAddressAndPort(const string &localAddress,
    unsigned short localPort) throw(SocketException) {
	//   Get the address of the requested host
	sockaddr_in localAddr;
	fillAddr(localAddress, localPort, localAddr);
	int on = 1;
#ifdef WIN32
	setsockopt(sockDesc, SOL_SOCKET, SO_REUSEADDR, (char*) &on, sizeof(on));
#else
	setsockopt(sockDesc, SOL_SOCKET, SO_REUSEADDR, (void*) &on, sizeof(on));
#endif
	if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0)  throw SocketException("Set of local address and port failed (bind())", true);
}
예제 #7
0
void CommunicatingSocket::connect(const string &foreignAddress,
    unsigned short foreignPort) throw(ClassException<Socket>) {
  // Get the address of the requested host
  sockaddr_in destAddr;
  fillAddr(foreignAddress, foreignPort, destAddr);

  cout<<"connect to "<<foreignAddress<<':'<<foreignPort<<'\t';
  cout.flush();
  // Try to connect to the given port
  if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
    throw ClassException<Socket>("Connect failed (connect())", true);
  }
  cout<<"OK!\n";
}
예제 #8
0
void Socket::sendAndReceive (const std::string& destination_address, void* data, size_t size, std::function<int(void*)> callback, const bool broadcast)
{
    sockaddr_in destAddr = fillAddr(destination_address, STANDARD_GVCP_PORT);
    setBroadcast(broadcast);

    ssize_t send = sendto(fd, (uint8_t*)data, size, 0, (struct sockaddr*)&destAddr, sizeof(destAddr));
    if (send <= 0)
    {
        throw SocketSendToException();
    }
    else
    {
        // we have nothing to wait for end just end here
        if (callback == NULL)
        {
            return;
        }

        timeval timeout;
        timeout.tv_sec = timeout_ms / 1000;
        timeout.tv_usec = (timeout_ms % 1000) * 1000;

        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(fd, &fds);

        while (select(fd+1, &fds, NULL, NULL, &timeout) > 0)
        {
            char msg[1024];

            struct sockaddr_storage sender = sockaddr_storage();
            socklen_t sendsize = 0;

            if (recvfrom(fd, msg, sizeof(msg), 0, (sockaddr*)&sender, &sendsize) >= 0)
            {
                if (callback(msg) == SendAndReceiveSignals::END)
                {
                    return;
                }

                // not working due to gcc bug
                //auto cam = std::make_shared<Camera>(ack, interfaces.at(i).socket, interfaces.at(i).name);
            }
        }
    }
}
예제 #9
0
/*
UDPSocket::UDPSocket(const string &localAddress, unsigned short localPort) 
     throw(ClassException<Socket>) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
  setLocalAddressAndPort(localAddress, localPort);
  setBroadcast();
}
*/
UDPSocket::UDPSocket(const string &foreignAddress, unsigned short foreignPort) 
     throw(ClassException<Socket>) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
  fillAddr(foreignAddress, foreignPort, mDestAddr);
  setBroadcast();
}