clen=sizeof(clientaddr);
sockfdc=accept(sockfd, (struct sockaddr *)&clientaddr, &clen);
printf("client %d\n", sockfdc);
{
{
memset(buff,0,sizeof(buff));
rlen= recv(sockfdc,buff,sizeof(buff),0);
if(rlen<0)
continue;
printf("rlen=%d,[%s]", rlen, buff);
char sec[100];
{
QByteArray byteArray(buff);
QTextStream in(byteArray, QIODevice::ReadOnly);
QString line;
qDebug()<<QString(byteArray)<<"\r\n";
while(1)
{
line=in.readLine();
if(line.length()==0)
break;
if(line.startsWith("Sec-WebSocket-Key:")) //Sec-WebSocket-Key: WhfOD2pkbro36ifGQUvJdg==
{
qDebug()<<line;
QStringList list=line.split(":");
line=list[1];
line = line.simplified();
break;
}
}
{
QString mask = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; // 这是算法中要用到的固定字符串
//accept = base64( sha1( line + mask ) );
line.append(mask);
QByteArray string = line.toLatin1();
QCryptographicHash *hash=new QCryptographicHash(QCryptographicHash::Sha1);
hash->addData(string);
QByteArray sha1=hash->result();
line=sha1.toBase64();
}
memset(sec,0,sizeof(sec));
QByteArray arr=line.toLatin1();
strcpy(sec, arr.constData());
}
sockSendStr(sockfdc,"HTTP/1.1 101 Switching Protocols\r\n");
sockSendStr(sockfdc,"Server: Apache-Coyote/1.1\r\n");
sockSendStr(sockfdc,"X-Frame-Options: SAMEORIGIN\r\n");
sockSendStr(sockfdc,"X-UA-Compatible: IE=Edge,chrome=1\r\n");
sockSendStr(sockfdc,"Upgrade: websocket\r\n");
sockSendStr(sockfdc,"Connection: upgrade\r\n");
sockSendStr(sockfdc,"Sec-WebSocket-Accept:");
sockSendStr(sockfdc,sec);
sockSendStr(sockfdc,"\r\n");
sockSendStr(sockfdc,"\r\n");
{
sleep(30);
}
}
QT在windows模拟websocket
最新推荐文章于 2024-07-13 18:12:23 发布