在客户端 - 服务器编程中出错

时间:2017-02-06 04:54:54

标签: java sockets networking distributed-computing

我在java中创建了客户端服务器程序。当我运行程序时,我应该获得端口号和IP地址,但是在运行Client.java时出现错误。以下是我的两个文件。

Server.java

package serverpro;


import java.io.*;
import static java.lang.ProcessBuilder.Redirect.to;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Server extends Thread  {

public static final int PORT_NUMBER = 12345;
protected Socket socket;

public static void main(String[] args) {
  ServerSocket server = null;
  try {
     server = new ServerSocket(PORT_NUMBER);
     while (true) {
        new Server(server.accept());
     }
  }
  catch(IOException ex) {
     System.out.println("Unable to start server or acccept connections ");
     System.exit(1);
  }
  finally {
     try {
        server.close();
     }
     catch(IOException ex) {
        // not much can be done: log the error
        // exits since this is the end of main
     }
  }
 }

 private Server(Socket socket) {
  this.socket = socket;
  start();
}

 // the server services client requests in the run method
  public void run() {
  InputStream in = null;
  OutputStream out = null;

    BufferedReader inReader = new BufferedReader(newInputStreamReader(in));
  // the constructor argument “true” enables auto-flushing
  PrintWriter outWriter = new PrintWriter(out, true);
  outWriter.println("Echo server: enter bye to exit.");

  //outWriter.println(“Echo server: enter ‘bye’ to exit.”);
  while (true) {
  // readLine blocks until a line-terminated string is available
  String inLine;
      try {
          inLine = inReader.readLine();
      } catch (IOException ex) {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
      }
  // readLine returns null if the client just presses <return>

  try {
     in = socket.getInputStream();
     out = socket.getOutputStream();
     // ... do useful stuff ...
  }
  catch(IOException ex) {
     System.out.println("Unable to get Stream from ");
  }
  finally {
     try {
        in.close();
        out.close();
        socket.close();
     }
     catch(IOException ex) {
        // not much can be done: log the error
     }
     }
     }
  }
 }

Client.java

package serverpro;

import java.io.*;
import java.net.*;

public class Client {
 public static void main(String args[]) throws IOException {
  new Client(args[0]);
 }

  public Client(String host) throws IOException {
  Socket socket;
  try {
    socket = new Socket(host, Server.PORT_NUMBER);
  }
  catch(UnknownHostException ex) {
     System.out.println(host + " is not a valid host name.");
     return;
  }
  catch(IOException ex) {
     System.out.println("Error connecting with" + host);
     return;
  }
  // … initialize model, GUI, etc. ...
  InputStream in = null;
  OutputStream out = null;
  try {
     in = socket.getInputStream();
     out = socket.getOutputStream();
     // ... do useful stuff ...
  }
  finally {
     try {
        in.close();
        out.close();
        socket.close();
     }
     catch(IOException ex) {
        // not much can be done ...
     }
  } 
 }

}

这是我在运行client.java文件时得到的错误代码

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at serverpro.Client.main(Client.java:13)
/Users/Puja Dudhat/Library/Caches/NetBeans/8.2/executor- snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

3 个答案:

答案 0 :(得分:0)

假设您没有传递必需的命令行参数。当我运行此代码时,它确实运行正常,前提是所需的参数被传递或硬编码;即:

public static void main(**String args[]**) throws IOException {
    new Client(**args[0]**);
}

答案 1 :(得分:0)

您的代码需要将一个参数传递给main方法,该方法似乎是您的客户端端口,存储在args[0]。因此,您必须为main方法提供一个。设置port = 12345的示例:

java Server 12345

如果您需要更多参数(例如args[1]处的值),则在启动main时只需添加另一个参数:

java Server 12345 secondArg

答案 2 :(得分:0)

如果您在同一台计算机上同时运行服务器和客户端,则可以将 localhost 作为命令行参数传递

java Client localhost

或者,你可以硬编码主机值(注意:这不是好习惯),

public static void main(String args[]) throws IOException {
    new Client("localhost");
}

另外作为建议,你可以使用像eclipse或intellij这样的ide来逐步调试你的代码。你可以浏览java的在线视频教程,很多都可以在youtube上找到