0

我有 Socket-Client 应用程序。下面的代码应该允许客户端同时读取来自服务器的回复和读取输入。问题是——这段代码在 Eclipse 中调试时“工作”,就像我在正在调试的进程中收到超出正常流程的消息一样,但是如果我正常启动应用程序,它会完全忽略该进程吗?“IDE 工作,现实生活不工作”综合症的最常见原因是什么?

整个文件:

服务器:

public class Server implements Runnable {

static ServerSocket serverSocket;
Socket tempSocket;
Socket tempSocket2;
static volatile List<User> usersList = new ArrayList<User>();
static boolean waitForNew = true;
PrintWriter tempOut;
volatile User[] tempUser;
volatile boolean isReadingN = false;

public Server(Socket _s, Socket _s2) {
    tempSocket = _s;
    tempSocket2 = _s2;
}

public Server(PrintWriter nOut, User[] user) {
    tempOut = nOut;
    tempUser = user;
    isReadingN = true;
}

@Override
public void run() {
    if (isReadingN) {
        while (true) {
            if (tempUser != null && tempUser.length > 0
                    && tempUser[0] != null)
                break;
        }
        User[] myUser = new User[1];
        myUser[0] = tempUser[0];
        // myUser[0]=usersList.
        while (true) {
            if (myUser[0].isCurrentlyLoggedIn() == false)
                break;
            String[] toSend = null;
            if (myUser[0].isNotificable())
                toSend = myUser[0].printNotifications().split("\n");
            else
                continue;
            //tempOut.println("");
            int sendL=toSend.length;
            tempOut.println(String.valueOf(sendL));
            for (int i = 0; i < toSend.length; i++)
                tempOut.println(toSend[i]);
        }
        return;
    }
    Socket clientSocket = tempSocket;
    System.out.println("Initiating conversation with the client");
    String inputLine;
    try {
        System.out.print("creating server out...");
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
                true);
        Socket iClientSocket = tempSocket2;
        ObjectOutputStream iout = new ObjectOutputStream(
                iClientSocket.getOutputStream());
        System.out.println("OK!");
        System.out.print("creating server in...");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
        System.out.println("OK!");
        System.out.print("creating server image streams...");
        System.out.println("OK!");
        System.out.println("Server initiating conversation");
        User[] currentUser = new User[1];
        new Thread(new Server(out, currentUser)).start();
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
            boolean[] downloadPicture = new boolean[1];
            downloadPicture[0] = false;
            String input = Command.call(inputLine, currentUser, usersList,
                    downloadPicture);
            String[] toSend;
            if (input != null) {
                toSend = input.split("\n");
            } else
                toSend = new String[0];
            out.println(String.valueOf(toSend.length));
            for (int i = 0; i < toSend.length; i++)
                out.println(toSend[i]);
            if (downloadPicture[0]) {

                    System.out.println("File sent.");

                    iin.close();

                } else{
                    out.println("1");
                    out.println("Error: File does not exit.");}
            } else
                //out.println(" ");
            if (inputLine.equals("EXIT")) {
                waitForNew = false;
                break;
            }
        }
        // End communication graciously
        System.out.println("Closing sockets, closing streams");
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    } catch (IIOException e) {
        System.out.println("Error: Could not find file");
        e.printStackTrace();
        System.exit(-1);
    } catch (IOException e) {
        System.out.println("Error");
        e.printStackTrace();
        System.exit(-1);
    }

}

public static void main(String[] args) {
    // Create socket on port given in argument, localhost
    if (args.length == 0) {
        System.out
                .println("Not enough arguments. Try Server <port number>");
        System.exit(-1);
    }
    int port = 0;
    try {
        port = Integer.valueOf(args[0]);
        System.out.println("Application start");

        serverSocket = new ServerSocket(port);
        System.out.println("Created socket on port " + port);
    } catch (NumberFormatException c) {
        System.out
                .println("Incorrect port number. Try Server <port number>");
        System.exit(-1);
    } catch (IOException e) {
        System.exit(-1);
    }

    // Waiting for client
    System.out.println("Waiting for client...");
    Socket clientSocket = null;
    Socket iClientSocket = null;
    while (waitForNew) {
        try {
            clientSocket = serverSocket.accept();
            iClientSocket = serverSocket.accept();
            new Thread(new Server(clientSocket, iClientSocket)).start();
        } catch (IOException e) {
            System.out.println("Accept failed: " + port);
            System.exit(-1);
        }
    }

}

}

客户:

public class Client implements Runnable {
static Socket clientSocket = null;
static Socket iClientSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;
static InputStream iin = null;
public static void main(String[] args) {
    int port = Integer.valueOf(args[1]);
    String host = args[0];

    try {
        clientSocket = new Socket(host, port);
        iClientSocket = new Socket(host, port);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
        iin = iClientSocket.getInputStream();
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + host);
        System.exit(-1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for " + "the connection to: "
                + host);
        System.exit(-1);
    }

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(
            System.in));
    String userInput;

    try {
        new Thread(new Client()).start();
        while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput);
        }
        System.out.println("Closing sockets, closing streams");

        out.close();
        in.close();
        stdIn.close();
        iClientSocket.close();
        clientSocket.close();

    } catch (IOException e) {
        System.exit(-1);
    }

}

@Override
public void run() {
    String a = null;

    try {
        while (true) {
            if((a = in.readLine()) == null)
                continue;
            int n;
            try{
                n = Integer.valueOf(a);
            }catch(NumberFormatException e){
                System.out.println(a);
                n=1;
                //continue;
            }
            a = "";
            for (int i = 0; i < n; i++)
                a += in.readLine() + "\n";
            System.out.println(a);
            // if(a.contains("POST"),)
            if (a.compareToIgnoreCase("EXIT") == 0) {
                System.out.println("Exiting");
                break;
            }
            if (a.endsWith("Sending File\n")) {

                System.out.println("Recieving image.");
               (some unimportant for now code)
                System.out.println("Image recieved");

            }
        }
    } catch (IOException e) {

    }

}

}

4

0 回答 0