0

メッセージをクライアントからサーバーに転送し、そのサーバーから別のサーバーに転送しようとしています。初めて正常に動作しますが、2 番目のメッセージを入力すると、「予期しない例外: 接続が拒否されました」と表示されるのはなぜですか?

ここにコードがあります

クライアント.java

import java.net.*;
import java.io.*;

public class Client {
    private Socket socket = null;
    private DataInputStream console = null;
    private DataOutputStream streamOut = null;

    @SuppressWarnings("deprecation")
    public Client(String serverName, int serverPort) {
        System.out.println("Establishing connection. Please wait ...");
        try {
            socket = new Socket(serverName, serverPort);
            System.out.println("Connected: " + socket);
            start();
        } catch (UnknownHostException uhe) {
            System.out.println("Host unknown: " + uhe.getMessage());
        } catch (IOException ioe) {
            System.out.println("Unexpected exception: " + ioe.getMessage());
        }
        String line = "";
        while (!line.equals("exit")) {
            try {
                line = console.readLine();
                streamOut.writeUTF(line);
                streamOut.flush();
            } catch (IOException ioe) {
                System.out.println("Sending error: " + ioe.getMessage());
            }
        }
    }

    public void start() throws IOException {
        console = new DataInputStream(System.in);
        streamOut = new DataOutputStream(socket.getOutputStream());
    }

    public void stop() {
        try {
            if (console != null)
                console.close();
            if (streamOut != null)
                streamOut.close();
            if (socket != null)
                socket.close();
        } catch (IOException ioe) {
            System.out.println("Error closing ...");
        }
    }

    public static void main(String args[]) {
        @SuppressWarnings("unused")
        Client client = null;
        if (args.length != 2)
            System.out.println("Usage: java Client host port");
        else
            client = new Client(args[0], Integer.parseInt(args[1]));
    }
}

AuServer.java

import java.net.*;
import java.io.*;

public class AuServer {
    private Socket socket = null;
    private Socket publishingsocket = null;
    private ServerSocket server = null;
    private DataInputStream streamIn = null;
    private String line = null;
    private DataOutputStream streamOut = null;

    public AuServer(int port) {
        try {
            System.out.println("Binding to port " + port + ", please wait  ...");
            server = new ServerSocket(port);
            System.out.println("Server started: " + server);
            System.out.println("Waiting for a client ...");
            socket = server.accept();
            System.out.println("Client accepted: " + socket);
            open();
            boolean done = false;
            while (!done) {
                try {
                    line = streamIn.readUTF();
                    System.out.println(line);
                    done = line.equals("exit");
                } catch (IOException ioe) {
                    done = true;
                }
                forward(line, 50090);
            }
            close();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    public void forward(String line, int port) {
        try {
            publishingsocket = new Socket("localhost", port);
            streamOut = new DataOutputStream(publishingsocket.getOutputStream());
            streamOut.writeUTF(line);
            streamOut.flush();
        } catch (UnknownHostException uhe) {
            System.out.println("Host unknown: " + uhe.getMessage());
        } catch (IOException ioe) {
            System.out.println("Unexpected exception: " + ioe.getMessage());
        } finally {
            try {
                publishingsocket.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public void open() throws IOException {
        streamIn = new DataInputStream(new BufferedInputStream(
                socket.getInputStream()));
    }

    public void close() throws IOException {
        if (socket != null)
            socket.close();
        if (streamIn != null)
            streamIn.close();
    }

    public static void main(String args[]) {
        @SuppressWarnings("unused")
        AuServer server = null;
        if (args.length != 1)
            System.out.println("Usage: java Server port");
        else
            server = new AuServer(Integer.parseInt(args[0]));
    }
}

AppServer.java

import java.net.*;
import java.io.*;

public class AppServer {
    private Socket socket = null;
    private ServerSocket server = null;
    private DataInputStream streamIn = null;

    public AppServer(int port) {
        try {
            System.out.println("Binding to port " + port + ", please wait  ...");
            server = new ServerSocket(port);
            System.out.println("Server started: " + server);
            System.out.println("Waiting for a client ...");
            socket = server.accept();
            System.out.println("Client accepted: " + socket);
            open();
            boolean done = false;
            while (!done) {
                try {
                    String line = streamIn.readUTF();
                    System.out.println(line);
                    done = line.equals("exit");
                } catch (IOException ioe) {
                    done = true;
                }
            }
            close();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    public void open() throws IOException {
        streamIn = new DataInputStream(new BufferedInputStream(
                socket.getInputStream()));
    }

    public void close() throws IOException {
        if (socket != null)
            socket.close();
        if (streamIn != null)
            streamIn.close();
    }

    public static void main(String args[]) {
        @SuppressWarnings("unused")
        AppServer server = null;
        server = new AppServer(50090);
    }
}

助けてください............

4

2 に答える 2

0

サーバーは、正確に 1 つの接続を受け入れ、同じスレッドで処理してから終了するように作成されています。接続を受け入れ続けたい場合は、ループしてください。クライアントを同時に処理したい場合は、新しいスレッドを開始して、受け入れられた各ソケットを処理します。

于 2013-10-29T22:33:20.513 に答える