//******UDP广播IP PORT,以及TCP监听链接请求***********
void AirLink::initNetWork()
{
std::cout << "initing network" << std::endl;
socketData = new QTcpSocket;
socketCtrl = new QTcpSocket;
udpSocket = new QUdpSocket;
QString localHostName = QHostInfo::localHostName();
qDebug() << "localHostName" << localHostName;
qDebug() << "localHostName0:" << QNetworkInterface().allAddresses().at(0).toString();
qDebug() << "localHostName1:" << QNetworkInterface().allAddresses().at(1).toString();
// qDebug() << "localHostName2:" << QNetworkInterface().allAddresses().at(2).toString();
// qDebug() << "localHostName3:" << QNetworkInterface().allAddresses().at(3).toString();
address=QNetworkInterface::allAddresses().at(2);
// address=QNetworkInterface::allAddresses().at(0);
QString ipAddress = address.toString();
qDebug() << "ipAddress" << ipAddress;
QString IPPort = QString("autorock-alive") + ":" + ipAddress + ":" + QString::number(SERVPORT);
localAddress = IPPort.toLatin1();/*QByteArray localAddress*/
qDebug() << "localAddress:" << localAddress;
/*广播本地IP及端口号*/
udpSocket->writeDatagram(localAddress,QHostAddress::Broadcast,UDPPort);
udpTimer.start(2000);
connect(&udpTimer, SIGNAL(timeout()), this, SLOT(sendIp()));
qDebug() << "**************************";
/*监听客户端的连接请求*/
serverData.setParent(this);
serverData.listen(QHostAddress::Any, SERVPORT);
serverCtrl.setParent(this);
serverCtrl.listen(QHostAddress::Any, SERVPORT + 1);
connect(&serverData, SIGNAL(newConnection()), this, SLOT(proConnectionData()));
connect(socketData, SIGNAL(connected()), this, SLOT(proConnectedData()));
connect(&serverCtrl, SIGNAL(newConnection()), this, SLOT(proConnectionCtrl()));
connect(socketCtrl, SIGNAL(connected()), this, SLOT(proConnectedCtrl()));
socketData->setReadBufferSize(1024*1024);//设置接收端缓冲区大小
std::cout << "network inited!" << std::endl;
}
//**********接受UDP广播的信息,以及请求链TCP接服务器
iint AirLink_lib::initNetWork()
{
char buffer[1024] = "\0";
int fromLen = sizeof(from_addr);
memset(&local_addr, 0 , sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(udpPORT); /*htons与htonl的区别,否则会出现链接不上的问题*/
if((udpServer_sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1) {
perror("create socket udpServer_sockfd error");
exit(1);
}
if(bind(udpServer_sockfd,(struct sockaddr *)&local_addr,sizeof(local_addr)) == -1) {
perror("bind error");
exit(1);
}
bzero(buffer,sizeof(buffer));
cout<<"waiting for server request..."<<endl;
int recvLen = recvfrom(udpServer_sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&from_addr, ( socklen_t *)&fromLen);
if(-1 == recvLen) {
perror("recvfrom error.");
exit(1);
}
cout << "receive a datagram from server:" << buffer << endl;
string udpData = buffer;
string dataHead = strtok((char *)udpData.c_str(),":");
string server_addr = strtok(NULL,":");
string strHOSTPORT = strtok(NULL,":");
int HOSTPORT = atoi((const char *)strHOSTPORT.c_str());
cout << "dataHead:" << dataHead << endl;
cout << "server_addr:" << server_addr << endl;
cout << "strHOSTPORT:" << strHOSTPORT << endl;
// char * tmp = (char *)server_addr.c_str();
if(dataHead == "autorock-alive") {
//socket for data
if((sockfdData = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("create socket sockfdData error");
exit(1);
}
memset(&serverAddr, 0 , sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr((const char *)server_addr.c_str());
serverAddr.sin_port = htons(HOSTPORT);//htons is diffent from htonl
if( -1 == connect(sockfdData, (struct sockaddr *)&serverAddr, sizeof(serverAddr))) {
perror("sockfdData connect error.");
exit(1);
}
//socket for ctrl
if((sockfdCtrl = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("create socket sockfdCtrl error");
exit(1);
}
memset(&serverAddr, 0 , sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr((const char *)server_addr.c_str());
serverAddr.sin_port = htons(HOSTPORT + 1);
if( -1 == connect(sockfdCtrl, (struct sockaddr *)&serverAddr, sizeof(serverAddr))) {
perror("sockfdCtrl connect error.");
exit(1);
}
}
close(udpServer_sockfd);
cout << "init complete." << endl;
}