TCP and UDP are communication protocols that allows us to send and receive Data in a network. We have both guys for a reason and good reasons and Because each one of these two protocols habitat advantages and disadvantages right.we will talk about these two protocol their pros their cones and We will write a TCP server and a UDC PDP server using node.js So at the end, you’ll learn actually how to build a TCP server I already UDP server so you can build that in and build your own app that communicate over TCP and UDP Based on really your application and which would what you are trying to do.
This is coming up if you’re new here Welcome. So the first thing we’re gonna do here is We’re going to go through. What are we gonna talk about right guys? So we’re gonna talk about the tcp pros and cons we’re gonna explain what tcp is gonna talk about the pros and cons and Then we’re gonna show you a tcp code example using node.js as the server We’re gonna build our own code and just told you guys more might be familiar with tolling which is like almost like a built-in Client in Windows and Mac and Linux.
So the first thing we’re gonna do here I really really really really highly recommend that you watch the video that I made on OSI model Okay, the opal standard model which which tells you these seven layers of networking, right and I’m gonna reference that video here. It will tell you exactly I would we showed with example You showed all this whatever you see here that we actually dissected then and be mystified all of that Hope you enjoyed check it out before usually that I really recommend it if you if you already know that you can just jump in that and UDP and TCP is actually a layer 4 transport Protocol, this is the transport protocol and they live right here So before I jump I really need to explain to you.
Just at least two pieces of information here the IP address and the port and Let’s go to the IP address each machine Each even if you think about it each VM virtual machine If you go all the way down each container can have their own IP address within a network and that IP address For simplicity’s just say a machine, right? Each machine has an IP address and that IP address is the address of that machine this is how you address that machine, but there are Thousands of applications running in that machine.
So that’s why we need and further discriminate or to tell us hey this application is that Application. How do you do that? This is using the port so port application and IP right so identify application you need two pieces of information the IP address and port and that’s if you’re familiar with connections and all that stuff in TCP You know that you have to specify the app the IP address way to connect to and where eventually you’re going which application eight is HTTP server 23 is SMTP 25 no 25 % TP 23 I think it’s telnet and whatever right so all of these SSH and now left a lot of stuff there are built-in Servers as well. All right TCP stands for transmission control protocol and this protocol was designed to allow us to basically send information from one server to another server by specifying the IP address and the port there is obviously a lot of Benefits to this thing and we’re gonna go through the pros and cons of this thing So let’s start with the pros of this protocol.
And what is it? so we know what it does right sends a message from one server to another server and it is used by a lot of Applications right HTTP uses the browser is under the need the the vehicle if you well Write emphasis on the edge the heck. All right off the HTTP is actually underneath is actually a TCP because there are reasons for that ya know and Databases uses TCP all the time, right? There are cases where UDP is used, but we’re gonna talk about that.
Okay, so What is the pros of this thing? What are the benefits? the first thing is acknowledgment, so you know guys the Internet right there are wires all over the under the oceans and and there there is obviously Unreliability involved with the internet when you send information so there is obviously if you send it using fiber optics or Electric signals you get these thing is nature right you gonna lose this information for any reason right a shark bites into it and the wire or or cosmic rays will send some magnetic field and just loses your your Your data right? So we cannot really rely on that. So we need some sort of acknowledge me that hey I sent you a message Please tell me if you received it.
So TCP has built in stuff for that and in order to implement that it adds additional Stuff to the message that is getting sent it has to because otherwise how does it know right? So let’s keep adding information on this and the message like a sequence to say Hey, I the server I sent your message Hello, and this tag it with one in the server responses. I received one and so on right so you can see the Acknowledgment if the server if the client didn’t receive the acknowledgment, it doesn’t trust That this takes us to the next point which is they can’t eat delivery.
So Because if if we don’t receive acknowledgement this transmission control protocol will says hey, I do not receive acknowledgement I’m gonna resent this thing again, so it has this retransmission thing where it sends the data again and again Even if if it doesn’t elicit and until it knows that I have received that Packet I have received this information. I I know that the server got it. Ok, so it has guaranteed delivery if the server Did not receive that coin or it was corrupted for a reason. Sometimes the bits do arrive the packet does arrive but the flips like one is zero and zero is one and your hello message is Essentially something else now. Okay. You don’t want it.
You don’t want to do that. That’s why you have a guaranteed delivery Okay, and you can start thinking oh my god. Do you see if it does try transmission? Isn’t that slow? Well it is but there is always Trade off cause you have to do in software engineering Connection based so take this with a grain of salt guys So I put this connection based and pros but might as well it could be a cons, but let’s just leave it there connection based TCP is a Connection based and that means that the client and the server the communication to communicate with each other They need to first establish a unique connection between them So that means they have says hey I want to connect server says cool. Can you connect to me? Alright your IP Is this your in this subnet? I think you’re cool. Let’s connect.
Let’s chat and then this there is back and forth and back and forth until a stateful connection and and let’s focus on the world stage for connection is Maintained on both ends, right? So the process now holds information about this connection the server also holds information about this connection It needs to because how does how also does it know about the guaranteed delivery? How does does about acknowledge? How does it know that hey, this is my client.
I need to serve it I need to make sure that I sent the information and this this goes back to one of the layers which is which is the session layer which makes Sure that all of this information is delivered in a timely manner so connection based congestion control So in turn let’s a lot of packets going on right guys especially when you have Fiber optics or Electric Ethernet data you sending data all over? the internet what happened is sometimes the the packets will get delayed because there are a lot of people sending stuff and the servers get Overwhelmed the browser is good overall.
So TCP has this Congestion control feature, which says you know, what? it’s like it’s like putting like a hose of water and It’s like flowing water and you’re filling a balloon or something If you don’t control that the balloon will pop up right because you’re filling it with a lot of water So you need some sort of a congestion control that another example is I don’t know if you drive on the 10 at the eye Or the 91 on the high window in the US and there’s this If you’re entering the highway from an exit from an interest right there, is this small traffic lights It’s like one car at a time, right? It’s like it Will start with red and then will turn green for a second and then turns back to red and then that’s this is called the congestion controls is This is just to control the traffic essentially and this is exactly the same thing We talked it from the from the LA traffic essentially, right? So there are traffic We we just stole the ID and put in TCP. So so here you go there and you love LA traffic, right? So you go there you wait.
There is if there is like You can eat one car at a time essentially, right? so TCP control this congestion by if there is a lot of cars it will stop will wait and It will send the data only when the network can handle it essentially. So it’s like almost like backflow kind of thing Ok, you can already say oh my god. So all of this stuff is going to be really slow because there’s all this waiting What is going on guys? I thought this thing is cool.
This thing sucks, right? Ordered packets, so same thing right guys with Contingent controls some packets were race to the server some packets will reach before that You might send a packet first, then you can even if you’re sending especially a long text Right, and then this sticks obviously it’s gonna get chopped up We talked about this in the OSI model video and then it’s get chopped up into smaller and smaller smaller videos for our packets and this packet will get just delivered right but Internet does not guarantee. You’re gonna receive them in order obviously, right? Cuz one package could go through China another package could go through. I don’t know Throw the Pacific right? So it doesn’t doesn’t really guarantee that this one package will receive there.
That’s what TCP will order this packet so you can start thinking that TCP start adding all these headers on top of that stuff, right? So in order to order the packets start to label them Hey, okay, come here. Your your packet number seven your packet number eight your packet number nine So start ordering them back on the server when it receives them. So there’s a lot of right guys, but it’s all good reasons We need this stuff. We need it but Do I always need this stuff comes back to the cons? Ticktick the pros. I’m just literally flip them and becomes cons right larger packets. You can see write Larger packets I have package for acknowledgment. I have I have headers for guarantee delivery for connection I have congestion control. So I have order packets of you. I think it’s 20 bytes But still there’s a lot of data that is a lot of data bed Overhead the CCP adds all of stuff on a simple message Even if you send one byte piece if you has to add all that garbage Just to ensure these features These features let’s turn this back.
There’s features, right? So a larger pack larger packets means more bandwidth So if you’re on a 3G or 2g network tough lock using TCP, right? So it will be really really hard with with more bandwidth because your packets are larger now Okay, obviously, this is now less Relevant, if you have like 10 gigabits or 1 gigabit bandwidth still obviously use TCP if you want to slower than UDP, obviously we talked about Ordered packets, we talked about congestion control all of this could involve waiting all the stuff has waiting guys We’re waiting all the time We’re waiting for acknowledgment. We’re waiting for content congestion control. We’re waiting for our packets to get order Wow There’s a lot of work We’re waiting for guaranteed every because I want to make sure that our services aren’t always waiting waiting waiting waiting Ok, so obviously it’s gonna be slower than UDP which we didn’t discuss yet. Yeah Stateful.
All right, so We made a video of a very popular video the difference between stateful and stateless I’m gonna reference it here go check it out, but in a nutshell If you wanna understand the different between stateful a stateless in a connection based system architecture Here’s how you get here’s how you understand.
The state’s focus is stateless if you can destroy your server and Restart it or just restart the server shut it down under show and restart it back and if you can do that if you can restore your server while the client is connected and Have the client just resume their work normally without any workarounds Then your stateless Okay, alright, let’s repeat that if you can restart your server and The client is just connected and just can do their work normally without again and interrupted then your Your system essentially is stateless or your server is stateless and the reason is because you have you don’t store and form a About the state that the client is currently in is the client connected is the client acknowledged is the client center of religion did There is there’s no knowledge of this but TCP is actually the opposite of that is stateful It’s fully of stateful because the server car carries information about the connection The client carries information about the connection you destroy the server All the client for that matter or you just shut it down.
That’s it The connection is closed that if the client tries to just send information immediately It will fail it would say hey server is out of reach or wherever right? Obviously, you’re gonna say, okay I’m gonna canary connect you can do that But you lost the connection if the client is in the middle of sending information and just look there the data is done a dead That means you you essentially lost that Okay, so I was like some of you want to ask IR this videos long guys But I like I like to make videos to explain everything This is like yeah, you can always skip and go for five minutes videos which which doesn’t give you any information but but if you come here to this channel, I’ve tried to explain almost I try at least That’s what my videos are essentially longer but yeah stateful Essentially TCP is tasteful because when you close that connection that set your connections that you cannot just resume that Easily, right? So you lose that plus there’s like this is the second point is the server memory Okay, even the client memory for them, but the server is more important. What does that mean? So another cons here is because of the statefulness because of the connection base.
That’s why I didn’t know where to put this connection base thingie guys right because of these two the server has to stack up on these TCP connections have to allocate memory for Each connection obviously, where does it put it? Otherwise, there’s a button on disk. No can’t put it in CPU You have to be put in a memory, right? And Then obviously they have to Lou Server has to listen on these thing and keep checking and hey did I receive stuff on it is I you receive a stuff on This sport did I receive stuff on world for you? I recieve stuff from this connection so there are that’s why there is a limit of a number of connection you can you can make TCP connection you can make to a server because and that brings us to this point denial of service attack the our service attack happens because Some will tries to essentially establish connections to the server using TCP connections But it makes the server remember guys the waiting that where the server does the guaranteed delivery They can the congestion control the order package all this stuff it It uses these features against the server says, ok. I’m gonna send you a packet.
Guess what? but I am NOT gonna reply I Am not gonna tell you if I if I if I I’m gonna acknowledge this message I’m not gonna guarantee any delivery there is essentially a bad client that does up on it’s a very low-level Client that does that it sends a connection request? But it just just literally just turns around and create another connection.
And so the server keeps waiting for a second there’s a timeout obviously after which the Server gives up But do this million times your server will stack up on memory Because of all this useless connections and you’re gonna essentially die your server was just dead Yahoo! Ahead god had this in the early 2000s. Obviously web servers are much more small small right now long Descriptor of tcp versus UDP, but now let’s get into demo. Let’s show you some code guys Alright guys, so this code is a TCP service And sure this is no js’ I’m using Visual Studio code.
Don’t worry I’m gonna give you the code in the description below so you don’t have to Literally write it by this and sure let’s go throw it blind by line. This requires a package on Node.js called NIT and we use NIT to create a server obviously, we gonna create a server and then the server will gives us a socket and this is our connection essentially so that socket once we connect no ones we’re gonna once we Create our server and start start listening to that. What we’re gonna do is the moment a client connects to us We’re gonna send it back. We’re gonna write back to client the word.
Hello and we’re gonna do this once this is just when the client connects, okay, but then We’re gonna do this on data. That means if the server the client sends there’s more data We’re gonna call this function and that function will receive the data and well, what does it do? It just prints it on here. Literally, we’re gonna put it in the debug console. Does that make sense? And obviously we’re listening on a port called 8080 because we remember IP address and port our IP address is one two seven zero zero one. This is the loopback I’m gonna use that it because we’re on the same machine, but you get the idea.
The port is 80 80, that’s more application So I have a TCP server. Let’s go ahead and run it and after we’re gonna run it we’re gonna go to the Terminal and then connect to my server. Let’s do that. How about that guys debug start debugging And then leave the string running and then you can see we are now running, ok, and you can see because the See the debugger is not stopping because it’s waiting for connection and that’s my point by the memory thing Right because it’s like we’re waiting for waiting. We have a server that or constantly waiting for a connection.
Ok, and that Now that we have a server on port 8080. Let’s go to the connection and use telnet which is a built-in I think if you don’t have talent just use brew and install a few in case of Mac and In case of Windows you can I think and install from the windows feature Telnet is right out there. And then I’m gonna connect to one two, seven zero zero one, okay Which is my local machine or you can use a same Mac Which is hostname there my host name and then port is 8080 and look at what it all happened This will say trying one two seven connected to local O’s and emphasis on the word connected I am Connection. This is a connection has been established. So whatever we talked about that guys acknowledgement and all that stuff and hey, can I connect to you and all that jazz has happened now and Look at that guys. Hello. We received the word.
Hello from the server Okay And I have put a breakpoint guys Here that means essentially when when I send information we get a trip and stop the code here Okay, just to show you so I’m gonna send something. Let’s send the word Hi Enter enter will actually trigger it look at that. We got a breakpoint. We got the word date We got data and essentially data is only sending on bytes. This is the buffer so we have four bytes Why do we have four bytes I sent to war two letters which is to white, right? So the word H the word I wrote the letter H the letter the letter I and then 13 and 10 is the carriage return which is the inter.
It’s two bytes for some reason. I don’t remember okay, but what I got essentially does a hit do that and then Look at that Hi Do it that fits like a horror movie right now It’s just missing the blinking cursor. Oh my god, I’m freaking out. Okay imagine you receive a high Out of nowhere. Hi. That is so creepy. Jeez. Okay. Alright, so you get the idea, right? Hi, let’s do so We get sup, okay, I’m not as a frightened. What’s up the school this guy’s cool. Alright, and then you can send stuff right? My name is Usain All right You can see right. We got the idea now Let’s let’s demonstrate what will happen if I just kill the server okay, if I kill the server? Okay, let go back to the client What? Connection closed by foreign horse coerced by foreign host These exotic foreign hosts, man.
They keep closing these connections, man Connection closed by foreign host. Okay, so now okay that listen some more information what I Just lost my stuff. I was in the middle of a connection. I just lost that’s the statefulness that we talked about guys. Okay Alright, so that’s that the example of the TCP. It’s just jammed jump back into boring slides guys.
All right guys UDP so for the UDP, which stands for user Datagram protocol, I don’t really know what it what users Really has to do with it here because TCP is also designed for users so tell me in the comments section below for you now because users The user thing is really really weird for me as An acronym here, so but essentially what let’s talk about the pros and the cons about this I’m gonna start with essentially negative thing here where I’m gonna talk about the cons of this which is exactly Opposite of the TCP are literally copied and pasted and flipped it. So I start with the cons guys So the first thing is it has no acknowledgment.
You send the data you know, the server does not tell you I received it or not and The vice-versa obviously, okay, so there’s no acknowledgement send the data Don’t care. That’s it. Okay, so just just like that. It’s just like things are a little faster, but there’s Chances where you might lose your data, obviously There’s no guarantee delivery whatsoever guys you sent the data. There is no Retransmission there is nothing you sent the data that said Reach to reach doesn’t doesn’t okay there is some information that they you the udb packet adds on top of the message and then and that information is essentially the CRC or the checksum, essentially it does a quick check Some says I can only tell you hey server I can only tell you that this packet is bad or good and that’s it you want me to drop it or do you want me to deliver it and tell you that it’s bad and Even that you can’t really guarantee that it’s really 100% good.
Okay, so no guarantee delivery connectionless So because it is connectionless again this take this with a grain of salt. I don’t know if this is like Pros or cons, but there is no physical connection So you don’t know you’re a client and the client doesn’t know you’re the server essentially There’s no physical connection that tells you that hey I am connected to this thing There is no trust there is no security there’s nothing. Okay and No congestion control. Oh The UDP will if it receives packets if it’s asked to be sent back packets or data, it will keep sending in Traffic or no traffic. It doesn’t care.
So No congestion control essentially when your DP receives that information it just It just sends it. It just sends the data. It doesn’t care if there is traffic in the internet or not It doesn’t wait to the crap for the traffic to to clear or anything It just drives and drives and drives. Okay, so just Literally just drives and then you will get into this is a huge congestion as a result of congestion Right because there is no control at all. Right just send it know ordered packet Obviously it’s not going to order anything It’s if it if it’s asked to send these five packets, it’s gonna send them and it’s gonna the server gonna receive them But they will not be obviously ordered It’s the UDP will not take the head to order these five packet It’s not it’s not going to wait for all the packets to arrive in order to shuffle them. It’s not gonna had headers to Sequence them and number them.
There’s nothing to do any of them Okay, and finally is actually the bad thing is security and because there is no connection Right. Let’s turn this back. There’s no connection The server doesn’t really know who was this guy. It’s just Anyone can just jump in and if the port is open and then send you stuff that Is really bad, right? Because that’s why a lot of fire was just disables UDP altogether because of this reason right because there’s no connection I how can I trust I don’t know who you are But in the TCP there is a connection it does that’s at least okay. Tell me who you are. Where are you from which IP? Ok, you cool, you can can but UDP. There is no connection.
You don’t establish connection You don’t tell me hey, I want to connect you. Just send me data okay, so a lot of people disable UDP for this reason because because you can literally no connection You know the port and IP you just flood it with one gigs of data just flood Okay, obviously, you can implement some sort of a protection and the server look pros guys Well is the pros the pros is? smaller packets Because there is no acknowledgement. There is no guarantee delivery. There is no connection. There is no congestion There is no order. It doesn’t have to add all these headers that the TCP does to maintain all these features, right? That’s why you have smaller packets with the UDP And Smaller packets will lead to less bandwidth.
There is no data. No There’s no much data to to sender on so you can use it in low bandwidth Areas essentially especially games especially over a 3G or 2g or very weak network Intermittent network some some game system use UDP for this and they implement essentially what it’s called reliable UDP Right and they implement some of these stuff features on the higher level application that they are building on top of UDP, so Yeah, centrally they set a message and then they check it up higher up. Ok so a lot of games use that actually really in China where where some some Philippines, I think it’s the Bandwidth is not that great faster than TCP Obviously, there’s no waiting for congestion control. There is no waiting for order packets I know I’m not gonna wait to order my packets.
I’m just gonna send it right so faster. I’m not gonna wait for acknowledgment Stateless that’s actually a good thing, right? And we talking about stateful and stateless you know in a nutshell by the same thing if you have a server that’s running UDP and you have a client sending information if Your server eyes Right and it came back up. Alright this sir the client can just continue sending information It doesn’t because it doesn’t not need to connect this until you. Hey, I am blah. I am this you can trust me There’s a there’s no all this Nonsense that has to go in order to establish a connection. Obviously guys, right? That’s why I just Write stateless how about guys will show you some example and we end this video. Let’s jump into some code All right guys, so what you’re seeing here is no J’s app that Creates a socket creates a server and listens on port 8081. It’s a UDP Protocol here.
Okay, and What we’re gonna do a central user go through line by line, very simple. We’re gonna require that package that’s called D grab and then when we when we get that we’re gonna create a socket on UDP 4, which is Ipv4 there’s UDP for and UDP 6 there for those two IP addresses Systems so you can create a socket and then you create an event. Hey when I receive a message Call this function, please and just literally what this does like server got this message from this thing Okay, that’s exactly what it does Very simple stuff and obviously it binds the port 88 you want to this socket? So it keeps listening okay, so similar thing Nothing different We still listening right but there there’s no concept of connection and we’re gonna show that so we’re listening in port 8081 I just immediately I’m not gonna connect. I’m just Sending data.
How about do we do that guys and here’s go back to our terminal What we’re gonna do is we’re gonna print echo gets so high Right and then here’s what here’s how you essentially do it. Okay, so echo high we’re gonna you know this pipe essentially what it does is takes this data and send it to whatever the the right hand side is what we’re going to send the heart to is ncu stands for knit cat and Dash W. This is option if you want to but this is really establish a timeout, right? I don’t know wise choose time out There is one W timeout they chose W instead of T for some reason, okay? Timeout of one second after one second give up.
That’s what it means Okay, but you can always you can remove that all together if you want to and send it to this server Obviously localhost that’s that’s the one two seven zero zero one same machine and then the port 8081 inte R Just like that. We got it guys. We got the data. Let’s capture. Let’s see what we got. Let’s see what we got We got a message three bytes No, actually more than that So you can you can notice that that the UDP Sent makes three bytes the word TCP has four Okay The carriage return only has the 10 which is the inter versus the TCP has Both 10 and 13 two bytes for the inter.
So just like that is Essentially an extra byte. All right so this is the high and What we’re gonna do is just like let’s make it two string and print it and you can see that Hey, I got high and I got it from one two and seven zero zero one and this is the internal IP address of the client, right we talked about The internal port and the external board we talked about that and the client port essential in the server port We talked about that in the OSI model check that video out guys But essentially guys what I want to explain to you is I can turn on and send another request here say For example. Hello okay, and then I get it I can’t kill the server and then literally restarted and The client can still communicate with me That’s the stateless part of this aspect, right? It is always the client Doesn’t keep information about the server and the server doesn’t keep them for you just receive stuff it doesn’t save anything and memory per se it’s just like just really take it and then Does it thing and then discard it? Okay. Alright guys That is all that is literally Oh, what have we learned? All right, guys, what have we learned? We learned about the TCP and UDP learn about the pros and cons We’re about will show you an example on the TCP will show you an example on UDP We learn about that pros and cons of UDP, but in general or if you want to build an application now You have all the tools, you know the difference between TCP and UDP right guys.
So because of that you can build your application using any of those if you are building a game for instance and that game is designed for let’s say Countries where it is very low bandwidth you can you can use the UDP and build some sort of a Check ups at the higher at your application to to essentially protect against data loss and all that stuff, but You get you can get use of the UDP less bandwidth feature, right because it doesn’t have as much information, right? versus if you’re building like a secure chatting app UDP will not work. Well for Europe because your dare that we’re gonna receive skumps crumbled and plus you’re sending and small information Anyway, so you can use TCP for that. It’s it’s it’s really Negligible the amount of headers that gets added specially for texting app.
A lot of people just use Just use TCP for that. Okay, and Video multimedia streaming. A lot of people uses UDP, especially because and and you you can even just tolerate Loss of data you can tolerate bad Packets receiving in videos because yeah, what if a frame became just crumbled So what who cares right if a frame were to comes this combo I can see it still to see the video, right? Yeah, as long as cuz you’re sending a lot of data a lot of data was TCP me a lot of waiting a lot of Acknowledgement a lot of congestion control out of ordering if you really don’t care about these four features Just use the ODB if you really care about this order Delivery like you can never use UDP with I say I database okay, it’s just you’re sending small packets of information and it’s like it the sequel statement essentially and do you better get that correct result you get better get it in order there and you get a better get a small results you You can usually get back small results and then that results you get at render and you do stuff with stuff with the database Right.
So TCP is almost flavor right? I’m not saying that you cannot use UDP of database you can but you’re gonna do a lot of heavy lifting on the server that application level and What else UDP is used by the way for DNS? The dynamic name server its uses UDP because dozen texts a bliss it needs to be stateless Sometimes the statelessness is very important and you really need it Sometimes that’s why it’s like the scaly based believer scalability. All right You Ruby’s really scalable for still. I wish this is information we didn’t we didn’t talk about but you do be scalable because you can literally have 70 containers running you DPS All right you DB servers and then just round-robin through them and you can have applications Serve these UDP because there’s no connections right people clients can just connect serve the request and then disconnect you don’t there is there is no Connection per se let’s let’s remove that word connect there, but you can literally send information to the server without any connection per se and that is powerful for horizontal scaling Okay, so you can just duplicate the server and run a UDP server and just test like that that’s what for gaming is powerful Right.
It’s like a scaling just literally have another server and just that’s a push done. You get the client just Connects to the other server and resumes its work, right because there’s no state stored in these servers Alright guys.