当前位置: 技术问答>java相关
命令行 “java client servername” args[0] 代表 servername 吗?
来源: 互联网 发布时间:2015-04-29
本文导语: public class EchoClient1 { public static void main(String args[]) { try{ if (args.length != 1){ System.out.println("USAGE: java Client servername"); return; } String connectto= args[0]; Socket connection; ...
public class EchoClient1 {
public static void main(String args[]) {
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),8500);
}
else{
connection=new Socket(InetAddress.getByName(connectto),8500);
}
BufferedReader input=new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream(), true /* autoFlush */);
// read information from server
String info;
info = input.readLine();
System.out.println(info);
boolean done = false;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String sInput;
while(!done){
sInput = in.readLine();
out.println(sInput);
if (sInput.equalsIgnoreCase("bye")) done = true;
info = input.readLine();
System.out.println(info);
}
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}
}
public static void main(String args[]) {
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),8500);
}
else{
connection=new Socket(InetAddress.getByName(connectto),8500);
}
BufferedReader input=new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream(), true /* autoFlush */);
// read information from server
String info;
info = input.readLine();
System.out.println(info);
boolean done = false;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String sInput;
while(!done){
sInput = in.readLine();
out.println(sInput);
if (sInput.equalsIgnoreCase("bye")) done = true;
info = input.readLine();
System.out.println(info);
}
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}
}
|
java 类名 参数1 参数2
其中,参数1就是args[0],依次类推
其中,参数1就是args[0],依次类推