Java客户端服务器程序 - 当我一次又一次地运行客户端时拒绝客户端连接

时间:2016-04-12 21:55:49

标签: java client-server serversocket

我的客户端程序就是休息 这是一个随机客户端程序 当我一次又一次地运行它时,我的连接被拒绝了 但是当我第一次运行程序时,这是有效的

 public static void main(String args[]) throws UnknownHostException, IOException{
    try(Socket s = new Socket("127.0.0.1",9651)){
        System.out.println("Enetr a number");
        Scanner sc = new Scanner(System.in);
        int ip= sc.nextInt();
        System.out.println("hello-----"+ip);
        PrintStream p= new PrintStream(s.getOutputStream());
        p.println(ip);
        System.out.println("hello-----"+p);
        BufferedReader in = new BufferedReader( 
                new InputStreamReader( s.getInputStream()));
        //DataInputStream in=new DataInputStream(s.getInputStream());
        int x= in.read();
        System.out.println("test test ----"+x);
        sc.close(); 
        s.close();

    }
    catch(IOException e){
        System.out.println(e.getMessage());
        System.exit(1);

    }


}

我的服务器程序

public static void main(String args[]) throws IOException{
    int m;
    try(ServerSocket ss= new ServerSocket(9651)){
        System.out.println("server created at port 1321");
        try(Socket sk=ss.accept()){
            System.out.println("a connection is made with some client");
            BufferedReader in = new BufferedReader( 
                    new InputStreamReader( sk.getInputStream()));
            m=in.read();
            int q=doub(m);
            trade(q);
            PrintStream p = new PrintStream(sk.getOutputStream());
            p.println(q);

            System.out.println("testing....m"+m+"-----q"+q);
            sk.close();
        }
        catch(IOException e){
            System.out.println(e.getMessage());
            System.exit(1);
        }
        ss.close();
    }
    catch(IOException e){
        System.out.println(e.getMessage());
        System.exit(1);
    }



}

public static int doub(int x){
    System.out.println("calling the double method");
    int z=x*2;
    return z;

}
public static void trade(int y){
    if(y>=0){
        System.out.println("working well");
    }
    else
        System.out.println("quit the comp");

}

错误o / p

Connection refused: connect

0 个答案:

没有答案