Simple Client And Server Chat Program - Java Tutorials. Hey everyone, today I have been working on a simple server and client program where a client types something in, the server receives it, and then it sends it back to the client.
Simple Chat Program In Java (computer project) Instances since qualities use felt flexibility etc nbspNew pink something client. Libraries paper experimental results. Simple Java Client/Server Program. But when I put the client program on a different computer.
After about a half an hour I finished it and decided to put a tutorial onto here. Yes it's not too useful but this is the basics of a chat client and can help you understand server and clients better. Before I start I just want to say that if you don't like the way my code looks, please don't criticize.
Everyone has their own way of organizing and setting up code so don't put comments saying your code doesn't appeal to me. Lets get started! Part 1. So for this there will be two separate projects, a Client project and a Server project.
We are going to start with the Server project first. In this we will have to classes, a Main class (what starts the server and accepts clients) and a Client class (An object that we will set each client to through their socket). So I'm going to put the code up and then explain each important line and why we need it. Then we make a new Server.
Simple Chat Program In Java Computer Project Ideas
It include two projects Server and Chat.
Socket and bind it to our port variable. Then finally we print out that we are waiting clients.
The next thing that we have is a while loop which is practically saying while the program is running, do everything inside of it. Then we have the main part which waits for a socket (client) to connect and when one does, it creates a new Client object with the parameter of the Socket. Socket s = server. System. out. println(.
Then we print out the address they connected from just to let the server owner know when someone connects. After that we create our new Client object, which you will see soon, and then we create a new thread on the next like. Then we start the thread. For those of you that don't know what a thread is, . You can also have multiple threads running at once and at the same time, not in the order of execution in the code.
Then at the end of the file we close all of the brackets and catch the exception if there is one. Now we are going to go to the Client. Client. import java. Print. Writer. import java.
Socket. import java. Scanner. public class Client implements Runnable. This is what is needed for a thread.
When a thread starts, it look for the run method and does everything inside of there over and over. The first part is our constructor and our instance variable: socket. Socket socket. public Client(Socket s).
So what I did is I made a Socket as the parameter in then I set the instance variable, socket, to the value of s. The next code we have is our run method which we implemented from the Runnable interface and it will run everything inside of it. Then we have a try/catch which we need to use incase of the next part. These will be used to send and get data from the client. The reason we have that try/catch is because if the socket isnt set or their is a problem getting the input/output stream, it will throw an exception. Finally we have our main code which gets the things the client has typed in, reads it, and resends it. Inside of it we have an 'if' statement which is saying if the client has typed something in and pressed enter, do everything inside.
So inside first we set what they typed to a new String variable: input, we print out what they said, we sent it back to them with the out stream, and then we flush the out stream. If you don't flush it the message will never get sent (You can try that by commenting out the code).
Then at the end we catch if there is an exception and close all of the brackets. So first im going to start with the Main.
IOException. import java. Socket. public class Main . After that we create a new Client Object with the parameter of the socket. Then we use that object and create a new thread to run whats inside of it. So that is really the only thing in the Main.
Client. java. import java. Print. Writer. import java. Socket. import java.
Scanner. public class Client implements Runnable . We also have the class implement the Runnable interface so that we can use it with a Thread. Then you see the first line which sets the Scanner variable chat to a new Scanner with the parameter of System.
What this is doing is creating a variable to let the Client type things on the screen. Then we have our main code which gets input from the client and sends it to the Server. Then inside of that we set the String variable input to whatever the client types in. So if he doesnt type anything in, it will just sit there until he does and it wont do the rest of the code. Then we send out whatever they typed in to the client with the out stream.
After that we flush the out stream and if we don't it wont send it completely. Then we have an 'if' statement which is saying if the server sent us something through the input stream then do the next line. What the next line is doing is simply printing out what the server sent us. Then after that we close all of the brackets and catch the exception if there is one.
And finally.. We are done! I hope you guys really liked it and I hope it helps you guys in the future!