예제 #1
0
		void TCPSocket::connect(const IPv4Address &addr, int port){
			struct sockaddr_in name;
			if(_socket == -1) create();
			name.sin_family = AF_INET;
			name.sin_port = htons(port);
			name.sin_addr = *addr.address();
			if(::connect(_socket, (struct sockaddr *)&name, sizeof(name)) != 0){
				int erx = errno;
				close();
				_socket = -1;
				switch(erx){
					case ETIMEDOUT:
						throw ConnectionTimeout();
						break;
					case ECONNREFUSED:
						throw ConnectionRefused();
						break;
					case ENETUNREACH:
						throw NetworkUnreachable();
						break;
					default:
						throw NetworkException(erx);
				}
			}
			in.open(_socket);
			out.open(_socket);
		}
예제 #2
0
    size_t
    UDPSocket::write(const uint8_t* buffer, size_t size, const Address& host, uint16_t port)
    {
      sockaddr_in host_sai;
      socklen_t sock_len = sizeof(host_sai);

      host_sai.sin_family = AF_INET;
      host_sai.sin_port = Utils::ByteCopy::toBE(port);
      host_sai.sin_addr.s_addr = host.toInteger();

      int rv = sendto(m_handle, (const char*)buffer, size, 0, (::sockaddr*)&host_sai, (::socklen_t)sock_len);

      if (rv == -1)
      {
        if (errno == EHOSTUNREACH)
          throw HostUnreachable(host.str());
        else if (errno == ENETUNREACH)
          throw NetworkUnreachable(host.str());
        else
          throw NetworkError(DTR("error sending data"), DUNE_SOCKET_ERROR);
      }

      return rv;
    }