简体   繁体   English

为什么第二次拒绝连接?

[英]Why it is refusing the connection for second time?

I am trying to forward a message from a client to a server and again from that server to another server. 我正在尝试将消息从客户端转发到服务器,然后再从该服务器转发到另一台服务器。 For the first time it works fine but when I type second message its say "Unexpected exception: Connection refused" why is it so? 第一次可以正常工作,但是当我键入第二条消息时,它说“意外异常:连接被拒绝”,为什么会这样呢?

Here is the code 这是代码

Client.java Client.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 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 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);
    }
}

Pls help............ 请帮助.........

A typically socket server would require some kind of loop where in the server socket would accept incoming connections and spawn a new Thread which would be responsible for actually handling the new Socket connection, leaving the current thread free to continue processing any new incoming connections, for example... 典型的套接字服务器将需要某种循环,其中服务器套接字将接受传入的连接并产生一个新的Thread ,该Thread负责实际处理新的Socket连接,从而使当前线程有空继续处理任何新的传入连接,例如例...

server = new ServerSocket(port);

while (continueAccpetingConnections) {

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

}

The SocketHandler would implement Runnable and provide a constructor that would accept a Socket variable. SocketHandler将实现Runnable并提供一个接受Socket变量的构造函数。

It would then be the responsibility of the SocketHandler to actually perform the communications required by the server. 然后由SocketHandler负责实际执行服务器所需的通信。

Now, if you wanted to have only one active connection, you might use 现在,如果您只想拥有一个活动连接,则可以使用

while (continueAccpetingConnections) {

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

}

Which would prevent any new connections until process returned... 这将阻止任何新的连接,直到process返回为止。

Your server is written to accept exactly one connection, process it in the same thread, and then exit. 您的服务器被编写为仅接受一个连接,在同一线程中对其进行处理,然后退出。 If you want to keep accepting connections, do so, in a loop. 如果要继续接受连接,请循环执行。 If you want to handle clients concurrently, start a new thread to handle each accepted socket. 如果要同时处理客户端,请启动一个新线程来处理每个接受的套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM