gpt4 book ai didi

java - 客户端/服务器应用程序中的线程

转载 作者:行者123 更新时间:2023-12-01 11:56:35 25 4
gpt4 key购买 nike

我制作了客户端服务器应用程序,它在每个新线程中加入新客户端。我想将信息发送到服务器,例如在拍卖系统中竞价。如何检查客户端类中的线程是否与服务器类中的线程相同?如果不是,我如何检查服务器端的哪个线程是客户端?

package com.multi;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.driver.OracleDriver;

public class Server {

public static final int PORT = 5545;
public static int I = 1;
public static String[] users = null;
private static final String URL = "jdbc:oracle:thin:@localhost:1521:XE";
Connection conn = null;
Statement stmt = null;
private static final String DB_USER = "DBDEMO";
private static final String DB_PASSWORD = "******";
private ResultSet res;
private String sql = null;
public void startDataBase() {
try {
DriverManager.registerDriver(new OracleDriver());
conn = DriverManager.getConnection(URL, DB_USER, DB_PASSWORD);
System.out.println("conn done");
conn.setAutoCommit(false);
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
} catch (SQLException e) {
e.printStackTrace();
}

}

public void runQuery(){
try {
stmt = conn.createStatement();
res = stmt.executeQuery("select * from ITEAMS");
System.out.println("query works");
while(res.next()){
System.out.println(res.getString("DESCRIBE"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void startServer() {
try {
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server is waiting for connetions...");
startDataBase();
runQuery();
while (true) {

Socket socket = server.accept();
if (socket.isBound()) {

System.out.println("user" + I + " connected");
++I;
}
new ServerThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new Server().startServer();
}

}

//服务器线程

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerThread extends Thread {
Socket socket;
String message = null;


ServerThread(Socket socket) {
this.socket = socket;
}
public void run(){
try {

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((message = bufferedReader.readLine()) != null){
System.out.println(message);
}
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

//客户端

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Client extends JFrame{

/**
*
*/
private static final long serialVersionUID = 1L;
private static String[] names = new String[]{ "Sara", "Jessica", "Tom", "Emanuel", "Joshua",
"Kristin", "Kirito", "Goku", "Bulma" };
private JTextField chatWindow;
private static int c = 0;

public void clientJoin(String name) {


try {
chatWindow = new JTextField();
add(new JScrollPane(chatWindow));
setSize(300, 150);
setVisible(true);
Socket socket = new Socket("localhost", 5545);
chatWindow.setText(name);
PrintWriter printWriter = new PrintWriter(
socket.getOutputStream(), true);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
// System.out.println(name);

// System.out.println(name);
// while (true) {
// String readerInput = bufferedReader.readLine();
for(int i = 0 ; i < 3;i++){
printWriter.print(name+":" );
printWriter.println(c+=5+10*Math.random());
}
// }

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}

public static void main(String[] args) {
for(String name : names) {
Runnable r = new Runnable() {
@Override
public void run() {
new Client().clientJoin(name);
}
};
new Thread(r).start();
// System.out.println("ges here");
c++;
}

}

}

最佳答案

How Can I check if thread in client class is the same as in server class?

除非 ClientServer 类运行在同一个 JVM 中(即在同一台机器上和同一进程中),否则它们不能 共享线程。它们不能相同。

此外,由于客户端和服务器线程是不同类的实例:分别是ThreadServerThread。这意味着即使 ClientServer 在同一个 JVM 中实例化,各自的线程仍然不可能相同。

(两个不同类的对象不能相同。尤其是线程,因为线程相等性是由对象标识决定的。)

<小时/>

如您所见,如果您按字面解释,这个问题就没有意义。如果您实际上想问一些不同的问题...您将需要澄清问题。

关于java - 客户端/服务器应用程序中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28405802/

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