1

我正在尝试在 Netbeans 上运行 java 中的客户端-服务器程序。

这是服务器的代码:

// 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]); #line 48, error arises here. 
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

这是客户端的代码:

// File Name GreetingClient.java

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

public class GreetingClient
{
   public static void main(String [] args)
   {
      String serverName = args[0]; #line 10, error arises here 
      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();
      }
   }
}

所以当我想模拟客户端和服务器一起运行时,我首先运行服务器。服务器运行后,我需要运行客户端。

但是,当我尝试运行服务器文件时,出现以下错误:

运行:线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:GreetingServer.main 中的 0(GreetingServer.java:48)Java 结果:1 BUILD SUCCESSFUL(总时间:0 秒)

同样,当我尝试运行客户端程序时,我收到以下错误:

运行:线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:0 在 GreetingClient.main(GreetingClient.java:10) Java 结果:1 BUILD SUCCESSFUL(总时间:0 秒)

为什么会这样?请帮忙。

更新:

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

public class GreetingServer extends Thread
{
    private static int port;
   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)
   {
      port=9000;
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}


// File Name GreetingClient.java

import java.io.*;
import java.net.*;
public class GreetingClient
{
    private static String serverName;
   public static void main(String [] args)
   {
      String sName = "MyServerName";
      int port = 9000;
      try
      {
         System.out.println("Connecting to " + sName
                             + " on port " + port);
         Socket client = new Socket(sName, 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)
      {
      }
   }
}

现在服务器端没有错误。但我仍然在客户端收到以下错误:

运行:线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:0 在 GreetingClient.main(GreetingClient.java:10) Java 结果:1 BUILD SUCCESSFUL(总时间:0 秒)

4

2 回答 2

2

您的代码假设您传递了要用作参数的端口,因此:

  • 您可以使用 Patryk Roszczyniała 发布的行运行您的应用程序

  • 如果您不想使用它,只需删除以下行:

    String serverName = args[0]; 
    int port = Integer.parseInt(args[0]);
    

    并用硬编码值替换它们,例如:

    String serverName = "MyServerName";
    int port = 3000;
    

更新 :

我刚刚测试了你的代码,实际上 serverName 是你用来连接服务器的地址,所以"localhost"如果你在同一台机器上同时运行客户端和服务器,请将其替换为。

测试代码:

这是我测试的更新代码,如果您在同一台机器上运行它们应该可以完美运行。

客户 :

import java.io.*;
import java.net.*;
public class GreetingClient
{
    private static String serverName;
   public static void main(String [] args)
   {
      String sName = "localhost";
      int port = 9000;
      try
      {
         System.out.println("Connecting to " + sName
                             + " on port " + port);
         Socket client = new Socket(sName, 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)
      {
      }
   }
}

服务器 :

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

public class GreetingServer extends Thread
{
    private static int port;
   private ServerSocket serverSocket;

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

   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)
   {
      port=9000;
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}
于 2013-04-24T08:41:52.433 回答
1

要运行SocketServer,您需要传递将在哪个服务器上工作的端口。例如:

SocketServer server = new SocketServer(1234);

问题是您想通过 java 参数传递端口,而您没有这样做。首先,在使用任何变量之前,您应该首先验证它。

if (args.length>0) {
    int port = Integer.parseInt(args[0]); #line 48, error arises here. 
}

您的程序需要参数,但我认为您可能在没有参数的情况下运行程序。你应该以这种方式运行程序:java -jar app.jar 1234 但是你这样做是这样的:java -jar app.jar

在本文中,您将了解如何使用 netbeans http://netbeanside61.blogspot.com/2009/02/using-command-line-arguments-in.html将参数传递给 java 程序。

如果您是 Java 新手,您应该首先阅读本教程http://docs.oracle.com/javase/tutorial/networking/sockets/。这很有帮助。

于 2013-04-24T08:36:51.240 回答