⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 连接测试系统
💻 JAVA
字号:
package com.cnu.cie.olts.server;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;


//实现 Client线程类
public class Client extends Thread                              
  {
    Socket socket;//用来存储一个连接客户的socket信息
	String name="";//用来存储客户的连接姓名
	String password="";//用来存放客户的密码
	String status="";//用来存储客户的身份信息
	BufferedReader bufreader; //用来实现接受从客户端发来的数据流
	PrintStream ps;//用来实现向客户端发送信息的打印流
    //实现想客户端发送信息的方法
	public void send(StringBuffer msg)                   
	{
	   ps.println(msg);//用打印流发送信息
	   ps.flush();
	}
    //Client线程类的构造器
	public  Client(Socket s)                             
	  {
	   socket=s;                                           
	   try
		  {  
		   //存储特定客户socket的输入流接受s这个客户发送到服务器端的信息	
		   bufreader=new BufferedReader(new InputStreamReader(s.getInputStream()));
	       //存储特定客户socket的输出流发送服务器给s这个客户的信息  
		   ps=new PrintStream(s.getOutputStream());
		   //读取接受来的信息		  
		   String info=bufreader.readLine();
		   //用StringTokenizer类来读取用":"分段字符
		   if(info!=null){
			   StringTokenizer stinfo=new StringTokenizer(info,":"); 
			   //head用来存储类似于关键字的头信息
			   String head=stinfo.nextToken();
	           if(stinfo.hasMoreTokens())
	           //关键字后的第二段数据是客户名信息		   
	        	   name=stinfo.nextToken();
	           if(stinfo.hasMoreTokens())
			   //关键字后的第三段数据是客户password信息
				   password=stinfo.nextToken();
			   if(stinfo.hasMoreTokens())
			   //关键字后的第三段数据是客户state信息
				   status=stinfo.nextToken();
			   //在控制台打印头信息
			   System.out.println(info);
		   }
	   }
	   catch(IOException e)
		  {
	       System.out.println("Error:"+e);
		   Server.disconnect(this);
		   this.stop();
		   this.destroy();
	   }
	}
    //线程运行方法   
	public void run()                                           
	  {
	   while(true)
		  {
	       String line=null;
		   try
			  {
		       //读取客户端发来的数据流	  	
			   line=bufreader.readLine();
		   }
		   catch(IOException e)
		   {
		       System.out.println("Error:"+e);
		       if(socket!=null&&!socket.isClosed())
		    	   Server.WriteToLog("Error:"+e+"\n");
			   Server.disconnect(this);
			   this.stop();
			   this.destroy();
			   return;
		   }
		   if(line==null)//客户已离开
			  {
			   Server.disconnect(this);
			   this.stop();
			   this.destroy();
			   return;
			  }		   
		   StringTokenizer st=new StringTokenizer(line,":");
		   String keyword=st.nextToken();
		   //如果关键字是ITEMTYPE则是客户端发来的获取试题类型指令
		   if(keyword.equals("ITEMTYPE"))
		   {
			   String sql="select distinct itemtype from itemtype_info";
			   StringBuffer returnvalue=new StringBuffer("ITEMTYPE");
			  try {  
				ConnectionOLTS connect=new ConnectionOLTS();
				Connection con=connect.getConnection();
				Statement stmt=connect.getStatement();
				ResultSet   result=stmt.executeQuery(sql);
				while(result.next()){
					returnvalue.append("#_#"+result.getString("itemtype"));
					}
				ConnectionOLTS.closeConn(con);
				ConnectionOLTS.closeStatement(stmt);
				ConnectionOLTS.closeResultSet(result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Server.WriteToLog("ERR:"+e.getMessage());
					e.printStackTrace();
				}
				send(returnvalue);
		   }
		 //如果关键字是SUBJECT则是客户端发来获取科目信息的指令
		   else if(keyword.equals("SUBJECT"))
		   {		       
			   String sql="select distinct subject from item_info";
			   StringBuffer returnvalue=new StringBuffer("SUBJECT");
			  try {  
				ConnectionOLTS connect=new ConnectionOLTS();
				Connection con=connect.getConnection();
				Statement stmt=connect.getStatement();
				ResultSet   result=stmt.executeQuery(sql);
				while(result.next()){
					returnvalue.append("#_#"+result.getString("subject"));
					}
				ConnectionOLTS.closeConn(con);
				ConnectionOLTS.closeStatement(stmt);
				ConnectionOLTS.closeResultSet(result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Server.WriteToLog("ERR:"+e.getMessage());
					e.printStackTrace();
				}
				send(returnvalue);
		   }
		 //如果关键字是GETQA则是客户端获取试题的信息
		   else if(keyword.equals("GETQA"))
		   {		       
			   String sql=st.nextToken("\0");
			   sql=sql.substring(1);
			   System.out.println(sql);
			   StringBuffer returnvalue=new StringBuffer("QA");
			   String itemtype,content,answer,score,limittime,subject,difficulty;
			   int itemid;
			   try {  
					ConnectionOLTS connect=new ConnectionOLTS();
					Connection con=connect.getConnection();
					Statement stmt=connect.getStatement();
					ResultSet   result=stmt.executeQuery(sql);
					while(result.next()){
						itemid=result.getInt("id");
						itemtype=result.getString("itemtype");
						content=result.getString("content");
						answer=result.getString("answer");
						score=result.getString("score");
						limittime=result.getString("limittime");
						subject=result.getString("subject");
						difficulty=result.getString("difficulty");
						returnvalue.append("@_@"+itemid+"&_&"+itemtype+"&_&"+content+"&_&"+answer+"&_&"+score+"&_&"+limittime+"&_&"+subject+"&_&"+difficulty);
						}
					ConnectionOLTS.closeConn(con);
					ConnectionOLTS.closeStatement(stmt);
					ConnectionOLTS.closeResultSet(result);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Server.WriteToLog("ERR:"+e.getMessage());
						e.printStackTrace();
					}
					System.out.println("QA----"+returnvalue);
					send(returnvalue);
		   }
		 //如果关键字是ADDNEWITEM则是客户端发来添加新试题的指令
		   else if(keyword.equals("ADDNEWITEM"))
		   {		       
			   //取出sql语句	
			   String sql=st.nextToken("\0");
			   sql=sql.substring(1);
			   System.out.println(sql);
			   try {  
					ConnectionOLTS connect=new ConnectionOLTS();
					Connection con=connect.getConnection();
					Statement stmt=connect.getStatement();
					stmt.executeUpdate(sql);
					ConnectionOLTS.closeConn(con);
					ConnectionOLTS.closeStatement(stmt);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Server.WriteToLog("ERR:"+e.getMessage());
						e.printStackTrace();
					}
		   }
		 //如果关键字是UPDATEITEM则是客户端发来试题更新的指令
		   else if(keyword.equals("UPDATEITEM"))
		   {		       
			   //取出sql语句	
			   String sql=st.nextToken("\0");
			   sql=sql.substring(1);
			   System.out.println(sql);
			   try {  
					ConnectionOLTS connect=new ConnectionOLTS();
					Connection con=connect.getConnection();
					Statement stmt=connect.getStatement();
					stmt.executeUpdate(sql);
					ConnectionOLTS.closeConn(con);
					ConnectionOLTS.closeStatement(stmt);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Server.WriteToLog("ERR:"+e.getMessage());
						e.printStackTrace();
					}
		   }
		 //如果关键字是GETUSERINFO则是客户端获取用户的信息指令
		   else if(keyword.equals("GETUSERINFO"))
		   {		       
			   String sql="select * from user_info";
			   StringBuffer returnvalue=new StringBuffer("USERINFO");
			   String usernamestr,passwordstr,statusstr;
			   int userid;
			   try {  
					ConnectionOLTS connect=new ConnectionOLTS();
					Connection con=connect.getConnection();
					Statement stmt=connect.getStatement();
					ResultSet   result=stmt.executeQuery(sql);
					while(result.next()){
						userid=result.getInt("id");
						usernamestr=result.getString("username");
						passwordstr=result.getString("password");
						statusstr=result.getString("status");
						returnvalue.append("@_@"+userid+"&_&"+usernamestr+"&_&"+passwordstr+"&_&"+statusstr);
						}
					ConnectionOLTS.closeConn(con);
					ConnectionOLTS.closeStatement(stmt);
					ConnectionOLTS.closeResultSet(result);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Server.WriteToLog("ERR:"+e.getMessage());
						e.printStackTrace();
					}
					System.out.println("USERINFO----"+returnvalue);
					send(returnvalue);
		   }
		 //如果关键字是ADDNEWUSER则是客户端发来添加新用户的指令
		   else if(keyword.equals("ADDNEWUSER"))
		   {		       
			   //取出sql语句	
			   String sql=st.nextToken("\0");
			   sql=sql.substring(1);
			   System.out.println(sql);
			   try {  
					ConnectionOLTS connect=new ConnectionOLTS();
					Connection con=connect.getConnection();
					Statement stmt=connect.getStatement();
					stmt.executeUpdate(sql);
					ConnectionOLTS.closeConn(con);
					ConnectionOLTS.closeStatement(stmt);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Server.WriteToLog("ERR:"+e.getMessage());
						e.printStackTrace();
						send(new StringBuffer("ADDUSERFAIL"));
					}
					send(new StringBuffer("ADDUSERSUCC"));
		   }
		 //如果关键字是UPDATEUSERINFO则是客户端发来用户信息更新的指令
		   else if(keyword.equals("UPDATEUSERINFO"))
		   {		       
			   //取出sql语句	
			   String sql=st.nextToken("\0");
			   sql=sql.substring(1);
			   System.out.println(sql);
			   try {  
					ConnectionOLTS connect=new ConnectionOLTS();
					Connection con=connect.getConnection();
					Statement stmt=connect.getStatement();
					stmt.executeUpdate(sql);
					ConnectionOLTS.closeConn(con);
					ConnectionOLTS.closeStatement(stmt);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Server.WriteToLog("ERR:"+e.getMessage());
						e.printStackTrace();
						send(new StringBuffer("UPDATEUSERFAIL"));
					}
			   send(new StringBuffer("UPDATEUSERSUCC"));
		   }
		   //如果关键字是QUIT则是客户端发来断开连接的信息
		   else if(keyword.equals("QUIT"))
		   {		       
			   //服务器断开与这个客户的连接	
		       Server.disconnect(this);
			   this.stop(); 
			   this.destroy();
		   }
	   }
	}
  } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -