【问题标题】:How to compile this network socket example in Java using Eclipse?如何使用 Eclipse 在 Java 中编译这个网络套接字示例?
【发布时间】:2014-12-04 06:57:00
【问题描述】:

好的,我在书中找到了这个例子,所以我知道这段代码没有错误。下面的程序包含两个类,它们都是主类。一个用于客户端,一个用于服务器。

根据这本书,我应该像这样编译它们:

编译客户端和服务端,然后启动服务端如下:

$ java GreetingServer 6066 正在端口 6066 上等待客户端...

检查客户端程序如下:

$ java GreetingClient 本地主机 6066 在端口 6066 上连接到 localhost 刚刚连接到 localhost/127.0.0.1:6066 服务器说谢谢你连接到/127.0.0.1:6066 再见!

我希望能够在 Eclipse 上运行它们,但每次我这样做时,都会出现以下错误: 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:0 在 GreetingServer.main(GreetingServer.java:47)。

如何在 ECLIPSE 中运行这个程序?谢谢。

// 文件名 GreetingClient.java

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

public class GreetingClient
{
   public static void main(String [] args)
   {
      String serverName = args[0];
      int port = Integer.parseInt(args[1]);
      try
      {
         System.out.println("Connecting to " + serverName
                             + " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("Just connected to "
                      + client.getRemoteSocketAddress());
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out =
                       new DataOutputStream(outToServer);

         out.writeUTF("Hello from "
                      + client.getLocalSocketAddress());
         InputStream inFromServer = client.getInputStream();
         DataInputStream in =
                        new DataInputStream(inFromServer);
         System.out.println("Server says " + in.readUTF());
         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}


// File Name GreetingServer.java

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

public class GreetingServer extends Thread
{
   private ServerSocket serverSocket;

   public GreetingServer(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(10000);
   }

   public void run()
   {
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " +
            serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to "
                  + server.getRemoteSocketAddress());
            DataInputStream in =
                  new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out =
                 new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
              + server.getLocalSocketAddress() + "\nGoodbye!");
            server.close();
         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      int port = Integer.parseInt(args[0]);
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

【问题讨论】:

    标签: java eclipse sockets


    【解决方案1】:

    查看这些行

    public static void main(String [] args)
    {
      String serverName = args[0]; //<-- Expecting a value
      int port = Integer.parseInt(args[1]); //<-- Expecting a value
    

    args[] 是一个字符串数组,其中包含您通过命令行传递的参数。当您尝试直接从 eclipse 运行它时,您没有指定这些值,这会导致 args[] 为空数组,因此 args[0] 会给出 ArrayIndexOutOfBoundsException

    要解决这个问题,可以在 eclipse 中创建一个运行配置(见截图)

    并指定您希望 eclipse 在运行此类时通过的参数。您可以通过right-clicking the project, select run, then run-configurations --&gt; double click on java_application 执行此操作并传递您想要的信息。在指定参数时可能需要指定主类名,以便eclipse可以识别将args传递给哪个主类。

    或者您可以直接在类本身中硬编码这些值(用于测试)

    【讨论】:

      【解决方案2】:

      您需要创建一个运行配置并在那里传递参数。在eclipse中运行程序。让它失败。这会自动创建运行配置。编辑配置以添加参数。或者创建一个新的运行配置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-26
        • 2016-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 2013-03-18
        • 1970-01-01
        相关资源
        最近更新 更多