void DatagramSocket::send(const DatagramPacket& packet) throw(S1004LibException) { bool success= false; sockaddr_in recipient; recipient.sin_family= AF_INET; recipient.sin_port= htons(packet.getPort()); vector<InetAddress> results= InetAddress::getByName(packet.getAddress()); for(auto it= results.begin(); it != results.end(); it++) { int nBytes; inet_pton(recipient.sin_family, it->getHostAddress().c_str(), &(recipient.sin_addr)); nBytes= sendto(udpSocket, packet.getData().c_str(), packet.getLength(), 0, (struct sockaddr *) &recipient, sizeof(recipient)); success= success || (nBytes >= 0); } if (!success) { stringstream msg(stringstream::out); msg << "Error sending packet to " << packet.getAddress() << ":" << packet.getPort(); throw S1004LibException(msg.str()); } }
int DatagramSocket::write(const DatagramPacket &pkt) { int numbytes; sockaddr_in addr; socklen_t addr_len=sizeof(addr); memset(&addr, 0, addr_len); addr.sin_family=AF_INET; addr.sin_port=htons(pkt.getPort()); addr.sin_addr=*((in_addr*)pkt.getAddress().getPtr()); if ((numbytes=sendto(*((socket_t*)m_sock), (char *)pkt.getBuffer().getData(), pkt.getLength(), 0, (sockaddr *)&addr, addr_len)) == -1) { Error_send("Unable to send packet from UDP socket\n"); } return numbytes; }
//============================================================================== // PlainDatagramSocketImpl::send // //============================================================================== void PlainDatagramSocketImpl::send(const DatagramPacket& p) { testSocketIsValid(); int flags = 0; struct sockaddr* to=0; struct sockaddr_in sa; if(isConnected()) { if(p.getAddress() && !(m_rpRemoteAddr->equals(*(p.getAddress())))) { throw IllegalArgumentException(QC_T("Address in datagram packet does not match connected address")); } if(p.getPort()!= -1 && p.getPort()!=m_remotePort) { throw IllegalArgumentException(QC_T("Port in datagram packet does not match connected port")); } } else { // // If the socket is not connected, then the passed datagram packet must contain // valid information. // if(!p.getAddress() || p.getPort()==-1) { throw IllegalArgumentException(QC_T("datagram packet does not contain required address/port information")); } // // Use the InetAddress and port contained in the packet to // create and initialize a sockaddr_in structure. // ::memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(p.getPort()); // convert port to network byte order ::memcpy(&sa.sin_addr, p.getAddress()->getAddress(), p.getAddress()->getAddressLength()); to = reinterpret_cast<struct sockaddr*>(&sa); } if(Tracer::IsEnabled()) { Tracer::TraceBytes(Tracer::Net, Tracer::Low, QC_T("Datagram send:"), p.getData(), p.getLength()); } int rc = ::sendto(m_rpSocketDescriptor->getFD(), (const char*) p.getData(), p.getLength(), flags, to, to ? sizeof(struct sockaddr_in) : 0); if(rc<0) { static const String err(QC_T("error calling sendto ")); String errMsg = err + NetUtils::GetSocketErrorString(); throw SocketException(errMsg); } // // When an unbound datagram socket is used to send data, the system will allocate // it an address and ephemeral port. Let's see what weve been given! // if(!isBound()) { } }