#include "udpsender.h"


UdpSender::UdpSender(QString IP, quint16 Port)
{
	sock = new QUdpSocket();
    qDebug() << sock->state();
    //cout << sock->socketDescriptor() << endl;
    //qDebug() << sock->socketDescriptor();
	addr = QHostAddress(IP);
	port = Port;
    connect(sock, SIGNAL(readyRead()),this, SLOT(slotRead()));
}


UdpSender::~UdpSender()
{
    sock->close();
	delete sock;
}


void UdpSender::slotRead()
{
    QString s;
    QByteArray * recdata = new QByteArray();
    *recdata = sock->readAll();
    s = *recdata;
    qDebug() << "Received data: " << s;
    //for (int i = 0; i < s.length(); i++) cout << s[i].toAscii();
    //cout << endl;
    //qDebug() << sock->write(recstr.toUtf8());
    qDebug() << sock->socketDescriptor();
}


void UdpSender::connectSend(QString s)
{
	sz = s.length();
    sock->connectToHost(addr,port);
    qDebug() << sock->error();
    qDebug() << sock->state();
    qDebug() << sock->socketDescriptor();
    if (sock->write(s.toUtf8())>0) qDebug() << "writen!";
}
