Author Topic: java Server  (Read 1214 times)

Offline diagostimo

  • Ethereal Devastator
  • ****
  • Posts: 257
  • LV: 20
    • View Profile
java Server
« on: February 14, 2013, 12:41:54 PM »
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:
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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 :D
« Last Edit: February 14, 2013, 12:44:37 PM by diagostimo »

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5135
  • LV: 198
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: java Server
« Reply #1 on: February 15, 2013, 07:42:14 PM »
The only suggestion I can think of would be to move the closing of the io streams and the socket out of the close method to just outside the while loop in the run method. otherwise there is a possibility (abet small) that when you call the close method it could be in the middle of the while loop and you'll crash that thread.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline diagostimo

  • Ethereal Devastator
  • ****
  • Posts: 257
  • LV: 20
    • View Profile
Re: java Server
« Reply #2 on: February 16, 2013, 12:57:24 AM »
hey, thanks for the reply, ye ill do that, as it is now there is no more references to the streams in the loop that cause a crash when it shuts down, but when I come to add more yes I could definitely have an issue there.
also I have been looking into how to set up a database with MYSQL, and so far its going pretty well, the only question I have really is how to implement my database into my server, I know that connecting to it directly from the client would be stupid, as it is server side, but should I connect to it through the main Server, or the thread that handles the client, I know that I could connect to the database when the server launches, and get the clientThreads to go through the server to acquire information, but what would be the most practical way?

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5135
  • LV: 198
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: java Server
« Reply #3 on: February 16, 2013, 02:02:14 AM »
you connect the server to the database and have the server change and add things too it
the database is there so that server can store and retrieve data quickly
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.

DropBox, the best free file syncing service there is.
(click to show/hide)