learn network programming in c, but without all those pesky sockets

  Рет қаралды 96,075

Low Level Learning

Low Level Learning

Жыл бұрын

When learning to program, one of the first advanced projects you'll get is a networking project. You may even have this in your classes right now where you need to send data from a client to a server and vice versa. This isn't an easy task for a new programmer, and while you should definitely learn the C networking API for Linux or Windows, there's an easier way to do this.
ZeroMQ or ZMQ is a message queue library that provides an abstraction over the socket API to make networking easier in C, as well as many other languages. In this video, we go over a basic implementation of the ZMQ library, using a request response schema for a basic networking application.
ZMQ API: api.zeromq.org/
🏫 COURSES 🏫
www.udemy.com/course/c-progra...
🔥🔥🔥 SOCIALS 🔥🔥🔥
Low Level Merch!: www.linktr.ee/lowlevellearning
Follow me on Twitter: / lowleveltweets
Follow me on Twitch: / lowlevellearning
Join me on Discord!: / discord

Пікірлер: 130
@LowLevelLearning
@LowLevelLearning Жыл бұрын
Leave a comment, let me know what YOUR favorite networking library is!
@TheStuartstardust
@TheStuartstardust Жыл бұрын
Python 😁
@31redorange08
@31redorange08 Жыл бұрын
The standard library of modern languages.
@corruption781
@corruption781 Жыл бұрын
bruh you use libraries
@ibrahimaba8966
@ibrahimaba8966 Жыл бұрын
zmq is the best library, this lib has so many usefull patterns such as PUB SUB, ROUTER DEALER, etc..! I love it.!!!!
@zrodger2296
@zrodger2296 Жыл бұрын
I used to use MPICH all the time on a Beowulf cluster.
@kerboplaytv8744
@kerboplaytv8744 Жыл бұрын
What’s wrong with the linux sockets API? It’s actually very logical and clean, I don’t understand how this is any better.
@ibrahimaba8966
@ibrahimaba8966 Жыл бұрын
Hello, zmq has a bunch of patterns such as PUSH PULL, ROUTER DEALER. This lib is awesome.
@conorstewart2214
@conorstewart2214 Жыл бұрын
This here didn’t seem as good as the normal socket libraries. Maybe it has lots of advanced features that weren’t shown but it didn’t seem any better.
@rosen8757
@rosen8757 Жыл бұрын
I agree, just normal plain sockets looks just as good as this.
@Mankindux
@Mankindux Жыл бұрын
plain sockets are fine but you have to take care of lot of things yourself like handling partial messages, in UDP reassembling and reordering packets and so on. however there are many alternatives to zeromq which are less bloat, furthermore zeromq is written in C++.
@thisisreallyme3130
@thisisreallyme3130 Жыл бұрын
Thanks! I knew about 0mq solving pub sub problem.. ok… but NOBODY ever said it’s also easier. Now I have a fresh compelling reason to check it out .
@0xgerbot_500
@0xgerbot_500 Жыл бұрын
Awesome video, that PTSD from the socket API got me looking for alternatives. Thank you very much!
@johndurham7703
@johndurham7703 Жыл бұрын
This is sweet. Sockets is fine but I'm already bangin away at some crazy stuff with this API. Thanks L3!
@arion_vulgaris
@arion_vulgaris Жыл бұрын
I tried to use zmq once in the project, but realized that it does not guarantee delivery on its own. I read parts of zmq source and it appears to me that data, which was sent from zmq and is then in OS network buffer, may get lost when TCP connection will break. Zmq will do re-connection for you, but data from OS buffer may never get to the destination. So without implementing some acknowledge protocol over zmq messages I could not be sure that data put to zmq will be actually delivered. I tried to do that and it was like re-implementing TCP in simplified form. Another solution I came up was to listen to socket events (or whatever it was called) and reacting on disconnections in some way.
@Modus_Pwnin
@Modus_Pwnin Жыл бұрын
I love this video. Thank you so much. Now i want MORE SOCKET RELATED PROJECTS
@orko3661
@orko3661 Жыл бұрын
Hi, wouldn't the zsock_destroy function in server.c never be called ? Wouldn't it be better to setup a function whenever a SIGINT is received that destroys the socket and terminate the server ?
@ivelinkarageorgiev3111
@ivelinkarageorgiev3111 Жыл бұрын
Better idea
@paulzupan3732
@paulzupan3732 7 ай бұрын
From the documentation for zstr_recv: Returns NULL if the context is being terminated or the process was interrupted. Maybe check for NULL and leave the loop?
@MellexLabs
@MellexLabs Жыл бұрын
I have been using ZMQ for awhile now to build distributed computing software for Manufacturing and Execution Systems and it works really great... can do anything from normal pub sub and req resp patterns to esoteric patterns like radio dish and multi stage compute pipelines. Maybe someday someone can show it working on an ESP32 or similar...
@imveryhungry112
@imveryhungry112 8 ай бұрын
i wish i was smart enough to understand ZMQ :(
@edinetgrunhed6000
@edinetgrunhed6000 Ай бұрын
THANKS for this video, im looking this kind of video for ages
@rodolfoblasser3329
@rodolfoblasser3329 Жыл бұрын
Awesome! Looks promising for embedded devs.
@ghostlucian12
@ghostlucian12 Жыл бұрын
What I like about sockets are the IO Strategy, especially multiplexing, to handle multiple sockets, using select option to select only the sockets that has pending data, and IOCTL where you can check the state of a socket or set a block state to a socket. For socket strategy there is IOStrategy, Async and Multi-threaded, - IO strategy uses a single thread where it selects sockets that have pending data to handle them, - Async uses opens and closes or uses a thread-pool to handle sockets in separate threads, if you have to synchronize data received from the sockets you have to synchronize it manually - Multi-threaded handles sockets in separate threads, in my opinion multi-threaded should be avoided, it is the easiest to handle but the slowest when handling a lot of clients, - There is also an hybrid mode where using IO Strategy in separate threads as socket channels, for example I also wrote 3 layers of abstraction for the socket library in c++, Socket->DataSocket ->Listening and Connection socket, after this I wrote the communication protocol, because send and recv, does not ensure, you will receive the full message, because if the message is too big, it will be broken into chunks or receive part of the next message, especially if you have blocking turned off per socket A good socket library should have options to use their own strategy(IOStrategy, Async, Multi-threaded, etc,) to handle sockets, an easy to use listening and connection socket, also a communication protocol,
@miklov
@miklov Жыл бұрын
Interesting how zmq have stuff running post main to detect non freed resources. Maybe that's a signal handler or something, will grep for dangling in the source when I have an opportunity. Thanks for the video!
@nuttolum
@nuttolum Жыл бұрын
a week ago i had no experience in c and 2 days later i made a web server hosting a simple api using winsock2 thanks to discovering this channel! i really enjoy your c videos especially!
@billigerfusel
@billigerfusel Жыл бұрын
It would been 2 hours if you used python lol
@____-pb1lg
@____-pb1lg Жыл бұрын
@@billigerfusel python saves dev time, C saves user time
@nuttolum
@nuttolum Жыл бұрын
@@billigerfusel more like 2 minutes lmao
@TheSimoc
@TheSimoc Жыл бұрын
@@billigerfusel With no prior experience, I really doubt..
@esben181
@esben181 Жыл бұрын
Python is too abstract to make sense imo
@aah134-K
@aah134-K 5 ай бұрын
If you are running it for sometimes, A client sends a request then crash, Rerunning the client may have a sync issue! How to deal with that situation? A server is sending a message but no client to read, and the new client is sending a message server is busy
@kevinstefanov2841
@kevinstefanov2841 Жыл бұрын
Do you have / are you going to make a video on the Linux Sockets API?
@alexandrohdez3982
@alexandrohdez3982 Жыл бұрын
It would be nice a Demo using libcurl library and then integrates with a json parser library 😉. Great video 🙏
@RealNekoGamer
@RealNekoGamer Жыл бұрын
I've heard of ZeroMQ but not CZMQ. Is it like a more simplified version? As for my favorite net lib? In C, I'm using the sockets API directly, with Winsock startup/shutdown boilerplate, because I need like raw TCP/UDP a lot of the time. I want to use Rust, but that's where networking gets... complicated, especially with concurrency, which I'm also struggling with, though the channel semantics look promising. The `tokio` crate seems like the go-to, but if you need a program to do expensive calculations as well (ie. a server-side authority game server), that's where it falls short. I was told to use std::net in conjunction with the `rayon` crate in such cases, but concurrency and networking on that scale makes brain soup.
@ForeverNils
@ForeverNils Жыл бұрын
cool but what if we need to serve 1000 clients asynchronously? maybe io_uring (liburing) will help?
@jasonfernandez1386
@jasonfernandez1386 Жыл бұрын
Why didn't localhost work on the server side?
@edgeeffect
@edgeeffect Жыл бұрын
In my day job (sadly) I do a lot of web development... this seems very familiar. Nice.
@ivelinkarageorgiev3111
@ivelinkarageorgiev3111 Жыл бұрын
Thanks man, awesome vid. I've just started to use this library to deploy C code directly to a Raspberry Pi robot of mine. it's super useful. Keep Rocking \m/
@sergiocoder
@sergiocoder Ай бұрын
Isn't the socket freed by the OS when a process dies? I always thought it's just a good practice to clean everything, but not strictly needed
@deadeye1982a
@deadeye1982a Жыл бұрын
I used ~5 years ago zmq for a RADAR-System to transmit processed I/Q-Data to a websocket-server. From there on, the clients received the data via websockets inside the provided web application.
@wandrewp
@wandrewp Жыл бұрын
Similar application here, I found 0MQ in GNURadio so I used it there.
@SpeedingFlare
@SpeedingFlare Жыл бұрын
Happy 100k subs!!!
@LowLevelLearning
@LowLevelLearning Жыл бұрын
Thanks!!
@thisisreallyme3130
@thisisreallyme3130 Жыл бұрын
Thanks. Maybe someday it’d be great to do the same thing in sockets, revisit 0mq at the same time… sort of a compare and contrast.
@unperrier5998
@unperrier5998 Жыл бұрын
It's nice but so basic. Maybe the rest of the library is more advanced, like for example handle multiple streams concurrently (have a context for the underlying socket) or asynchronous networking like select/epoll.
@waytospergtherebro
@waytospergtherebro Жыл бұрын
Your reading comprehension is the only thing that's basic here.
@unperrier5998
@unperrier5998 Жыл бұрын
@@waytospergtherebro how does it feel to be mean?
@CallousCoder
@CallousCoder Жыл бұрын
@@waytospergtherebro go shag a cactus 🌵
@conorstewart2214
@conorstewart2214 Жыл бұрын
@@waytospergtherebro no idea where that came from, are you okay? Most people who have used sockets would look at this example and think what is the point of that, since this library doesn’t seem to have any benefits over just using sockets. This is a very basic example however and doesn’t show off any advanced features.
@otmanm4095
@otmanm4095 Жыл бұрын
Bro, thanks you!
@zacishtaar674
@zacishtaar674 Жыл бұрын
I wish you do a series solving 42 networks school exercises.
@dijkstra4678
@dijkstra4678 Жыл бұрын
at first looking at the title I thought this man was gonna interface the network card directly
@qeqsiquemechanical9041
@qeqsiquemechanical9041 10 ай бұрын
that's cool, but what about performance tradeoff?
@programmingdesign4382
@programmingdesign4382 Жыл бұрын
Spent all day trying to compile ZeroMQ in Visual Studio 2019 with its C++ binding. Learned I've to move on and find something else. Maybe I can compile its C binding on Linux and use the generated library back on Visual Studio (Windows)? Don't want to lose more hours. Also, couldn't find an understandable example of how to implement async named pipes on Windows with C++.
@bacon7837
@bacon7837 3 ай бұрын
Can you please make a full tutorial on making a C http server for beginners
@Baal3033
@Baal3033 Жыл бұрын
0:06 I do actually have a C networking assisgnment right now :D
@redmundperrz7234
@redmundperrz7234 Жыл бұрын
This got my attention 😮😮
@wandrewp
@wandrewp Жыл бұрын
Didn’t you need to allocate a number of bytes for the returned msg string? You just have char *msg in the server and char *str in the client without allocation. Maybe I’m looking too far into this quick demo but just curious as welll.
@LowLevelLearning
@LowLevelLearning Жыл бұрын
The receiver allocates it for you :)
@guilherme5094
@guilherme5094 Жыл бұрын
👍Thanks.
@wazzamolloy
@wazzamolloy Жыл бұрын
This is messaging middle-ware and not network programming. They are different users completely. And not just an abstraction. Also the TCP/IP network programming API is from UNIX not Linux.
@abdox86
@abdox86 Жыл бұрын
Nahh, I’ll just stick to sockets, plus it’s not doing a lot of abstraction, still bind and connect, what if u wanna use UDP instead of TCP !! though I really loved the way how it giving a warning about the not freed socket.
@debuti
@debuti Жыл бұрын
Have you heard about DDS?
@5tr41ghtGuy
@5tr41ghtGuy Жыл бұрын
Before investing your time in 0MQ, here are a few considerations: * The sockets API is well documented, and underlies all IP based networking software. You'll need to learn it sooner or later. * 0MQ is not a replacement for the sockets API; rather, 0MQ is a pub/sub networking toolkit built on top of the sockets API. * NATS is a similar pub/sub toolkit which is widely used in industry, but with better intrinsic security than OMQ. * If you want to write networking software which is as reliable as possible, you will need to learn how to use non-blocking I/O with a single thread. 0MQ (and NATS) kick this can down the road by using seperate threads under the hood to buffer messages.
@mihailmojsoski4202
@mihailmojsoski4202 Жыл бұрын
"you will need to learn how to use non-blocking I/O with a single thread" why not use non-blocking I/O on multiple threads
@5tr41ghtGuy
@5tr41ghtGuy Жыл бұрын
@@mihailmojsoski4202 nothing against learning to do that; I do this myself when I must. Multi thread programming is often a tar pit full of bugs which are very difficult to find, and may manifest differently depending on the number of CPU cores.. For many applications programs, a single thread + non-blocking I/O can easily saturate the system's network I/O capability.
@_thehunter_
@_thehunter_ Жыл бұрын
very rarely people talk about zeromq
@FaZekiller-qe3uf
@FaZekiller-qe3uf Жыл бұрын
Nice.
@LowLevelLearning
@LowLevelLearning Жыл бұрын
Very nice
@NullCyan
@NullCyan 7 ай бұрын
0:40 shrek in the top left corner
@benjaminhon86
@benjaminhon86 Жыл бұрын
Zeromq rocks
@yassinesafraoui
@yassinesafraoui 3 ай бұрын
If you're doing networking in C to learn C, this is good, if you're doing networking in C to learn networking, better use the socket API to handle all the networking yourself and understand it more
@sezarstarscourge7368
@sezarstarscourge7368 Жыл бұрын
cool
@imveryhungry112
@imveryhungry112 8 ай бұрын
I wish i was smarter so I could understand ZMQ :(
@JohnDoe-sp3dc
@JohnDoe-sp3dc 11 ай бұрын
LocalHorse Gang checking in here
@kylemetscher4923
@kylemetscher4923 Жыл бұрын
8:18 it's not C if there isn't at least one segfault
@ZenoTasedro
@ZenoTasedro Жыл бұрын
I was HUGE ZMQ fan, but I don't think it's grown as much after the passing of Hintjens, their BDFL. I once re-architected the provisioning system for a cloud backend with ZeroMQ and it was such a perfect fit for a distributed, lock-free concurrency across global data centers. My biggest reason to not use ZeroMQ nowadays is a relative lack of security standards across bindings/libraries, particularly for authN and encryption. It also has relatively weak support for some programming languages, like JavaScript. Still, what's better than zeroMQ for building distributed systems? Nothing as far as I am aware 😅
@ZenoTasedro
@ZenoTasedro Жыл бұрын
I may someday make my own ZeroMQ alternative, it's just so much work 🥹. It would have to be written in Rust! 🤣🦀
@rabbitcreative
@rabbitcreative Жыл бұрын
> what's better than zeroMQ for building distributed systems? Erlang?
@OmniscientOCE
@OmniscientOCE Жыл бұрын
I think there's a newer one called nanomsg but I haven't used it
@ZenoTasedro
@ZenoTasedro Жыл бұрын
@@rabbitcreative the language of 9s!
@kayakMike1000
@kayakMike1000 Жыл бұрын
ØMQ is a facade then? It kinda sounds like a Plan9 channel.
@keyboard_toucher
@keyboard_toucher Жыл бұрын
Remove `sleep(1);`
@everortega3251
@everortega3251 Ай бұрын
128 comments, now 129
@DeathSugar
@DeathSugar Ай бұрын
> networking is not hard it's hard when you get out of ping-pong client-server code. multiple client support (read - threading), buffered reading, all kinds of security things and wrapping your data inside of it - thats when things start to get hairy, and raw sockets, binding and stuff ain't actually the scariest part at this point.
@JustPlainRob
@JustPlainRob 8 ай бұрын
Ah yes, all those pesky functions that do the exact thing I want them to. To any students watching: Learn the damn socket api. Don't use this crap. Save higher level abstraction until you learn and understand what happens under the hood.
@CallousCoder
@CallousCoder Жыл бұрын
You forgot an exit after your bind check. Now it will still go into the loop. Ugh the receive call returns a pointer to data?! That is a daft implementation. You would want to pass a point to a data block, Because now you need to free the pointer to the data after everything call. Otherwise when you pass in a data pointer you would just free it after your client is done. This returning of a data pointer is not very C-like.
@KingJellyfishII
@KingJellyfishII Жыл бұрын
so you'd rather have either a buffer overflow exploit or require many lines of boilerplate code instead of being not C-like?
@CallousCoder
@CallousCoder Жыл бұрын
@@KingJellyfishII I don’t think you see the incredible danger here. As this is a memory leak waiting to happen! And you don’t get a buffer overflow by pre defining a buffer and telling read() how many bytes to receive. It will never return more than specified, that’s the whole purpose of telling the number of bytes to read in the first place. Actually this also is a great way to get a memory overflow and cripple a whole system, because a server can create a stream that’s infinitely long and the way I see it, this receive call will most just realloc and keep growing the buffer until memory is exhausted. Otherwise how does the user know it only got a partial packet?? Nothing is telling you that here. So this in any case this library is worse (and just not good C practice) than predefining a buffer, that you have full control over and will never overrun if you out the right size in read(). And you can manage large streams without running out of memory when you do it yourself. And I wrote my own wrappers ages ago to handle all this, so I dealt with the boiler plate ones. Take it from someone who’s been developing client server applications since 1997.
@KingJellyfishII
@KingJellyfishII Жыл бұрын
@@CallousCoder Thanks for your detailed response. I think my annoyance comes from constantly having to implement a resizable buffer using realloc for some annoying protocol that uses essentially null termination of packets (that are required to be ASCII). However, i realise that most protocols are probably less annoying than that.
@CallousCoder
@CallousCoder Жыл бұрын
@@KingJellyfishII yeah I feel you, been there done that once. Because you need need to then know when to stop reading. Most protocols actually put a size in the header or message are fixed size. That’s generally how protocol design is done. And in case of streams (like a get from ftp - although that also knows the size) you just immediately push to disk after each read(). But if you have preconceived notion on how much you are going to receive than its really annoying and you will need to realloc then - it buffer to disk and read afterwards 😝
@waytospergtherebro
@waytospergtherebro Жыл бұрын
Maybe once you learn English you can put all that nonsense into a coherent sentence.
@aniketsaurav1535
@aniketsaurav1535 3 ай бұрын
why would anyone need some library in network programing. I don't like it.
@billclinton4913
@billclinton4913 Жыл бұрын
Bro is asking to get hacked.
@klamberext
@klamberext Жыл бұрын
Localhorse..
@funprog
@funprog Жыл бұрын
Nice video but It reminds me why I don't like programming in C for networking. Header files, **, * &, free etc
@bdnugget
@bdnugget Жыл бұрын
Does this shit work on Zig too?
@eugenenovikov671
@eugenenovikov671 5 ай бұрын
without sockets, but using socket lib. clickbating
@canfloph
@canfloph 9 ай бұрын
onion.
@ThePowerRanger
@ThePowerRanger Жыл бұрын
Tenth view.
@FalcoGer
@FalcoGer Жыл бұрын
and that's why I don't like c. global namespace everything.
@fluentsloffy9420
@fluentsloffy9420 Жыл бұрын
you need to shift your video back a little the slight audio desync is fucking killing me
@dissonance9922
@dissonance9922 Жыл бұрын
Dude doesn't know what Ø is
@lodgin
@lodgin Жыл бұрын
> networking in c but without all those pesky sockets > so we're going to be using zsock
@LowLevelLearning
@LowLevelLearning Жыл бұрын
it do be like that
@achronath
@achronath Жыл бұрын
I sure love binding to tcp://localhorse:5555
@ic3man5
@ic3man5 Жыл бұрын
You aren't checking that zsock_new isn't returning NULL.
coding in C but I start over for every compiler error
16:34
Low Level Learning
Рет қаралды 264 М.
Networking in C++
32:50
The Cherno
Рет қаралды 237 М.
تجربة أغرب توصيلة شحن ضد القطع تماما
00:56
صدام العزي
Рет қаралды 57 МЛН
LOVE LETTER - POPPY PLAYTIME CHAPTER 3 | GH'S ANIMATION
00:15
Хотите поиграть в такую?😄
00:16
МЯТНАЯ ФАНТА
Рет қаралды 3,7 МЛН
The Linux socket API explained
15:21
Chris Kanich
Рет қаралды 33 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 823 М.
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 391 М.
Explaining Pointers Until I Go Insane
6:42
Mults
Рет қаралды 234 М.
🌐 Network Programming in Rust - Building a TCP Server
21:55
everything is open source if you can reverse engineer (try it RIGHT NOW!)
13:56
Low Level Learning
Рет қаралды 1,3 МЛН
How one thread listens to many sockets with select in C.
12:01
Jacob Sorber
Рет қаралды 96 М.
Making Minimalist Web Server in C on Linux
10:23
Nir Lichtman
Рет қаралды 235 М.
why do hackers love strings?
5:42
Low Level Learning
Рет қаралды 401 М.
Scientific Concepts You're Taught in School Which are Actually Wrong
14:36
تجربة أغرب توصيلة شحن ضد القطع تماما
00:56
صدام العزي
Рет қаралды 57 МЛН