Пример #1
0
    int DatagramSocket::read(DatagramPacket &pkt)
    {
		int numbytes;
		sockaddr_in addr;
		socklen_t addr_len=sizeof(addr);

		if ((numbytes=recvfrom(*((socket_t*)m_sock), (char *)pkt.getBuffer().getData(), pkt.getBuffer().getLength(), 0,
						(sockaddr *)&addr, &addr_len)) == -1)
		{
			Error_send("Unable to receive packet from UDP socket\n");
		}

        pkt.setPort(addr.sin_port);
		pkt.setAddress(InetAddress(inet_ntoa(addr.sin_addr)));
		pkt.setLength(numbytes);

		return numbytes;
    }
Пример #2
0
    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;
    }