JoD Ep1: Building a Multi-User Chat Application in Java - Part 1: ServerSocket

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so in this series of videos we're going to look at how to create a multi-user chat application so at the end of the series of videos we're going to have a complete working version of a multi-user chat system right but in this video we're going to start from the beginning and so in this video we're going to look at how to create a prototype for the server for the chat server because in a multi-user chat system you're going to have multiple clients that you're going to have a central server that the mote that the clients communicate with right so what you see on the screen already have a project created for this application called multi-user chat and in that application I'm going to create multiple modules which represents the server and the client piece so let's go ahead and create two modules one for the server and then one for the client so I'm going to create the server first right I'm going to name this module called the chat server so I'm going to create another module right and that will be the one for the client I'll call it chat client okay so now we have two modules one chat server and one for the chat client so in this video we just focus on the chat server how we can create a simple working chat server all right so I'm going to create a class pause overlay and this will be the entry point for my server application okay so I'm going to just create this main program right and when you create a chat server it's basically a network server so so in order for us to create a network server we have to create something called a server socket and parameter to a server socket is a port and so in this case let us define a port and we'll call will use this port as eight one eight okay so here I can pass that port into the instance of server socket and do some exception exception handling right and okay so once we set up a server right we can watch your server server socket we can then call this a accept method right and this accept method is the one that actually creates the connection between the server and the client all right and this except it's going to return us a socket and this socket represents the connection to the client that's already I call it the client socket right and then you need to put this in a loop because you're going to continuously accept the connections from the client main purpose of a server socket is to basically start to accept connections from connecting clients and each time that you call accept it actually returns a new client socket if there are no connections this accept will block so that's why we do this in a wild loop so for the sake of just you know proving that it works right what we're going to do is we're going to right now something to the client socket and then close that client socket connection and the way that we do that is every socket has a output stream right and what we're going to do is we're going to just turn out some string to this output stream okay so now we can run this okay so it's running and I'm going to use my terminal I'm going to use a program called telnet alright to check to see if my my server is working right because here this the second parameter to telnet is going to be my port and that's going to match whatever port I define in my program all right so here you see that it actually connected print out this hello world which is actually the same string that right here that we are writing to the socket and it finally closes it that's why you see the X got closed by the foreign host which is the server itself so this is the beginning of a simple chat server right so here you know we created a server socket now which pass at a port which we define as eight one eight so in the server socket you always have to call accept which will accept any client connections from the server all right so let's put more debug statement so you can see this working okay so I added to print out statements one before we accept the connection than one showing that we actually accepted the connection okay so now if I do this again so you can see that this accept is going to block until we actually create a connection to that server and so let's see what happens when we do use the telnet so right now we're going to print out where this connection coming from right so you can see that actually says you know accept the connection from socket and then this is address and then this port right which is the client port and then this is going to be the server port right and then after that it's going to do it again so this while loop is going to keep executing until until we actually terminate the program okay so so far we have a very basic working version of our chat server right we're able to create a server socket which is listening out this port eight one eight right and then from there you know we we have a while loop infinite loop basically the calls except which accepts a connection from the client and then from there we print out hello world to the client and then close the connection but there's a problem with this loop and the problem is that when you are processing the connection from the client you cannot accept any more connections so let me show you why this is a problem all right so instead of closing this connection immediately all right let's say that we we we want to write out something for ten seconds right so during that ten seconds this means that no other can client can connect to the server all right so this this let me submit like that for you and so I'm going to create a loop it's going to print out the time so I'm going to need to wrap this in tool right and then and then between each iteration I'm going to sleep for a second okay so now when I run this program again and I use my telnet to test the program clear the screen first now you can see that every second the server is going to print out what time it is and then send that to the client so that's all good except that let's see what happens when we actually have two clients running so now I'm going to create a another terminal right and you're going to see what happens when I have multiple connections to that server and so here right the first is the first terminal is connected right but what happens when I run the second connection right so the second connection actually blocks until the first connection is finished right that's because we have a single thread on the server does processing the client connection but at the same time you cannot accept any new connection because of this while loop that we have here right so this while loop will execute for ten seconds because we do this for ten times but each time we sleep for one second so that's a total of ten seconds right so while it's sleeping or whilst processing the client connection it cannot execute execute this accept and that's basically when it accepts the connection from the client all right so in order to address that you know we have to go ahead and then spot off another thread that is responsible for executing this piece of code right and that's what we're going to do next so in order for the server to handle multiple connections you have to use the main thread to accept the connections from the client and then use another thread to handle the communication with the established clients right so that's what I'm going to do right here all right so as we see before the reason why we cannot handle multiple connections is that this for loop is going to sleep for 10 seconds and then that's going to block and block this main thread from accepting other connections right so what we have to do is we have to allow this main thread to keep accepting connections and then delegate the handling of the client connections to another thread and so that's what we're going to do right now by refactoring this piece to a separate method so I can extract those that that that block of code into a method and this will handle okay so here I just basically move all that piece of code into another method called handle client socket right and then once I do that then I can actually create another thread right and in the run method of that thread I will call this method and then finally I can then start that thread okay so what I'm doing is basically to create a new thread every time we get a connection from the client right we create a new thread and in that new thread we basically going to call this handle client socket by passing it the client socket that is tied to that client connection all right so now let's see how that works okay so I have my two terminals all right so now if I run the first connection right and I run the second one so now they're both executing concurrently so that's how you handle multiple connections right by using different threads to handle the client communication and then you keep the main thread free to accept other connections okay so let's make this code a little bit more prettier right by not having to create this thread but to let's create a new class right we call this server worker which is going to extend the thread right and in the new server worker so here the server worker alright so we now have this so instead of having this thread right we're going to just create a new instance of sewer worker and then we will pass in the client socket which is pretty constructor with that we just so great that feel and then every thread has a run method and this button method will do whatever this handle socket does and so now we just move this piece into that server worker I won't be static and then now this run we'll just call this crank socket which you don't need that you don't need this because that class variable is going to be a client socket okay so now once I create the Walker instance then I can just don't start that and then let me no longer need this piece here right so now we run it again we can test this okay and still continues to work okay so that's how you basically setup the server piece right so to recap first we have the Silvermane which creates a server socket that listens on a port and then once you do that then you have a infinite loop that accepts incoming connections once you have a connection then you can spawn off a worker which handles the communication with that client socket all right so this server worker basically is a thread right and then that thread when it runs basically call this handle client socket and then from there it basically does what it's supposed to do to communicate with the client okay this will work for a limited number of clients right because we have to create a thread for every client connection so if you have too many client connections you're going to have too many threads and you know normally when you have too many threads the system cannot support that many threads but there's actually workaround for that and then that's basically you have to get into multiplexing i/o and now what we will do next time okay so up until now we only been sending data back to the client right so let's see how we can communicate with the client back and forth all right so here in this handle client socket right right now we're just printing out some some strings to the client and we terminate the socket connection right but what we can do is we can actually read data from the client and also send data back to the client right and the way that we do that is by by getting access to the input stream for reading the data from the client and then likewise we have the output stream that we can access from the client socket to get data from from the to get data from the client okay so this will be our output stream right so using the input stream and operation we have a bi-directional communication with the client connection okay so what I'm going to do here is run a greatly buffer reader and so we can read line by line okay so so I have a line alright so what I'm going to do is in this reader I'm going to read each line and then if you know that lines is right then I will break out this while loop right until then it will it will it will keep reading the lines right and then what I will do is that if it's not quit then I'm going to echo back a message right and then that message you're going to say you typed and then plus whatever it was received from the client I'm going to have to pin our new line just so then we can have better formatting okay so let's see how this works okay so now when I connect I'm going to say hello and I'm going to send hello and then it's going to send me back you type hello all right and and then one two three right and until finally if I type quit then that connection get closed by the server all right so I'd do the same thing next one to put and quit so so basically what I've done is I was able to read the input from the client and then I go back whatever the client has typed right so this is really the basis foundation for creating a communication between the client and server so that's basically how you're going to create the server piece that can handle multiple client connections and read the input from the client and then also send up send the output back to the client all right so this basically you have that bi-directional communication with each individual client connection right so what we've done today is we have created a foundation for our chat server right and then next time we'll work on the chat client okay so if you want to get the project file for this video right also that includes the source code you can go to objects and accomm slash ep-1 and this will take you directly to the page that we can download the project files and so let's take a look at the summary of today's video so we learned that we need to have a server socket in order to accept connections right by calling this accept method right and we have that except trendex that will create a socket which is going to be your client socket right and then using that client socket you know we're able to get the output stream right and then we also it will get the input stream and then the input stream in the opposition are two ways that you can send and read data so there's actually bi-directional communication with the client right we also learn that you know we need a worker thread trying to handle client communication right this is and that's because we want to accept multiple connections and because we saw that we saw that if we did not spot off another thread to handle the client communication that that the the main drive it will block and then we only able to handle one client at a time right as I we need to have multiple client connections okay so thank you for watching and I'll see you next time
Info
Channel: FullStackMastery
Views: 80,926
Rating: undefined out of 5
Keywords: Java, Networking, Multi-User Chat, Instant Messaging, Server Socket, Chat Server, Sockets, Programming
Id: cRfsUrU3RjE
Channel Id: undefined
Length: 22min 51sec (1371 seconds)
Published: Wed Apr 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.