0

我正在尝试将消息从客户端转发到服务器,然后再从该服务器转发到另一台服务器。第一次它工作正常但是当我输入第二条消息时它说“意外异常:连接被拒绝”为什么会这样?

这是代码

客户端.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]));
    }
}

应用服务器.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

1

例如,典型的套接字服务器将需要某种循环,其中服务器套接字将接受传入的连接并产生一个新Thread的负责实际处理新连接的新Socket连接,从而使当前线程可以自由地继续处理任何新的传入连接。 ..

server = new ServerSocket(port);

while (continueAccpetingConnections) {

    Socket socket = server.accept();
    Thread thread = new Thread(new SocketHandler(socket));
    thread.start();

}

SocketHandler实现Runnable并提供一个接受Socket变量的构造函数。

然后SocketHandler由服务器负责实际执行服务器所需的通信。

现在,如果您只想拥有一个活动连接,您可以使用

while (continueAccpetingConnections) {

    Socket socket = server.accept();
    process(socket);

}

这将阻止任何新的连接,直到process返回......

于 2013-10-29T05:24:31.357 回答
0

你的服务器被写成只接受一个连接,在同一个线程中处理它,然后退出。如果您想继续接受连接,请在循环中这样做。如果要同时处理客户端,请启动一个新线程来处理每个接受的套接字。

于 2013-10-29T22:33:20.513 回答