Java客户端-服务器程序-当我一次又一次运行客户端时,客户端连接被拒绝
嗨,休闲是我的客户程序 这是一个随机客户端程序 当我一次又一次地运行它时,我的连接被拒绝了 但是当我第一次运行这个程序时,它就起作用了Java客户端-服务器程序-当我一次又一次运行客户端时,客户端连接被拒绝,java,client-server,serversocket,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");
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
除此之外,还可以检查防火墙。有时不允许环回。最后,try with resources应该关闭正在运行的任何东西,就像它在Finally块中一样,因此s.close in客户端是不必要的。服务器中的sk也是如此。嗨,安德鲁,如果问题是与防火墙,那么我不应该得到成功的o/p第一次。当我一次又一次地运行它时,我遇到了错误