Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:27:22+00:00 2026-05-31T13:27:22+00:00

I’m new in html5 and its websocket! now I’ve been trying to make my

  • 0

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!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:27:23+00:00Added an answer on May 31, 2026 at 1:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.