问题描述
我是html5及其websocket的新手!
I'm new in html5 and its websocket!
现在我一直在尝试使用c制作我自己的websocket服务器但是
now I've been trying to make my own websocket server with c but
这对我来说很难
我只是想从c服务器向html客户端发送hello,world但是
I just wanna send "hello, world" to html client from c server but
我遇到握手问题
onMessage和onOpen都不能在客户端工作...... !!
onMessage and onOpen both are not working in client side...!!
据我所知,服务器需要发送
from what I know just server needs to send
HTTP / 1.1 101交换协议\\ n
HTTP/1.1 101 Switching Protocols\r\n
升级:websocket\r\ n
Upgrade: websocket\r\n
连接:升级\\\\ n
Connection: Upgrade\r\n
Sec-WebSocket-Accept:我收到的钥匙+ 258EAFA5-E914-47DA-95CA-C5AB0DC85B11
Sec-WebSocket-Accept: key I received + 258EAFA5-E914-47DA-95CA-C5AB0DC85B11
并且必须完成! //只是从我所知道的... T_T
and it has to be done! // just from what I know...T_T
我在google上运行了一段时间,但直到现在它还没有用!
I made a bit by running on google but until now it's not working!
你能让我知道我的错误是什么吗?
could you let me know what is my mistake??
=========== summary ====== ==========
===========summary================
我想知道的是服务器发送如何响应客户端html甚至还没有连接!
what I wanna know is how server sends respond to client html even doesnt have connection yet!
我在ubuntu工作所以现在使用File指针进行套接字编程
现在我的代码通过使用write()函数和客户端套接字的描述符发送响应
I'm working in ubuntu so now use File pointer for socket programmingnow my code sends respond above by using write() function with descripter of client socket
write(clnt_sock, query, sizeof(query));
但我不确定是不是
==================================
==================================
这是我的C服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void error_handling(char *message);
int main(int argc, char *argv[])
{
int serv_sock;
int clnt_sock;
int i=0;
char readHeader[500]={0};
char* parsingHeader[10];
char* saveOrigin;
char* saveKey;
char* magicKey="258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
char query[105]="HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ";
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size;
char message[]="Hello World!";
if(argc!=2){
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
serv_sock=socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socket() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1 )
error_handling("bind() error");
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
clnt_addr_size=sizeof(clnt_addr);
clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_addr,&clnt_addr_size);
if(clnt_sock==-1)
error_handling("accept() error");
read(clnt_sock,readHeader,355);
parsingHeader[0]=strtok(readHeader,"\r\n");
for(i=1;i<6;i++)
parsingHeader[i]=strtok(NULL,"\r\n");
saveOrigin=strtok(parsingHeader[4]," ");
saveOrigin=strtok(NULL," ");
saveKey=strtok(parsingHeader[5]," ");
saveKey=strtok(NULL," ");
strcat(saveKey,magicKey);
strcat(query,saveKey);
strcat(query,"\r\n");
strcat(query,"\r\n");
printf("%s",query);
write(clnt_sock, query, sizeof(query));
close(clnt_sock);
close(serv_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}`enter code here`
感谢您阅读我的问题!!!
thank you for reading my question!!!
推荐答案
您不能依赖客户端按照定义的顺序发送标头。你应该循环遍历parsingHeader中的所有项目,直到你找到Sec-WebSocket-Key。而
You can't rely on the client sending headers in a defined order. Rather than taking the value of parsingHeader[5], you should loop through all items in parsingHeader until you find "Sec-WebSocket-Key".
之后,你需要取代parsingHeader [5]的值。更改您回复的密钥。 描述了这一点。将magicKey附加到saveKey之后,应该使用sha-1哈希,然后对其进行base64编码。 包含生成sha-1哈希的代码。有许多开源base64编码器可供使用,例如。
After that, you need to change the key you respond with. RFC 6455 describes this. After you have appended magicKey to saveKey, you should take the sha-1 hash of this then base64-encode it. RFC 3174 includes code to generate the sha-1 hash. There are lots of open source base64 encoders available, like this one.
这篇关于C代码中握手WebSockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!