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. (Some of the spacing, from copying and pasting, is off but I did my best to organize it.
Main.java:
Code: Select all
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws IOException {
try
{
final int PORT = 6677;
ServerSocket server = new ServerSocket(PORT);
System.out.println("Waiting for clients...");
while (true)
{
Socket s = server.accept();
System.out.println("Client connected from " + s.getLocalAddress().getHostName());
Client chat = new Client(s);
Thread t = new Thread(chat);
t.start();
}
}
catch (Exception e)
{
System.out.println("An error occured.");
e.printStackTrace();
}
}
}
The first thing that we start with is a try/catch and the reason we need this is because we need to bind the port to the server and that throws and Exception.
The next thing that we do is the following:
Code: Select all
final int PORT = 6677;
ServerSocket server = new ServerSocket(PORT);
System.out.println("Waiting for 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
Code: Select all
Socket s = server.accept();
System.out.println("Client connected from " + s.getLocalAddress().getHostName());
Client chat = new Client(s);
Thread t = new Thread(chat);
t.start();
For those of you that don't know what a thread is, "a thread is a thread of execution in a program." (More Info) and a thread continuously runs until told to stop. 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.
So that is the end of the Main.java file. Now we are going to go to the Client.java file where all of the good stuff happens.
Client:
Code: Select all
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client implements Runnable{
private Socket socket;
public Client(Socket s)
{
socket = s;
}
@Override
public void run()
{
try
{
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true)
{
if (in.hasNext())
{
String input = in.nextLine();
System.out.println("Client Said: " + input);
out.println("You Said: " + input);
out.flush();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Before I get started with the main code, if you notice at the top next to "public class Client" we have an ending part which says "implements Runnable" and what that is doing is it is implementing the functions/methods in the Runnable interface and lets us use them in our code. 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:
Code: Select all
private Socket socket;
public Client(Socket s)
{
socket = 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..
Code: Select all
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
Finally we have our main code which gets the things the client has typed in, reads it, and resends it:
Code: Select all
while (true)
{
if (in.hasNext())
{
String input = in.nextLine();
System.out.println("Client Said: " + input);
out.println("You Said: " + input);
out.flush();
}
}
Then at the end we catch if there is an exception and close all of the brackets.
So that is the end of Part 1, of the server, and good job on getting this far!
So now we are going to start working on the Client that connects to the server.
Part 2
So now is the start of part 2 and the Client project has the same two classes as the server, Main.java and Client.java. So first im going to start with the Main.java:
Code: Select all
import java.io.IOException;
import java.net.Socket;
public class Main {
private final static int PORT = 6677;
private final static String HOST = "localhost";
public static void main(String[] args) throws IOException
{
try
{
Socket s = new Socket(HOST, PORT);
System.out.println("You connected to " + HOST);
Client client = new Client(s);
Thread t = new Thread(client);
t.start();
}
catch (Exception noServer)
{
System.out.println("The server might not be up at this time.");
System.out.println("Please try again later.");
}
}
}
Code: Select all
private final static int PORT = 6677;
private final static String HOST = "localhost";
Then we have a try/catch incase you don't connect to the server.
Inside of that we have the code that lets you connect to the server:
Code: Select all
Socket s = new Socket(HOST, PORT);
System.out.println("You connected to " + HOST);
Client client = new Client(s);
Thread t = new Thread(client);
t.start();
So that is really the only thing in the Main.java class and now we can move onto the Client.java:
Code: Select all
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client implements Runnable {
private Socket socket;
public Client(Socket s)
{
socket = s;
}
@Override
public void run()
{
try
{
Scanner chat = new Scanner(System.in);
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true)
{
String input = chat.nextLine();
out.println(input);
out.flush();
if(in.hasNext())
System.out.println(in.nextLine());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Code: Select all
private Socket socket;
public Client(Socket s)
{
socket = s;
}
Then we have this code for the input/output:
Code: Select all
Scanner chat = new Scanner(System.in);
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
Then we have our main code which gets input from the client and sends it to the Server:
Code: Select all
while (true)
{
String input = chat.nextLine();
out.println(input);
out.flush();
if(in.hasNext())
System.out.println(in.nextLine());
}
And finally...
We are done!
So that is my little client and server program I made for you guys today. I hope you guys really liked it and I hope it helps you guys in the future!
~baseball435
Source Code: here
Virus Scan: here