All pastes #340006 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-02-04 14:09 UTC
rendered paste body
import java.io.*;
import java.net.*;
import java.util.*;
 
class ServerText {

    // this is a list of all TextClientHandler, which we will need to store somewhere
    // in order to send them information
    private ArrayList<TextClientHandler> tcHandlers = new ArrayList<TextClientHandler>();

    // this is the static variable the changes of which are all reported
    private int myVar;

    // this is done via a setter: all changes to the var are done with
    // this function
    public void changeVar( int newValue ) {
        
        // the actual functionality is provided here
        myVar = newValue;

        // and here all the TextClientHandlers receive information that
        // the var has changed, provide this object & new value of the
        // variable
        for ( TextClientHandler i : tcHandlers )
            i.inform( this, myVar );

    }



    public static void main(String[] args) {
        try {
            // lyt på port 12345
            ServerSocket ss = new ServerSocket(12345);
            
            // create a non-static instance of this class
            ServerText st = new ServerText();
                    
            while(true) {
                // accepter connection fra client
                Socket s = ss.accept();
                // start tråd til at håndtere client
                TextClientHandler tch = new TextClientHandler(s);

                // STORE the new TextClientHandler in order to inform it!
                st.tcHandlers.add( tch );

                tch.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
}
 
class TextClientHandler extends Thread {
    private Socket s;
    private BufferedReader br;
    private PrintWriter pw;
    public TextClientHandler(Socket s) {
        try {
            // initialiser læse og skrive klasser
            this.s = s;
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw = new PrintWriter(s.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void inform ( ServerText st, int newValue ) {

        // a new value was given to the variable!!!
        // do whatever you do then

    }
    public void run() {
        try {
            String line;
            // læs input fra client
            while((line = br.readLine()) != null) {
                // split input fra client op i dele
                String[] parts = line.split(" ");
                if(parts[0].toUpperCase().equals("ADD")) {
                    // læg to heltal sammen
                    int a = Integer.parseInt(parts[1]);
                    int b = Integer.parseInt(parts[2]);
                    int c = a + b;
                    pw.println("RES " + c);
                    pw.flush();
                } else if(parts[0].toUpperCase().equals("DUP")) {
                    // dupliker en streng
                    String s = parts[1];
                    String s2 = s + s;
                    pw.println("RES " + s2);
                    pw.flush();
                } else {
                    System.err.println("Unknown command: " + line);
                }
            }
            // luk læse og skrive klasser
            br.close();
            pw.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}