gpt4 book ai didi

java - 读写套接字,Java

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:05 24 4
gpt4 key购买 nike

我在服务器/客户端 Java 应用程序中读取和写入套接字时遇到一点问题。服务器连接到数据库。我想将包含用户数据(姓名、姓氏、密码)的对象“员工”发送到服务器,然后服务器查找有关此用户的数据库并重新发送给客户端信息 - 正(1)或负(-1)。

首先,当我想发送一个对象 Employee 时,我有:“java.net.SocketException:软件导致连接中止:套接字写入错误”我关闭了防火墙。

其次,当我想通过 writeInt - readInt 方法发送和接收 int 进行测试时,我无法读取服务器上的任何内容。

有什么问题?请帮忙。

代码服务器:

class ClientCommunication implements Runnable {
private Socket incoming;

public ClientCommunication(Socket clientSocket) {
incoming = clientSocket;

}

public void run() {
try {
synchronized (this) {
try {
serverObjectOutput = new ObjectOutputStream(
incoming.getOutputStream());
serverObjectInput = new ObjectInputStream(
incoming.getInputStream());
} finally {
incoming.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}

synchronized(this) {
while (true) {
try{
int operation = serverObjectInput.readInt();

switch(operation) {
case 1:
Employee employee = (Employee) serverObjectInput.readObject();
String SelectUserDataSQL = "SELECT COUNT(*) AS COUNT FROM pracownik where Imie = ? AND Nazwisko = ? AND Haslo = ?";
PreparedStatement CheckEmployeeLogin;
CheckEmployeeLogin = conn.prepareStatement(SelectUserDataSQL);

CheckEmployeeLogin.setString(1, employee.getFirstName());
CheckEmployeeLogin.setString(2, employee.getLastName());
CheckEmployeeLogin.setString(3, new String(employee.getPassword()));

ResultSet resultSQL = CheckEmployeeLogin.executeQuery();
if (resultSQL.next())
if (resultSQL.getInt("COUNT") == 0)
serverObjectOutput.writeInt(1);
else serverObjectOutput.writeInt(-1);
break;
}
} catch(IOException | ClassNotFoundException | SQLException ex)
{
}
}
}
}
}

class ServerStart implements Runnable {
private int portNumber;

public ServerStart(int portNumber) {
this.portNumber = portNumber;
}

public void run() {


try {
conn = getConnection();
stat = conn.createStatement();

} catch (SQLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}


try {
serverSocket = new ServerSocket(portNumber);

} catch (IOException e) {
e.printStackTrace();
}


try {
while (true) {
Socket incoming = serverSocket.accept();

clientSockets.add(incoming);

Runnable r = new ClientCommunication(incoming);
Thread t = new Thread(r);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

代码客户端:

public void actionPerformed(ActionEvent e) {
if (isConnected == false) {
String ServerIP = ip.getText().trim();

int ServerPort = Integer
.parseInt(port.getText().trim());


try {
ClientSocket = new Socket(ServerIP, ServerPort);

clientObjectInput = new ObjectInputStream(
ClientSocket.getInputStream());
clientObjectOutput = new ObjectOutputStream(
ClientSocket.getOutputStream());

isConnected = true;
} catch (IOException ex) {
}
synchronized (this) {
try {
ClientLoginFrame login = new ClientLoginFrame();

Employee employee = login.getEmployee();

clientObjectOutput.writeObject(employee);
int result = clientObjectInput.readInt();

if(result == 1)
{
// DO SOMETHING
}
else {
ClientSocket.close();
}
} catch (IOException ex) {
ex.printStackTrace();

}
}
}
}
});
}

最佳答案

  1. 添加一个 ex.printStackTrace() 来查看您的

    捕获(IOException | ClassNotFoundException | SQLException ex)

  2. 服务器端,在您的 ClientCommunication 类上:您似乎在进入 while 循环之前关闭套接字。所以套接字已经关闭,无法发送/接收消息。您不应在此处调用 incoming.close(),而应在 run() 方法的末尾调用。

关于java - 读写套接字,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27662862/

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