Results 1 to 3 of 3
Thread: New to client-side servers
- 02-04-2012, 04:01 AM #1
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
New to client-side servers
Hey guys,
I'm in AP CS in high school so this is probably simple stuff. I'm pretty much 10 steps ahead of everyone in my class at this point because i'm really loving this stuff so far, to the point where I created my own hangman game(~400 lines) not too long ago while everyone else was just learning what an object was. I haven't really dabbled into networks or threads yet though, just sort of looked at that kind of code so far. Seeing my interest, my teacher gave me text file with some client-side server framework(?) because I'm interested in making a networked type game. I understand some of the code, but a lot of it is foreign and i'm just wondering if someone could give me a rundown of what exactly this is, how this type of server works in laymans terms, what each class does, what a socket and port are, and the whole stream sending/receiving thing. Just all in really simple terms(for someone with zero network knowledge) would be nice, thanks.
Java Code:-----Server import java.io.*; import java.net.*; import java.util.ArrayList; /* * This class will be the hub of information passing through the server-side items. * */ public class Server { private static final int MAX_CONNECTIONS = 22; private ServerSocket listenerSocket; public ArrayList<ClientConnection> clients = new ArrayList<ClientConnection>(); private Server(int port) throws IOException { listen(port); } private void listen(int port) { try { listenerSocket = new ServerSocket(port); } catch (IOException e) { print("Server could not be initialised. Port already in use?"); System.exit(-1); } print("Server initialized on port: "+port+"."); while (true) { try { if (clients.size() < MAX_CONNECTIONS) { Socket clientSocket = listenerSocket.accept(); new ClientConnection(this, clientSocket); } } catch (IOException e) { print("Connection could not be accepted."); } } } /* * Removes the given client from registry. * @param client The client to remove. */ public void removeConnection(ClientConnection client) { try { client.user.disconnect(); clients.remove(clients.indexOf(client)); print("Client connection removed."); } catch (IndexOutOfBoundsException e) { print("Client failed to disconnect from the server."); } } /* * Reports on the server console. * @param text The reported message. */ public void print(String text) { System.out.println(text); } /* * Takes a single paramater representing the port to use. */ public static void main(String args[]) throws IOException { System.out.println("Server started."); int port; try { port = Integer.parseInt(args[0]); } catch (Exception e) { port = 25526; } new Server(port); } } -----Server Thread import java.io.DataInputStream; import java.io.IOException; import java.io.DataOutputStream; import java.net.*; /* * Works for the server to allow clients to work simultaneously. * */ public class ClientConnection extends Thread { private Server server; private DataOutputStream clientOut; private DataInputStream clientIn; public Socket client; /* * Threads the class such that Server operations and Client operations do not conflict. * @param server The Server which hosts this class. * @param client The Client this class attaches to the Server. */ public ClientConnection(Server server, Socket client) { this.server = server; this.client = client; server.print("Initializing client..."); try { clientIn = new DataInputStream(client.getInputStream()); clientOut = new DataOutputStream(client.getOutputStream()); start(); } catch (IOException e) { server.print("Client failed to initialise."); } } public void run() { getInput(); server.clients.add(this); String message = ""; try { message = clientIn.readUTF(); } catch (IOException e) { server.print("Could not communicate with client."); server.removeConnection(this); client = null; } while (client != null) { // TODO Parse message from client try { message = clientIn.readUTF(); } catch (IOException e) { server.print("Client lost connection."); break; } } try { server.removeConnection(this); clientOut.close(); clientIn.close(); client = null; } catch (IOException e) { server.print("Disconnection failed."); } } public String getInput() { try { String input = clientIn.readUTF(); verifyReceived(); return input; } catch (IOException e) { server.print("Problem getting input from client."); server.removeConnection(this); client = null; } return ""; } } -----Client /* * The program which the end-user will interact with. * */ public class Client { public Connection connection; @SuppressWarnings("serial") private Client() { // TODO Create GUI } /* * Creates a new Connection thread to interact with a Server Side Thread for a single session. * @param host The hostname of the Server; an IP, 'localhost', etc. * @param port The port that the Server is listening on. Should be between 0 and 65536. */ private void connect(String host, int port) { Connection connector = new Connection(this, host, port); } public static void main(String[] args) { new Client(); } } -----Client Thread import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; /* * Works for the Client to communicate with the Server's thread. * */ public class Connection extends Thread { private DataInputStream serverIn; private DataOutputStream serverOut; private Client client; public Socket server; public Connection(Client client, String host, int port) { this.client = client; try { SocketAddress SA = new InetSocketAddress(host, port); server = new Socket(); server.connect(SA, 10 * 1000); serverIn = new DataInputStream(server.getInputStream()); serverOut = new DataOutputStream(server.getOutputStream()); start(); } catch (IOException e) { post("Could not connect."); disconnect(); } } public void run() { String message; try { message = serverIn.readUTF(); } catch (IOException e) { post("Could not communicate with server."); message = null; } while (server != null) { if (message != null) { // TODO Parse message } try { message = serverIn.readUTF(); } catch (IOException e) { client.connection = null; break; } } disconnect(); } /* * Disconnects the connection and closes the thread. */ public void disconnect() { try { if (serverOut != null) { serverOut.close(); } if (serverIn != null) { serverIn.close(); } server.close(); } catch (IOException e) { post("Disconnection failed."); } server = null; client.connection = null; post("Disconnected."); } /* * Sends the given message to the server. * @param text The message to send. */ public void send(String text) { try { text = Client.filter(text); serverOut.writeUTF(text); } catch (IOException e) { post("There was a problem sending the message to the server."); } } }
- 02-04-2012, 11:24 PM #2
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
Re: New to client-side servers
I know someone here wants to help
-
Re: New to client-side servers
You may be asking a bit too much of a forum that is "staffed" by volunteers. For us to help you, you're going to have to do most of the reading on your own via tutorials and the like, and then if you get stuck at a specific point, ask your specific question, and you're chances of getting a helpful response will increase greatly. Wishing you much luck!
Similar Threads
-
java.rmi.UnmarshalException; on client side
By bigmac15 in forum New To JavaReplies: 1Last Post: 08-07-2011, 10:38 AM -
Need help in socket programming, client side.
By rushabh in forum NetworkingReplies: 0Last Post: 01-20-2011, 07:33 PM -
session management ON CLIENT SIDE.
By alfonz19 in forum Java ServletReplies: 1Last Post: 01-15-2011, 03:57 PM -
how to access Ms Access from client side to server side
By arunkumarinfo in forum JDBCReplies: 1Last Post: 03-20-2010, 07:03 PM -
Applet with (label client side)?
By Godsent in forum Java AppletsReplies: 1Last Post: 04-30-2009, 10:46 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks