我在netbeans中创建一个java客户端/服务器套接字程序,它应该接受一个数字,然后返回平方根/平方数。 它允许我输入一个数字,然后我得到以下声明: 客户端中发现异常:java.util.NoSuchElementException。
我不确定为什么会这样做,请帮忙!
这是我的两个类的代码:
客户端:
package question1.clientserver;
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
try {
int number, temp;
Scanner sc = new Scanner(System.in);
Socket s = new Socket("127.0.0.1", 1452);
Scanner sc1 = new Scanner(s.getInputStream());
System.out.println("Enter any number to be squared");
number = sc.nextInt();
PrintStream p = new PrintStream(s.getOutputStream());
p.println(number);
temp = sc1.nextInt();
System.out.println(temp);
} catch (Exception e) {
System.out.println("Exception found in Client: " + e);
}
}
}
这是我的服务器代码
package question1.clientserver;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) {
while (true) {
System.out.println("Waiting for connection, please stand by...");
try {
int number, temp;
ServerSocket s1 = new ServerSocket(1452);
Socket ss = s1.accept();
System.out.println("Connection has been established");
Scanner sc = new Scanner(ss.getInputStream());
number = sc.nextInt();
temp = number * number;
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
} catch (Exception e) {
System.out.println("Exception in Server while creating connection" + e);
}
}
}
}
}
如果您有任何想法或建议,请告诉我们!
栈跟踪
run: Enter any number to be squared
25 (which is what i type in for example)
Exception found in Client: java.util.NoSuchElementException
Build Successful (total time: 31 seconds)