All pastes #716535 Raw Edit

Untitled

public text v1 · immutable
#716535 ·published 2007-09-26 22:37 UTC
rendered paste body
import java.io.*;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.*;
import java.net.ServerSocket;

abstract public class DaemonBase extends ExecutableBase implements Runnable {

        final int DEFAULT_PORT = 5000;
        public int port = 0; 
        // define port associated (arg)
        // 
        // opens a socket thread

        public DaemonBase() {
                this.port = this.DEFAULT_PORT;
        }

        public DaemonBase(int port) throws IOException {
                this.port = port;
        }

        public void run() {
                ServerSocket server;
                Socket client;
                BufferedReader in;
                PrintWriter out;
                String line;

                try {
                        server = new ServerSocket(this.port); 
                } catch (IOException e) {
                        System.out.println("Could not listen on port " + port);
                        System.exit(-1);
                }


                while(true) { 
                        try {
                                client = server.accept();
                        } catch (IOException e) {
                                System.out.println("Accept failed: " + port);
                                System.exit(-1);
                        }

                        try {
                                in = new BufferedReader(new InputStreamReader(
                                                        client.getInputStream()));
                                out = new PrintWriter(client.getOutputStream(), 
                                                true);
                        } catch (IOException e) {
                                System.out.println("Read failed");
                                System.exit(-1);
                        }

                        while(true) {
                                try {
                                        line = in.readLine();
                                        if (line.equals("ping")) {
                                                //Send data back to client
                                                out.println("pong");
                                        }
                                } catch (IOException e) {
                                        System.out.println("Read failed");
                                        client.close();
                                }
                        }
                }
        }
}


jay@slicer:~/ee/daemon$ javac DaemonBase.java 
DaemonBase.java:40: variable server might not have been initialized
                                client = server.accept();
                                         ^
DaemonBase.java:48: variable client might not have been initialized
                                                        client.getInputStream()));
                                                        ^
DaemonBase.java:58: variable in might not have been initialized
                                        line = in.readLine();
                                               ^
DaemonBase.java:61: variable out might not have been initialized
                                                out.println("pong");
                                                ^
DaemonBase.java:65: variable client might not have been initialized
                                        client.close();
                                        ^
DaemonBase.java:65: unreported exception java.io.IOException; must be caught or declared to be thrown
                                        client.close();
                                                    ^
6 errors
jay@slicer:~/ee/daemon$