int UdpSocket::send(UdpPacket& packet)
{
    int ret = sendto(listenSocket, packet.getData(), packet.getSize(), 0,
            packet.getAddress().getAddress(), sizeof(*packet.getAddress().getAddress()));
    if (ret == SOCKET_ERROR && errno != ECONNREFUSED) {
        setInetError("Can't send UDP packet");
        return -1;
    }
    return 0;
}
Exemple #2
0
/**
 *  Send an UDP packet.
 *  @param packet The UDP packet to be sent. It includes the data and the
 *                destination address
 *  return 0 if ok, -1 if error
 */
int UdpSocket::send(UdpPacket &packet)
{
#ifdef DUMP
  TRACE_CLASS("UdpSocket", "send(UdpPacket)");
#endif
  int ret = sendto(listenSocket, packet.getData(), packet.getLength(), 0,
		   packet.getAddress().getAddress(), sizeof(*packet.getAddress().getAddress()));
  if (ret == SOCKET_ERROR
#ifndef _WIN32
      && errno != ECONNREFUSED
#endif
      ) {
    setInetError("Can't send UDP packet");
    return -1;
  }
  return 0;
}
Exemple #3
0
/**
 *  Receive an UDP packet.
 *  @param packet The packet that will receive data. The address will be set
 *                to the source address.
 *  @return 0 if ok, -1 if error
 */
int UdpSocket::receive(UdpPacket &packet)
{
  TRACE_CLASS("UdpSocket", "receive(UdpPacket)");
  socklen_t addrSize;
  addrSize = sizeof(*packet.getAddress().getAddress());
  int ret = recvfrom(listenSocket, packet.getData(), packet.getSize() - packet.getOffset(), 0,
		     packet.getAddress().getAddress(), &addrSize);
  if (ret == SOCKET_ERROR) {
    packet.setLength(0);
#ifndef _WIN32
  if (errno == EAGAIN)
    return 0;
#endif
    setInetError("Can't receive UDP packet");
    return -1;
  }
  packet.setLength(ret);
  if (ret == (long)packet.getSize()) {
    packet.setSize(packet.getSize() << 1);
  }
  return 0;
}
int UdpSocket::receive(UdpPacket& packet)
{
    socklen_t addrSize;
    addrSize = sizeof(*packet.getAddress().getAddress());
    ssize_t ret = recvfrom(listenSocket,
            packet.getData(),
            packet.getSize(),
            0,
            packet.getAddress().getAddress(),
            &addrSize);

    if (ret == SOCKET_ERROR) {
        packet.setSize(0);
        if (errno == EAGAIN) {
            return 0;
        }
        setInetError("Can't receive UDP packet");
        return -1;
    }

    packet.setSize(ret);
    return 0;
}