Ep.14: Server Command Line Arguments - Network Chat Programming
The Cherno
·
Intermediate
·12y ago
Key Takeaways
This video demonstrates how to use command line arguments in Java to set up and run a server, covering topics such as server-client architecture, command line interface, and network programming using tools like Java, JAR files, and Eclipse.
Full Transcript
Hey, what's up guys? My name is Aerno and welcome to episode 14 of network chat programming. So today we're going to talk all about servers and how to actually make a server, how a server basically works and how we can make an interface for our server as well. Now um unlike you know unlike this client that we have right now if we launch this right now uh what you'll see is um is this login screen of course and if we do enter a bunch of uh you know um details in here like the IP address a name and a host uh and a port sorry and we log in we get this wonderful thing and whatever we say appears here oops here okay and all that stuff works that's this is our user interface now it's not really that common to make a user interface for a server. And the reason there's a few reasons for that. First of all, it's just unnecessary. You know, the server is not really going to be doing too much. And out of the stuff that it will be doing, it's just not very necessary to have an interface for that. So, our server is going to run in the command line. Okay. And that is uh that's a lot of fun. So, let's get right into that. So, in Cherno, in this little folder that we've got here, I'm just going to rightclick uh hit new and hit class. Now I'm actually going to put this into a separate package called server. Okay. And I'm going to do things a little a little bit differently because this is kind of uh it's sort of like a mistake that I made. Uh this client Java class actually contains both the client code to make the client uh on the on the network receive and send data and also the user interface code. Now that's usually I'm not sure why I did that. Usually what you would do is you would have all the code for actually physically sending and receiving data doing stuff that a client does. So like the logic behind the client as a separate class and then you'd have like a top layer over that an interface layer like a user interface layer and that would be a separate class called something like client window or client GUI. Um so uh instead of so so what I'll do here is I will create a class called server but but I'll also create a new class here. So again, right click, new class, and I'll call this something like uh server main or something. Okay. Uh I'd call it ser server GUI or server or server window, but of course it's going to be command line. So server main will do nicely. Okay. So we've got two classes now. Server and server main. So what are the differences? Server main is going to be pretty simple. Okay. All that's going to do is essentially launch a new instance of the server and you know allow it to run. So this will actually have the main method. That's one of the reasons I called it main. Okay, so inside our server class, this is going to be of course the class that actually, you know, sets up and and runs the code to send and receive data here. So I'm going to make a new private integer here called port. And then I'm going to make a constructor. And the constructor, which is a server, of course, the only parameter that we'll take is port because of course we don't need an address. We are the address and we don't need a name because well, we're a server, not a client. But of course, we do need a port number because a server can run on any port pretty much. and we need to specify that port. And we'll just set this top port equal to port. Of course, setting this variable equal to the parameter. Okay, so that's that's it. That's all the code that's going to go into our server class. Now, server main on the other hand, that of course is the class that actually runs a server. Okay, it's the the top layer kind of thing. And one of the reasons it's cool to create a class like this is because if if we put all of our um server related logical code into this server class, what that means is that we can actually run multiple servers from this one server main class. Okay? Whereas if we were to do something like just have one class, that wouldn't be possible, right? We'd have to actually run multiple programs of servers. So in other words, we'd have five applica five server applications running, but with this we can have one application running with five servers. Really cool stuff. Um when that's not going to really be useful to us, but it is of course worthy to note. So we'll start off with the main method. Okay, the server is its own application of course. So public static void main string args. Now I I get a pretty common question actually. What is this? What is this array of strings here that we put into the parameter? And it's your lucky day because we're actually going to be utilizing that. Now, to understand how this works, I'm going to probably wish I was on Windows cuz it's a lot easier to explain, a lot easier, but more people would understand it. But we have this little thing called a terminal. Okay? And a terminal on Windows is a command prompt. Okay? That is the closest you can get to a terminal. It's still not really a terminal, but it's uh it's close. So what this does is it it it's basically, you know, the opposite of a GUI. So we have this GUI that allows us to run programs, create folders, remove folders, rename folders, copy files, you know, run applications is the big one usually. Um, and we also have this um we also have this this terminal, right? And back in the day, back in like Windows, you know, DOSS and stuff like that, um, or MS DOSS, I guess it was called, Microsoft DOSs, Microsoft disc operating system. Um, this isn't going to be a computer history lesson, by the way. But the point is, back in the day, this is all you had. You didn't have GUIs. Okay, so our server is going to run in in in an environment like this. And one of the reasons that's incredibly useful is because you'll actually be able to run this on, you know, a um uh a VPS, basically a virtual private server. You'll be able to run this on a server that is um you know, maybe you you can like buy a server, which I will do by the way. I'll um I'll demonstrate this eventually, but basically what you can do is you can you know how there's web hosting? Well, there's also VPS hosting. And instead of actually buying like a web server, you can just or rather renting a web server, you can rent a uh an actual computer essentially with a command line like this. Usually running a um either Windows server or Linux. Now you'll you'll most likely if you connect via S SSH or something like that rather than a remote desktop environment, what you'll get is a command line and this makes it basically impossible to run GUI programs. However, of course, console-based programs will work. So if we were to run something we um in Java at least you know we can do some cool stuff here like Java version that'll tell us the version and uh you know other stuff if we just type in Java it'll give us the usages. So what we want to do is essentially when we run our application we want to be able to type in let me just clear this we want to be able to type in something like Java then of course the name of our uh server. So chrono chat server jar that that is actually how we will run our application. Okay, you type in Java and then the name of the JAR file that you want to run. Okay, now now that you've done that, we actually want to hit spacebar and say, okay, we want to start this server on port 8192. Just like that. Okay, after this. Now, how do we keep how how do we the cool thing about terminals is after you've typed the name of the application you want to run, you can give it some arguments. Okay, these are kind of like parameters that you can actually pass into the application. So for us, we want to do the port, right? Because the port we want to we want um essentially we want the user to be able to set the port when he's starting a server. If we hardcode it, you know what? If that port is not available or what if we want to start multiple churn chat servers here, we can't because if it's if we bind it here to be like, you know, 8192, then that's the only port that can be used. The code actually needs to get altered and recompiled to get a different port. Whereas over here, this gives us the flexibility to actually enter a port into um at runtime and it will it will function from that. So how do we do that? Well, that that's where this arcs variable comes in. Okay, as you might be imagining right now, um args is an array of strings. Okay, and that array of strings is equal to however many parameters we decide to put after this. So if we do something like this, the length of this args array will be four. It'll contain four strings. 8192 23 Hey, and two. If we put in just 8192, the length will be 1. And this is the only value it'll contain. So that is great. So let's actually how do we utilize it? Well, we'll glad you ask cuz it's really simple. Um, we can it's again, it's just an array of strings. So the first thing we want to tell the user is to say, well, if you don't, you know, if you don't supply an argument, that could that could be a problem. could okay we might not want to make it a problem but it could be a problem. Um so but of course if they specify more than one argument so if someone decides to go something like 8192 and then four for some weird reason that is wrong and we we want to be able to handle that. So usually the way that I like to roll is if if args.length is greater than one so if there's more than one in there then we really do need to system.out out dot print line you know the usage so actually this is good and it's in fact as Java jar not uh Java so this is the way you would run this that's if you run separate class files you can just use Java but um Java jar is required for Java files my bad um but yeah okay so that um so if it is greater than one so if we enter some something like this we'll get a usage here Java jar uh you know churn chat server jar I guess like I'm not sure what we'll call it. I'm not sure. You might not even need the jar file here, but we'll see. And then um I guess we'll go port cuz it's it's uh it's not required. Okay. And then we'll return. So we won't run the program. Um this probably won't make a heap of sense. But if I do set this something to if it doesn't equal one. So in other words, you need you need to have a port number here and I run this right. What you'll see straight away is Eclipse will tell us over here in the console the thing that we just printed. Okay. And the reason is in Eclipse we of course when we ran this we did not specify any command line arguments. This is going to be a pretty long tutorial actually. Command line arguments is a not the easiest thing to explain but it does it is um very very powerful and very useful to know. Anyway, we'll create a new uh constructor here quickly called um server main and that will take a uh probably one parameter which will be the port here. Um now the other thing we should do is create a port integer here. Okay. And what we'll do is we'll uh try to set port equal to integer dot pass int and then our string which is args zero. Okay. So now we've got port and when we actually create our server main we just want to launch server main with the port port. Okay. And what that will do is then set private int port that will then set this.port equal to port. Okay. So what we've done now is we've actually taken an input from the user and we've assigned it to a variable here which is very very cool. Now just to um h just I'm just thinking if I should actually make port mandatory or not. Probably not. We could have a uh we could Okay, I'm just going to just for the sake of this of this tutorial, I am going to leave port as a mandatory field. So, you have to use port. Okay, that's actually required. Um and the reason I'm doing that is just because um uh I'm doing that because uh it it'll help illustrate the command line arguments a bit better. But um what you what you could do is you could have a default value. So in other words, if arguments.length is equal to zero. So in other words, if nothing if no parameters are given, then set the port equal to default port such as 8192. Otherwise, you can customize it if you like, but that will work as well. Okay. So how do we actually run this in Eclipse? Then we've made a little program that we can't actually run in Eclipse because what we get here is a usage uh problem here, right? Usage Java jar, you know, churn chat server jar because uh arguments are length is indeed not one. Um how do we how do we do how how do we go around that? So what you can do is under debug because that's the button that we pressed to run this. There's this awesome little menu or this uh yeah I guess this this uh option here called debug configurations. If you click on that it will open this this window here. Now there is a tab called arguments and program arguments. And this is where we can specify any arguments that we want. So we can just type in 8192 just like that and apply that of course and hit close. And then um if we run this let's just to illustrate this let's just print out port. Okay so if we run this now what you'll get in the console is the port that we entered in the command line arguments. If we go back to this debug configuration and change the port number to 8888 for example. Apply that and close that. Eight is my favorite number by the way. Um, and if we run this then in the console we'll get that. Okay, so we're simulating passing in command line arguments here. So that is that is pretty much how command line arguments work and how we're going to use them to give the user more control over how the server is launched. Um, so I hope you did enjoy this episode of network chat programming. If you did, please hit the like button. Again, if this video reaches 200 likes within 24 hours pretty much, then I will release a video tomorrow. If it reaches 300 likes within 24 hours, then I will release two videos tomorrow. Okay? So again, it's up to you guys how fast you want this series. So um but either way guys, hope you enjoyed this episode of Network C programming and I'll see you guys next episode. Goodbye.
Original Description
200 likes = 1 video per day, 300 likes = 2 videos per day!!!
-------------------------------------------------------------------------------------------------
In this video:
- How to use command line arguments in Java
- What String[] args is
- How to set up a server
- How to code a server
-------------------------------------------------------------------------------------------------
Website: http://www.thecherno.com
Twitter: http://www.twitter.com/thecherno
Facebook: http://www.facebook.com/thecherno
Subreddit: http://www.reddit.com/r/thecherno/
Steam Group: http://www.steamcommunity.com/groups/thecherno
-------------------------------------------------------------------------------------------------
Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from The Cherno · The Cherno · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
More on: Distributed Systems
View skill →
🎓
Tutor Explanation
DeepCamp AI