Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: New to client-side servers

  1. #1
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default 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."); }
    	}
    }

  2. #2
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: New to client-side servers

    I know someone here wants to help

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: New to client-side servers

    Quote Originally Posted by Bestsanchez View Post
    ... 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:
    // ... lots and lots and lots of code deleted for sake of brevity...
    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!
    Bestsanchez likes this.

Similar Threads

  1. java.rmi.UnmarshalException; on client side
    By bigmac15 in forum New To Java
    Replies: 1
    Last Post: 08-07-2011, 11:38 AM
  2. Need help in socket programming, client side.
    By rushabh in forum Networking
    Replies: 0
    Last Post: 01-20-2011, 08:33 PM
  3. session management ON CLIENT SIDE.
    By alfonz19 in forum Java Servlet
    Replies: 1
    Last Post: 01-15-2011, 04:57 PM
  4. Replies: 1
    Last Post: 03-20-2010, 08:03 PM
  5. Applet with (label client side)?
    By Godsent in forum Java Applets
    Replies: 1
    Last Post: 04-30-2009, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •