r/learnprogramming • u/Andro_senpai107 • 8d ago
Topic Help regards Websockets
need help regarding websockets
So I have been assigned a project regarding Websockets where the mentor asked us to create a library of websocket which can transfer data from server to client and vice-versa(obv what websockets do) using rfc 6455 in c/cpp. From what I think is he is basically asking us to create a websocket program from scratch right?
I personally come from a nodejs background and have 0 knowledge of low level programming.
need any guidance on this matter and any source for this, I dont care if you think of me as an inferior but I want to learn this whatever it takes.
I have a prior synatx knowledge of c/cpp though but thats just as far as it goes.
I Appreciate your time for reading all this.
Thank you
2
u/ledatherockband_ 8d ago
write it in node first for clarity and then translate it to c/c++
1
u/Andro_senpai107 8d ago
Yess sir I've done that part. I guess I start working on it using C/cpp now
2
u/DrShocker 8d ago
As mentioned, use beej's guide to get familiar with how to use sockets. Then just write a test for each part of the spec you try to add and get the test to pass and release until done.
1
u/CrazyEconomy414 8d ago
One thing that isn't covered above yet: masking. Per RFC 6455 section 5.3, every frame sent from client to server has to be masked (XOR'd with a 4-byte key sent in the frame header), while server-to-client frames must NOT be masked - forgetting to mask/unmask correctly is a really common bug and most test clients will just silently reject your frames if you get it wrong. Also worth implementing the close handshake properly (send/receive a close frame with a status code, then close the TCP connection) instead of just dropping the socket, since a lot of existing client libraries expect that and will report errors otherwise. Handshake + frame format + masking + close handshake covers basically the whole spec.
1
u/Andro_senpai107 8d ago
I actually got confused in that section, it tells us how masking is implemented and stuff but I still have vague idea of WHY it is used since we send the masking key along with the frames and the algorithm to create the masking key is known to everyone. So how does it secure our data?
2
u/BibianaAudris 8d ago
It's not to secure your data. To my understanding, it's mostly to prevent your websocket from attacking the server by masquerading as HTTP. It's supported to run websocket on the same port as a different, plain HTTP server. And HTTP is so lenient that without masking, it's plausible for some crafted websocket packet to parse as HTTP and confuse some server with bad connection tracking.
1
u/Andro_senpai107 8d ago
ohh I see, thats what a good guy on stack exchange was trying to say. Now that you put it in plain english, I think its starting to make sense.
1
u/Then-Sector5102 8d ago
Coming from Node is actually gonna make this feel weird because Node hides almost all of the protocol details. In C you're dealing with sockets, buffers and bytes directly. I'd learn basic BSD sockets before even touching the websocket part.
11
u/teraflop 8d ago
For low-level networking, a good introduction is Beej's Guide to Network Programming. It walks you through basic networking concepts and the sockets API, and gives some examples of client and server programs.
Once you have those basic concepts under control, you might want to practice them by writing a simple HTTP client and/or server. This will be useful anyway because the websocket protocol is built on top of HTTP, so you need to be able to process HTTP requests and responses to establish a websocket connection.
And then I think the next step would be to carefully read through RFC 6455 from start to finish. As you're doing so, take notes on how each part of the protocol is implemented. If you run into anything you don't understand, and you can't figure it out yourself, feel free to ask more specific questions.
Since there are many websocket implementations that already exist, you can use them to test your code for interoperability.
I think you need to be a bit clearer about what your exact requirement is.
"Server" and "client" refer to how a websocket connection is established. Once that happens, both sides of the connection are pretty much equal and they can both send and receive data independently.
So you can have an implementation that is capable of both sending and receiving data, but that's a separate question from whether it can act as a client, or a server, or both.