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

📄 serverthread.java

📁 程序测试说明
💻 JAVA
字号:
package test;

import java.io.*;
import java.net.*;
public class ServerThread extends Thread {

	Socket clientCs; // 客户端连接通信套接字 
	     DataInputStream input; // 定义输入流 
	     DataOutputStream output; // 定义输出流 
	    
	     public ServerThread(Socket s) { // serverThread构造方法 
	       clientCs = s; // 接收receiveServer传来的套接字 
	    
	       try { // 初始化输入、输出流 
	         input = new DataInputStream(s.getInputStream()); 
	         output = new DataOutputStream(s.getOutputStream()); 
	       } 
	       catch (IOException ex) { 
	         System.out.println(ex.getMessage()); 
	       } 
	    
	       System.out.println("Client connect! " + s.getInetAddress().getHostAddress() + 
	                          ":" + s.getPort()); // 显示客户机信息(IP地址、端口号) 
	       try { 
	         output.writeUTF("Welcome to the server! "); // 发送信息给客户机 
	       } 
	       catch (IOException ex) { 
	       } 
	     } 
	    
	     public void run() { // 线程执行方法 
	       String command = null; // 客户请求 
	       String str = null; 
	       while (true) { 
	         try { 
	           str = input.readUTF(); // 接收客户端请求 
	           System.out.println(str);
	         } 
	         catch (IOException ex) { 
	           System.out.println("Client closed!  " + 
	                              clientCs.getInetAddress().getHostAddress() + 
	                              ":" + clientCs.getPort()); // 显示客户机信息(IP地址、端口号) 
	           try { 
	             input.close(); // 关闭输入流 
	             output.close(); // 关闭输出流 
	             clientCs.close(); // 关闭套接字 
	             return; 
	           } 
	           catch (IOException ex1) {} 
	         } 
	         command = str.trim().toUpperCase(); 
	         // 查询本服务器可接受的请求 
	         if (command.equals("HELP")) { // 请求help 
	          try { 
	            output.writeUTF(" query, help ..."); 
	          } 
	          catch (IOException ex) {} 
	        } 
	        else 
	        if (command.startsWith("QUERY")) { // 请求query 
	          try { 
	            output.writeUTF("OK to query something!"); 
	          } 
	          catch (IOException ex) {} 
	        } 
	        // else if( … ){ …… }   // 在此可加入服务器的其他指令 
	        else { 
	          try { 
	            //output.writeUTF("Command not Found! Please refer to the HELP!"); 
	        	  output.writeUTF("you want add user");
	          } 
	          catch (IOException ex) {} 
	        } 
	      } // end of while 
	   
	    }
}

⌨️ 快捷键说明

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