0

每当我通过调用创建ServerSocket并观察套接字地址时getLocalSocketAddress(),我都会看到:

0.0.0.0/0.0.0.0:xxxx(xxxx是随机端口号)

我的服务器代码是:

try{
    Boolean end = false;
    ServerSocket ss = new ServerSocket(0);
    System.out.println("Program running, Server address:" + ss.getLocalSocketAddress().toString());
    while(!end){
        //Server is waiting for client here, if needed

        Socket s = ss.accept();
        System.out.println("Socket Connected !");  
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
        String st = input.readLine();
        System.out.println("Tcp Example From client: "+st);
        output.println("Good bye and thanks for all the fish :)");
        s.close();

    }
    ss.close();  
 } catch (Exception ex) {
     ex.printStackTrace();
 }
4

1 回答 1

0

不要在 ServerSocket 中分配“0”

端口是从 0-65535 ,但要使用它的 1 - 65534 .. MoreOver 尝试使用 1024 以上的端口,因为它被其他知名服务使用,如 Telnet、FTP、HTTP 等......

查看我的代码......它可能会帮助你执行你的代码......

服务器端代码示例:

    public class ServerTest {

        ServerSocket s;

        public void go() {

            try {
                s = new ServerSocket(4445);

                while (true) {

                    Socket incoming = s.accept();
                    Thread t = new Thread(new MyCon(incoming));
                    t.start();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }

        }

        class MyCon implements Runnable {

            Socket incoming;

            public MyCon(Socket incoming) {

                this.incoming = incoming;
            }

            @Override
            public void run() {

                try {
                    PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
                            true);
                    InputStreamReader isr = new InputStreamReader(
                            incoming.getInputStream());
                    BufferedReader br = new BufferedReader(isr);
                    String inp = null;

                    boolean isDone = true;

                    System.out.println("TYPE : BYE");
                    System.out.println();
                    while (isDone && ((inp = br.readLine()) != null)) {

                        System.out.println(inp);
                        if (inp.trim().equals("BYE")) {
                            System.out
                                    .println("THANKS FOR CONNECTING...Bye for now");
                            isDone = false;
                            s.close();
                        }

                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    try {
                        s.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                }

            }

        }

        public static void main(String[] args) {

            new ServerTest().go();

        }

}
于 2012-07-02T12:20:38.967 回答