 Welcome back everyone. This is Brian. We're going to be talking about the other end of that communication channel the TCP server And I'm just gonna paste a whole bunch of information in here. It's the exact same thing from the previous video just in case you skipped it We're gonna be using TCP. We are going to be the server meaning we're gonna open up and listen for clients to connect to us Doing so creates a network. We're doing this on IPv4 and we're going to use a port and we really don't have a protocol But I want you to understand by sending information back and forth. We are essentially creating our own protocol All right, let's dive in and take a look at what we're really doing here. So Previously we were the client where we connected out to a website and the website sent us something back in this case We are the server So something else is going to connect to us We are going to take that information and do something with it I know I've said it before but the first step is always to well get our includes hammered out. So we're going to import logging Socket and let's go ahead and let's configure our logging real quick here I know I've covered this before time and time again I just don't like skipping steps because then people go now wait. When did that get configured? So we've covered this To the point of being ridiculous, but you should know how logging works at this point But we're going to be using sockets. So when we use this module understand what we're doing We're working with IP protocols Let's go ahead and define our server. So we're going to do this in a function. I'm gonna say dev And this is going to be server And this is going to be what's called a blocking server Which means it's going to block this thread while it's talking This is not what you would consider high performance or industry standard We're just doing this so you can get the basic fundamentals down. We'll make a non blocking server later on in the series So we want an ip that we're going to use and a port we're going to talk on Now think of an ip like a phone number You can have multiple phones with multiple phone numbers and things of that nature So we just want our identifying number on the network and the port we're going to use to talk on From here, I'm going to say s equals socket And I want to say socket I always like saying that socket socket. Anyways socket to me. Anyways, let's go ahead and say socket dot And we need an address family of iNet because we're using ipv4 We've talked about it before ipv4 ipv version six accomplished basically the same thing They just identify your machine on a network And then we want to say socket dot sock stream Sock stream that has like a weird visual when you think about a sock stream like a stream of socks Kind of like my laundry basket, but Really it's kind of the same concept. You have a stream of data that's coming across that socket Now we're going to say address Equals and we want a tuple here That tuple is going to be our ip and our port combination And I'm going to say logging info And this is strictly for us just so we know what's going on in the server Now server's a little bit different. We're not going to connect to something What we need to do is bind Now usually when you're in a bind, it's not a good thing, but here it is. We're going to say s dot bind So we're taking that socket and binding it to the address which is a combination of the ip and the port This could throw an error if we try to bind something that either the operating system doesn't want us to do because we don't have permissions Or it's in use by something else It's going to come back with an error But basically what we're doing is we're telling the operating system. We are now using this and no one else can From there, I'm gonna say logging dot info And let's go ahead and flip these around. I wanted to Just kind of in real time making a decision here and how I want to handle this Just so we can see the ip and port we're binding to then we're going to flip around and say, you know what? We are now going to listen So once we bound to that address, which is the ip port combination We're going to go ahead and say socket dot listen And this is a lot like sitting next to the phone and listening and we can tell it how many things we want to listen for We're only going to listen for one in this example So only one thing is really expected to talk to us From here, we're going to go ahead and say we want a connection And the address of s dot Except so what's happening here is Our socket somebody has dialed our phone and we are going oh, I will pick it up I want to know who that person is based off their address And I want the connection object or the underlying socket used to talk back and forth Pretty simple the way that works, but it may look a little confusing on the screen I want to say logging dot info I should probably Tell it that we're formatting it. This is what happens when I change my mind halfway through a video All right, there we go. So we're going to say connected And we want to know the address of that remote connection From there, we've got a huge amount of decisions we need to make here But I'm going to say I want to do this in a while loop. So I'm going to say while true And we're going to say data and we want the connection I'm going to go ahead and receive some data here. So what we're going to do is we're going to block our socket and Basically say we're not going to do anything else Until the socket gives us data. This is called a blocking socket So the whole application is going to come to a screeching halt when it hits this line and it's going to wait For some amount of data all the way up to 1024 bytes Then I'm going to say if Len And there's multiple ways of this you could say if not data I like doing this if not len data equal zero So if the length of data is equal zero, then we're going to go ahead and get out of here We're going to go ahead and close that connection and we're going to go ahead and break out of here Now at this point pretty much the connections are already shot anyways because basically what we're saying is If the client didn't send us anything, then go ahead and shut everything down We're just trying to very gracefully do it on our end even though the client may have just logged off or died We just don't know So let's go ahead here and we're going to say logging dot info And we want to know what they sent us Once we've broken out of this loop, then it's just very simple. We're going to say logging info And we need to take that server socket and let's close it So we're going to stop listening This is a lot like just hanging up the phone and walking away because we've listened We now need to close or hang up that phone But we have to also close that connection. So understand what's going on here is Technically we have two sockets. We have our server socket, which we've bound to and we're listening on And then as somebody calls the operating system It's handing us another socket that we can work with and receive data from and when we're done We need to try to close that out gracefully All right, the moment you've been waiting for let's go ahead and do our main function here We're going to actually work with that server socket. So I'm going to say Main main and let's go ahead and make our main function And we're going to say server And we have an ip and a port so we're going to use our local host Which is just a representation of our local system And I need to pick a port that is not in use and later on in the series I'm going to show you how to find a port that's not in use, but right now I'm just going to guess So I'm going to say 2607 I have no idea if that port's even in use this may have disastrous consequences. So If we choose a port that is what's considered a will known port which is less than 1024 We may say access denied and our program dies and that's because those are considered special ports That's why I'm going to go with what did I say 2607 just because it's above that number All right, let's go ahead and run this and you see how we have bound to local host on 2607 And we are listening that means we successfully went to the operating system and says Pretty please open this port up and let me own it and it said sure there you go And now we're listening for information So I'm going to move this up here And we are going to open a command line I want to do this outside of python that way you can see just how squirrely things can get So I'm going to go with what's called telnet Which is a program and I'm going to say 127.00.1, which is the same thing as local host And let's go ahead and connect on 2607 You can see how we now have a connection and I'm going to try and move this over here So connected 127.001 and then we get this port number. So this is the alternating endpoints So our server is listening on 2607 the client is connected on 60448 A little confusing how that works, but think of them as two different objects because they are Then I'm going to say hello And you can see we have some data in hello. Let me move this down here Now I should note depending on your telnet program that you use if you're testing this out yourself You may actually see each individual keystroke as you do this It may get a little confusing as you're going to see he lll going down the screen So basically in my representation The data is not sent until I hit the inner key. So I'm going to say Hello world and as soon as I hit enter It sends it and it adds a carriage return line feed to it Now I'm going to actually close this window And this is where we'll get unpredictable results because in our protocol The language these two are talking back and forth on we haven't defined when it should shut down So I'm going to just close the window And it's gracefully exiting and closing the server Now on some systems you may get a big wad of weird looking Bites and that is basically the end program Not shutting down gracefully and the client doesn't know what to do or there was Still data in the buffer or some other crazy thing happened So in the end really what we're doing here is we're saying Bind to an address and a port locally Listen and then wait for a connection once we have a connection Go ahead and receive that information until we can't receive it anymore because they stop talking to us Then we close that server down I hope you enjoyed this video You can find the source code out on github.com If you need additional help myself and thousands of other developers are hanging out in the void rom's facebook group This is a large group with lots of developers and we talk about everything technology related Not just the technology that you just watched and if you want official training I do develop courses out on udemy.com. This is official classroom style training If you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button The more popular these videos become the more I'll create and publish out on youtube. Thank you for watching