PDA

View Full Version : cannot connect to UDP socket



saman_artorious
19th June 2013, 08:08
I wrote this code to connect to the ip address of my own machine. bind returns true, though connect wouldn't prompt anything. I also run another program, using same ip and port but these two do not connect.



void UDP::UDPtInit()
{
socketPort = PORT;
udpsocket = new QUdpSocket(this);
if(!udpsocket->bind(QHostAddress("192.168.111.1"),socketPort))
{
qDebug() << "true";
}
else
{
qDebug() << "false";
}
connect(udpsocket,SIGNAL(readyRead()),this,SLOT(re adState()));

connect(udpsocket,SIGNAL(connected()),this,SLOT(so cketConnected()));

connect(udpsocket,SIGNAL(error(QAbstractSocket::So cketError)),this,SLOT(socketError(QAbstractSocket: :SocketError)));

emit clientActive(true);
}


void UDP::socketError( QAbstractSocket::SocketError )
{
qDebug() << "UDPCLIENT ERROR: "<< udpsocket->errorString();
}


void UDP::socketConnected()
{
qDebug() << "UDPCLIENT : Socket connected!";
emit clientLogMessage(QString("UDPCLIENT : Connected !"));
}

wysota
19th June 2013, 08:21
UDP is a connectionless protocol.

saman_artorious
19th June 2013, 09:36
Server runs sending packets successfully, But Client fails at:

while(udpsocket->hasPendingDatagrams())

I have given them same IP and PORT address.

wysota
19th June 2013, 10:05
UDP has no notion of "client" and "server", only "peers". That aside, peers can't use the same socket, you have to give them different port numbers if they are to use the same IP.

ChrisW67
19th June 2013, 10:14
If the client and server are on different machines then:

I have given them same IP and PORT address.
makes no sense. If two machines have the same IP address then your network is misconfigured. If you have two processes on the same machine, say talking through the loop back interface, then they typically would not bind the same UDP port.

The IP address in bind() is not the address of the other end of a connection, its the address of an interface on the machine running the code. The IP address of the receiver is specified when you call writeDatagram().