I’m new in html5 and its websocket!
now I’ve been trying to make my own websocket server with c but
it’s difficult to me
I just wanna send “hello, world” to html client from c server but
I’m in trouble of handshaking
onMessage and onOpen both are not working in client side…!!
from what I know just server needs to send
HTTP/1.1 101 Switching Protocols\r\n
Upgrade: websocket\r\n
Connection: Upgrade\r\n
Sec-WebSocket-Accept: key I received + 258EAFA5-E914-47DA-95CA-C5AB0DC85B11
and it has to be done! // just from what I know…T_T
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================
what I wanna know is how server sends respond to client html even doesnt have connection yet!
I’m working in ubuntu so now use File pointer for socket programming
now my code sends respond above by using write() function with descripter of client socket
write(clnt_sock, query, sizeof(query));
but im not sure it’s right or not
==================================
it’s my C server
#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!!!
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".
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.