Я разрабатываю систему электронного голосования с использованием java. Клиент отправляет номер, который представляет запрошенный метод. Метод run() серверного потока затем ожидает ввода из сокета в соответствии с отправленным номером. Сервер читает ввод обычно для первого запроса клиента, однако я получаю следующее исключение из сервера:readline() метод bufferedreader (socket.getinputstream) не ждет ввода от клиента
Исключение в потоке «Thread-0» java.lang.NumberFormatException: Для входной строки: «тест» на java.lang.NumberFormatException.forInputString (Unknown Source) на java.lang.Integer.parseInt (Unknown Source) в java.lang.Integer.parseInt (Unknown Source) в ServerThread.run (ServerThread.java:139)
Затем поток прерывается ненормально (я пришел к выводу, что, поскольку i попробуйте выполнить другую операцию из того же клиентского GUI, я получаю SocketException: Socket закрыт). Пользователь = Integer.parseInt (in.readLine()); должен ждать другого целого, которое будет отправлено клиентом, но его нет. Его чтение последней отправленной строки. (Im использованием BufferedReader)
здесь метод пробег serverthread:
public void run() {
while (true) {
int user = -1;
// while ((user = Integer.parseInt(in.readLine())) != -1) {
try {
user = Integer.parseInt(in.readLine());
} catch (IOException e) {
System.out.println("Command Error");
}
switch (user) {
case 1:
try {
String username = in.readLine();
String password = in.readLine();
int cid = -1;
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT UUID FROM user_acc WHERE UUID IN (SELECT CID FROM candidate) AND username = '"
+ username + "';");
if (Security.Authenticate(username, password) && !username.equals("") && !password.equals(""))
out.println("true");
else {
out.println("false");
// in.reset();
break;
}
if (rs.next()) {
cid = rs.getInt(1);
String inetAddress = clientSocket.getInetAddress().toString().substring(1);
stmt.executeUpdate(
"UPDATE candidate SET ipadd = '" + inetAddress + "' WHERE CID = " + cid + ";");
}
} catch (IOException e) {
System.out.println("Writing in Sign in Error");
} catch (UserNotExistingException e) {
} catch (SQLException e) {
e.printStackTrace();
}
break;
case 2:
try {
String username = in.readLine();
String name = in.readLine();
String password = in.readLine();
String email = in.readLine();
String conf = in.readLine();
if (Security.signUp(username, name, email, password, conf))
out.println("true");
else
out.println("false");
} catch (IOException e) {
System.out.println("Reading in Sign Up Error");
} catch (UsernameEmailExistsException e) {
System.out.println("Email Exists");
out.println("false");
}
break;
case 3:
try {
String email = in.readLine();
String code;
if (Security.forgotPass(email)) {
out.println("true");
code = in.readLine();
} else {
out.println("false");
break;
}
if (Security.checkCode(code, email))
out.println("true");
else {
out.println("false");
break;
}
String newpass = in.readLine();
String conf = in.readLine();
String username = "";
for (int i = 0; i < email.length(); i++)
if (email.charAt(i) == '@')
break;
else
username += email.charAt(i);
if (Security.forceChangePassword(username, newpass, conf)) {
out.println("true");
} else
out.println("false");
} catch (IOException e) {
System.out.println("Reading in Forget Pass Error");
}
break;
case 4:
try {
int CID = Integer.parseInt(in.readLine()), userID = Integer.parseInt(in.readLine());
try {
if (nominate(CID, userID))
out.println("true");
else
out.println("false");
} catch (ClassNotFoundException | SQLException | UserNotExistingException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
break;
case 5:
try {
int uid = Integer.parseInt(in.readLine());
String candname = in.readLine();
if (vote(candname, uid))
out.println("true");
else
out.println("false");
} catch (IOException e) {
e.printStackTrace();
}
break;
case 6:
break;
case 7:
try {
String text = in.readLine();
int UID = Integer.parseInt(in.readLine());
int CID = Integer.parseInt(in.readLine());
if (postComment(text, UID, CID))
out.println("true");
else
out.println("false");
} catch (IOException e) {
e.printStackTrace();
}
break;
case 10:
try {
String username = in.readLine();
String oldPass = in.readLine();
String newPass = in.readLine();
String conf = in.readLine();
if (Security.changePass(username, oldPass, newPass, conf))
out.println("true");
else
out.println("false");
} catch (IOException e) {
e.printStackTrace();
}
break;
case 11:
try {
int CID = Integer.parseInt(in.readLine());
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ipadd FROM candidate WHERE CID = " + CID + ";");
if (rs.next())
out.println(rs.getString(1));
int port = Integer.parseInt(in.readLine());
out.write(port);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
break;
}
}
}
Вот мои методы клиента (в случае проблема заключается в них):
public class Client {
private static Socket clientSocket;
private final int PORT_NUM = 4373;
private static DataOutputStream p2pout;
private String IP;
public Client(String IP) throws IOException {
this.IP = IP;
try {
clientSocket = new Socket(IP, PORT_NUM);
} catch (Exception e) {
System.err.println("Cannot connect to the server, try again later.");
System.exit(1);
}
}
public void sendCommand(int com) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(com + "");
out.close();
in.close();
}
public static boolean SignIn(String Username, String password) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(1);
out.println(Username);
out.println(password + "\n");
String reply = in.readLine();
out.close();
in.close();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean SignUp(String Username, String name, String email, String password, String conf)
throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(2);
out.println(Username);
out.println(name);
out.println(password);
out.println(email);
out.println(conf);
String reply = in.readLine();
out.close();
in.close();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean ForgetPass(String email) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(3);
out.println(email);
String reply = in.readLine();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean ForgetPass2(String code) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(3);
out.println(code);
String reply = in.readLine();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean ForgetPass3(String newpass, String conf) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(newpass);
out.println(conf);
String reply = in.readLine();
out.close();
in.close();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean nominate(int CID, int UserID) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(4);
out.println(CID + "");
out.println(UserID + "");
String reply = in.readLine();
out.close();
in.close();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean vote(int cid, int UID) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(5);
out.println(cid);
out.println("" + UID);
String reply = in.readLine();
out.close();
in.close();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean postcomment(int CID, String text, int UID) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(7);
out.println(text + "");
out.println(UID + "");
out.println(CID + "");
String reply = in.readLine();
out.close();
in.close();
if (reply.equals("true"))
return true;
else
return false;
}
public static boolean Changepass(String Username, String oldpass, String newpass, String conf) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(10);
out.println(Username);
out.println(oldpass);
out.println(newpass);
out.println(conf);
String reply = in.readLine();
out.close();
in.close();
if (reply.equals("true"))
return true;
else
return false;
}
public String getIPAddress(int cid) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(11);
out.println(cid + "");
String inetAddress = in.readLine();
out.close();
in.close();
return inetAddress;
}
public int getServerChatPort() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
int port = Integer.parseInt(in.readLine());
out.close();
in.close();
return port;
}
public static void connectionListener(String inetadd) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
ServerSocket ss = new ServerSocket(0);
Socket clientSocket = ss.accept();
out.println(ss.getLocalPort() + "");
Socket candSock = ss.accept();
ChatListener cl = new ChatListener(candSock);
cl.start();
out.close();
in.close();
while (true) {
if (ChatListener.flag == 1)
cl.display();
// ^^ Displayed in GUI
}
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket chatConnect(String inetAdd) {
Socket soc = null;
try {
int port = getServerChatPort();
soc = new Socket(inetAdd, port);
} catch (IOException e) {
e.printStackTrace();
}
return soc;
}
public void send(String m) {
try {
p2pout.writeChars(m);
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() throws IOException {
clientSocket.close();
}
}
Проблема здесь - недопустимый ввод или неправильная обработка ввода. Ничто не может быть связано с «не ожиданием ввода от клиента». – EJP