hey guys, recently I have been learning how to program a server in Java, and I actually don't need help for once
, anyway I was wondering if I could get some feedback on the concept of my wonderful creation, I have read lots of tuts and realize there is a lot of ways to go about it, which is why I was wondering if this is efficient, from my perspective it seems to do its justice, at the moment its only exchanging a tiny amount of data, but it seams to fly through it, so here it is:
this is my server class:
package myServer;
import java.net.*;
import java.util.ArrayList;
import java.io.*;
public class Server {
public static Server server;
public ArrayList<ServerClientThread> users;
private boolean running;
private ServerSocket serverSocket;
private int port = 4444;
private Socket socket;
public static void main(String[] args) {
server = new Server();
}
//constructor begin
public Server() {
users = new ArrayList<ServerClientThread>();
serverSocket = null;
running = true;
try {
serverSocket = new ServerSocket(port);
System.out.println("the server has sucessfully launched");
} catch (IOException e) {
System.err.println("Could not listen on port: " + port + ".");
System.exit(-1);
}
run();
}
//constructor end
//run method begin
public void run() {
while(running) {
try {
socket = serverSocket.accept();
users.add(new ServerClientThread(socket, this));
users.get(users.size() - 1).start();
System.out.println("the server accepted a conection");
} catch (IOException e) {
e.printStackTrace();
}
}
}
//run method end
}
then this is my ServerClientThread class that manages each client on the server:
package myServer;
import java.io.*;
import java.net.*;
public class ServerClientThread extends Thread {
private Server server;
private Socket socket = null;
private boolean running;
private ObjectInputStream in;
private ObjectOutputStream out;
//constructor begin
public ServerClientThread(Socket socket, Server server) {
super("ServerClientThread");
this.socket = socket;
this.server = server;
try {
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
System.out.println("client thread successfully created");
} catch (IOException e) {
e.printStackTrace();
}
running = true;
}
//constructor end
//run method begin
public void run() {
int x = 0;
while (running) {
try {
byte command = (byte) in.readObject();
if (command == 0) {
out.writeObject(x);
x++;
} else if (command == -1) {
System.out.println("the close command has been proccessed, thread and client will now shutdown");
close();
System.out.println("also the client will be removed from the user list on the server for convinience");
server.users.remove(this);
}
} catch (IOException e) {
e.printStackTrace();
close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
close();
}
}
}
//run method end
//close method begin
public void close() {
try {
running = false;
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//close method end
}
and allas my Client class:
package myServer;
import java.io.*;
import java.net.*;
public class Client {
private Socket socket;
private int port = 4444;
private boolean running;
private ObjectInputStream in;
private ObjectOutputStream out;
//PrintWriter out = null;
//BufferedReader in = null;
public static void main(String[] args) {
try {
Client client = new Client();
} catch (IOException e) {
e.printStackTrace();
}
}
//constructor begin
public Client() throws IOException {
try {
socket = new Socket("localhost", port);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + port + ".");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: " + port + ".");
System.exit(1);
}
running = true;
run();
}
//constructor end
//run method begin
public void run() {
byte command;
int x = -1;
while (running) {
try {
if (x < 10000) {
command = 0;
out.writeObject(command);
x = (int) in.readObject();
System.out.println(x);
} else {
command = -1;
out.writeObject(command);
close();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//run method end
//close method begin
public void close() {
try {
running = false;
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//close method end
}
so ye its pretty simple right now, the server just sits and waits for connections coming in, when they do it creates a new thread to manage that connection, then the thread and the client commute an integer till it reaches 10,000, when it does the client sends a shut-down signal to the server thread, and they both close, pretty simple but its a start