gpt4 book ai didi

java - 客户端-服务器编程中出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:39 25 4
gpt4 key购买 nike

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

服务器.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
}
}
}
}
}

客户端.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)

最佳答案

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

java Server 12345

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

java Server 12345 secondArg

关于java - 客户端-服务器编程中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42060536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com