handler.out.flush
クライアントにサービスを提供するためにスレッドを使用していますが、 (サーバークラスで)コマンドを削除すると、スレッドがハングします。私はグーグルで検索しようとしましたが、それでも助けにはなりません。問題は通信ソケットにあると思いますが、それでも正しい解決策はありません。
簡単なログインと登録機能を作りたいので、2つのクラスを使用Server
しMysqlConn
ました。クラスはServer
、ソケットを介してクライアントから着信データ(ユーザープロファイル-ユーザー名、パスワードなど)を受信します。MysqlConn
受信後、データはクラスに送信されます。クラスの機能はMysqlConn
、データをチェックし、それらを照合するために私のsqlデータベースにアクセスすることです。データとデータベースが一致する場合、ログインプロセスは成功します。
クライアントから送信されるデータの形式は次のとおりです。
"login."+"name."+ "password." +"\n";
Server
クラスの内容は次のとおりです。
public class Server {
public static void main(String[] args)throws IOException, InstantiationException,
IllegalAccessException {
ServerSocket servsocket = null;
Socket sock = null;
try {
servsocket = new ServerSocket(28000);
while(true){
sock = servsocket.accept();
System.out.println(servsocket.isBound());
ChatThread thread = new ChatThread(sock);
String portnum = Integer.toString(sock.getPort());
thread.run(portnum);
}
} catch (IOException ioe) {
}
finally{
try {
servsocket.close();
} catch (IOException ioe) {
}
}
}
}
class ChatThread extends Thread{
static Vector<ChatThread> chatthread = new Vector<ChatThread>(10);
private BufferedReader in;
private PrintWriter out;
public ChatThread (Socket socket) throws IOException {
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(
new OutputStreamWriter(socket.getOutputStream())); }
public void run(String portnum){
String line;
synchronized(chatthread) {
chatthread.addElement(this); }
try {
line = in.readLine()+portnum;
String[] teksmasuk = line.split("\\.");
for(int i = 0; i < chatthread.size(); i++) {
synchronized(chatthread) {
ChatThread handler =
(ChatThread)chatthread.elementAt(i);
handler.out.println(line + "\r");
handler.out.flush();
if
(teksmasuk[0].contentEquals("reg")||teksmasuk[0].contentEquals("login")
||teksmasuk[0].contentEquals("logout")) {
if(teksmasuk[0].contentEquals("reg")){
}
else
if(teksmasuk[0].contentEquals("login")){
}
MysqlConn sqlcon = new MysqlConn();
String hasil = sqlcon.register(line);
}
else{
}
}
}
} catch(IOException ioe) {
ioe.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
synchronized(chatthread) {
chatthread.removeElement(this);
}
}
}
}
MysqlConn
クラス:
public class MysqlConn{
String dbn = "chat_db";
String URL = "jdbc:mysql://localhost/"+dbn ;
String usr = "root";
String pwd = "";
private String result;
boolean checkname = false;
boolean checkemail = false;
boolean checkpass = false;
private Connection con = null;
private String dbnama;
private String dbpass;
public String register(String line) throws InstantiationException,
IllegalAccessException, IOException, ClassNotFoundException{
String[] messagein =
line.split("\\.");
MysqlConn regs = new MysqlConn();
regs.login(messagein);
return result;
}
public void login (String[] messagein) throws InstantiationException,
IllegalAccessException{
if(messagein[0].contentEquals("login")) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(URL,usr,pwd);
Statement statement =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rslset = statement.executeQuery("select * from user");
int rs = statement.executeUpdate("update user set port="+
"'"+messagein[3] +"'" + "where nama = "
+ "'" + messagein[1] + "'" + "and password = " + "'"
+messagein[2] +"'" );
MysqlConn regs = new MysqlConn();
regs.check_status_login(messagein);
} catch (ClassNotFoundException e) {
System.out.println("Error #1:" + e.getMessage());
System.exit(0);
} catch(SQLException e){
System.out.println("Error #2:" + e.getMessage());
System.exit(0);
}
}
}
public void check_status_login (String[] messagein) throws InstantiationException,
IllegalAccessException, ClassNotFoundException{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(URL,usr,pwd);
Statement statement =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rslset = statement.executeQuery("select * from user");
while(rslset.next()) {
String dbname = rslset.getString("nama");
String dbpass = rslset.getString("password");
if((messagein[1].contentEquals(dbnama))){
+ messagein[1]+ "\r" + "Password from database: "+dbpass + "\r" +
"Password from client: "+ messagein[2]+ "\n");
checknama = true;
}
else if (messagein[2].contentEquals(dbpass)){
checkpass = true;
}
}
} catch (SQLException e1) {
+ e1);
}
if (!checknama){
hasil = "gagal";
}
else if (!checkpass)
{
hasil = "gagal";
}
else {
hasil = "login sukses";}
}
}