我正在尝试在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();
}
}
}
因此,当我想模拟客户端和服务器一起运行时,我首先运行服务器。 服务器运行后,我需要运行客户端。
但是,当我尝试运行服务器文件时,出现以下错误:
运行: 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0 在GreetingServer.main(GreetingServer.java:48) Java结果:1 建立成功(总时间:0秒)
同样,当我尝试运行客户端程序时,出现以下错误:
运行: 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0 在GreetingClient.main(GreetingClient.java:10) Java结果:1 建立成功(总时间: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)
{
}
}
}
现在服务器端没有错误。但我仍然在客户端收到以下错误:
运行: 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0 在GreetingClient.main(GreetingClient.java:10) Java结果:1 建立成功(总时间:0秒)
答案 0 :(得分:1)
要运行SocketServer
,您需要在哪个服务器上运行端口。例如:
SocketServer server = new SocketServer(1234);
问题是你想通过java参数传递端口而你没有这样做。首先,在使用任何变量之前,您应首先验证它。
if (args.length>0) {
int port = Integer.parseInt(args[0]); #line 48, error arises here.
}
你的程序需要参数,但我认为你可能没有params运行你的程序。
您应该以这种方式运行程序: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/。这非常有帮助。
答案 1 :(得分:1)
您的代码假设您将要用作参数的端口传递给:
您使用PatrykRoszczyniał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();
}
}
}