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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you're learning to code one of the first Advanced projects you'll probably do is networking you may actually have an assignment right now where you're asked to send data from one program to another over the network and for a lot of new C programmers the Linux socket API is a little intimidating very quickly after maybe recently only Having learned to use pointers for example you'll now be asked to use the socket API making calls to functions like socket connect bind listen all while making sure the input is clean and you're checking for errors all the way this is not easy to do for a lot of programmers and there's an easier way to do it in programming power comes from abstractions the idea of building layers on top of harder Concepts so that you don't have to spend all of your time worrying what's going on under the hood today we'll be talking about zero mq or zmq a library in C that allows you to do Linux based networking applications without having to actually use the Linux socket API zmq was portable to a different variety of languages to include C C plus plus C sharp and rust and allows you to do a variety of messaging patterns like Pub sub and reply and receive that makes your code simpler and easy to write at scale so let's get into it okay so our example task today is to create a networking server and client that exchange the string low level and gang the Assumption here is over the network over TCP we could do this in the normal Linux C socket API but it would take us a fairly long time and have to do a lot of talking about the socket API how the different calls work what we're going to do instead is use lib 0mq to do this with a lot less calls it looks a lot simpler to a new C programmer the first thing we have to do is actually install libzero mq there are different sets of bindings you can actually get that represent 0mq the easiest one to work with in my opinion on Ubuntu is going to be the czmq bindings we'll do sudo apt install lib czmq czmq Dev okay I already have that installed so I'm gonna leave that alone let that install and then at that point you'll have the library installed on your computer that you can use later on in your code we have the task here in our task.md markdown file we're going to start with the server and I have kind of the scaffolding for the project ready to go to include the library's header files we have to include czmq.h and that'll give us access to the functions that 0mq uses to give us access to their socket API if you ever get lost or confused you can actually make a new window and do man uh Z sock and this will break out the one I don't like that it's only one but the one man page for the socket API uh for zmq so that's pretty cool so our task is to make a server and a client so what that turns into in zmq speakers actually going to be a a request and response schema so what we're going to do is we're going to make a z sock T pointer called the responder and that's going to be a z socket that is of type zmq to responder okay so what this does this actually creates our socket underneath the hood and now we have a pointer to that socket structure and then just like in any other server scenario we'd have to bind that server to a port so we have to do Z sock bind we're going to use the responder and we're going to bind it to this port here and this is where it gets really cool right instead of doing all the crazy like setting up your structures and making them the right endess we can just type out a string and say Hey I want to bind to local horse localhost Port 5555 so that's pretty awesome and then we're actually going to get the error code back from this and what this function actually does is it Returns the port number that we bound to so we could say if R is not equal to 5555 we will say print uh failed to bind to Port easy and then from there now that we have created and bound our socket we're going to go into an infinite Loop and receive things and send things back remember our task was to receive the string low level and respond with gang so we're going to start that right now so we're gonna do while true it's gonna go on forever we're gonna use the Z stir receive function and again this is going to receive from the responder some string pointer so we're going to say care message equal to zester receive and we're going to say if stir compare the message to low level so when it says not strict compare that means they're the same we're going to send back zester send responder and we're going to respond with gang boom and then we need to also regardless of what happens here we need to free the message and that's it so what we can do now to compile that is do our standard GCC stuff so GCC Tac o server server.c and we have to link in uh czmq and there we go booms the server is set up and if we do this we will say that we failed to bind to the port so what's happening there I wonder why that happened hold on let's find out oh it doesn't like localhost let's change that to Star there we go so don't do localhost to do the star there okay so now that we fix that we have this thing listening and in theory we can connect to it but the problem at this point is that our server is not speaking standard raw TCP our server is speaking the messaging is a custom zmq implementation over TCP to kind of handle the namespaces of the zmq messaging uh setup so instead of just connecting to it with netcat I have to make a second program that we're going to do in client that sends the server low level so let's do that the same thing as before we have our scaffolding here czmq in the main function and just as before we're going to do a z sock T it's going to be the requester it's going to be a pointer type but the pointer here and we're going to say that it's equal to a z sock new and it's going to be of type zmq requester not reply remember the first one was a replier or a responder this is a requester so that's again messaging scheme is request response so we are now in the client we are the requester and then cool so now all we have to do instead of any binding or anything like that we just have to connect to our server and send it a zsoc connect with the requester to this area here TCP one two seven zero or we'll do uh localhost five five there we go so this will connect to this area here over the zmq TCP protocol and what we can do is we can say zster send to the requester the string low level and then after that we will sleep for one to make sure that we wait for the server to respond to us so we could say care stirs equal to Z stir receive and it's going to be from the requester and then we're going to print that out and it's going to be the stir so hopefully if we run this and we connect to our server and get data back it'll be just the word gang so let's test it out real quick we have the server running here we're going to go into our other window and we're going to do GCC attack o client client.c Tac L czmq to link in the message queue Library we're going to run it and we got gang back so there is one issue here that we didn't actually free any of our sockets we may actually see there's an error here as well so actually the server is serving infinitely this one will not turn off but this one is going to have issues because every time we run it and it ends the socket is left dangling so we have to do there actually is do a z sock free on our requester so we're gonna do that and we're also going to put this on the server side at the end we're going to put it on the responder cool that's what it was it's Z sock destroy and again all these functions are available in the zsoc API I'll link that in the description below go check that out and also needs to be the address of the thing so not just the thing itself but the address of the pointer so at responder again we'll compile it gcp Techo client client.c and Link in the ccmq library and we'll run this and we get gang back and that is it lol again I hope you enjoyed that video I hope you learned a new kind of a fun way to do networking and C C does not have to be hard networking does not have to be hard I would say don't skip learning the networking API go learn that but also use libraries like this to abstract away the harder Concepts and make your code a little cleaner so if you'd like that hit like hit subscribe and go watch this video that I think you'll like just as much take care
Info
Channel: Low Level Learning
Views: 94,328
Rating: undefined out of 5
Keywords: c programming, c networking, networking in c, zeromq, zmq, zero mq, sockets, socket programming, c tutorial, raspberry pi, pico, rpi, microcontroller, arduino, maker, craft, hobby, electronics, wires, temperature, safety, project, board, electric, leds, led, thonny, python, micropython, os, ide, onewire, ds18b20, circuitpython, review, launch, measure, probe, rp2040, specs, specifications, how to, guide, programming, Pico emulation, retro games raspberry pi pico, etaprime, eta prime, raspberry pi pico, arm cortex m0+
Id: dpXKe-dw0uk
Channel Id: undefined
Length: 8min 52sec (532 seconds)
Published: Sat Oct 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.