当代码到达 InputStream.read()时,应用程序将变得无响应。
我认为可能是这样,因为客户端代码先于服务器代码运行,然后等待输入。
这是服务器代码 单击登录按钮后,大小写为“发送登录详细信息” (它将登录详细信息发送给客户端),如果ID和密码正确,则大小写变为**“登录成功” 。它正在** output.readUTF();
中发送正确的值public void doWork(String line) {
Connection conn = Main.getConnection();
switch (line) {
case "Send login details":
Main main = new Main();
String details = main.giveLoginDetails();
String id = " ";
String password = " ";
int i = 0;
while (details.charAt(i) != ' ') {
id = id + details.charAt(i);
i++;
}
try {
id = id.trim();
password = details.substring(i + 1);
System.out.println(id + " " + password);
output.writeUTF(id + " " + password);
}
catch (IOException e)
{
e.printStackTrace();
}break;
case "Login successful":
String getDet1 = "SELECT * FROM $t;";
if(su == 1)
getDet1 = getDet1.replace("$t", "Post1");
else
getDet1 = getDet1.replace("$t", "Post"+(su*10+1));
System.out.print(getDet1);
rs2 = stmt2.executeQuery(getDet1);
String d;
int dd;
for (int x = 0; x < ch; x++) {
rs2.next();
d = rs2.getString(2);
output.writeUTF(d);
System.out.println(d);
dd = rs2.getInt(3);
output.writeInt(dd);
System.out.println(dd);
d = rs2.getString(4);
output.writeUTF(d);
System.out.println(d);
d = rs2.getString(5);
// if(d == null) {
// output.writeUTF("null");
// System.out.println("null");
// }
// else
output.writeUTF(d);
// System.out.println(d+"not null ");}
}
}
catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
}
}
客户代码:-登录场景之后的另一个场景。 它是Initializable接口的Initialize方法。在这里,我正在读取输出,但是挂起了。
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
String posts = "";
int ch = 0;
Main m = new Main();
Stage stage = m.getStage();
stage.setResizable(true);
int c = 0;
try {
posts = Networking.input.readUTF();
ch = Networking.input.readInt();
}
catch (IOException e)
{
e.printStackTrace();
}
int rs = Integer.valueOf(posts);
for(int k = 0; k<rs; k++) {
c++;
}
try {
Networking.input.readUTF();
}
catch (IOException e)
{
e.printStackTrace();
}
String det = "";
int cl;
for (int i = 0; i < ch; i++) {
a[i].setDisable(false);
a[i].setOpacity(1);
try {
Networking.output.writeUTF("send voting details");
det = Networking.input.readUTF();
System.out.println(det);
cl = Networking.input.readInt();
System.out.println(cl);
det = Networking.input.readUTF();
System.out.println(det);
det = Networking.input.readUTF();
System.out.println(det);
}
catch (IOException exx)
{
exx.printStackTrace();
}
}
}
添加: 这是我的网络课程
package sample;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Networking {
public static ServerSocket serverSocket= null;
public static void connect(int port)
{
try
{
serverSocket = new ServerSocket(port);
System.out.print("Server is started");
while (true)
{
Socket socket = null;
try {
socket = serverSocket.accept();
DataInputStream input = new DataInputStream(socket.getInputStream());
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
Thread thread = new ServerThread(socket, input, output);
thread.start();
System.out.print("Client Accepted");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
}