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

📄 doservice.java

📁 Java开发的实现TCP/IP协议的服务器端程序。极具扩展性。可供初学者使用。希望对他们有用。
💻 JAVA
字号:
/*
 * Copyright (c) 2008 By Zibo Magnifico.  All rights reserved.
 * This code is one resource of Zibo Magnifico Electronic Commerce Recrut Co. Ltd.,
 * Author: Fengwen Pan
 * Date  : 2008.12.10
 */
package com.MPServer;

import java.io.*;
import java.net.*;
import java.sql.*;

public class doService extends Thread {
	private Socket toClient;
	private MPServer srv;

	/**
	 * The doService constructor creates a thread for itself.
	 */
	public doService(Socket acpSocket, MPServer mpsrv) throws IOException {
		this.toClient = acpSocket;
		this.srv = mpsrv;
	}

	// the run()
	public void run() {
		try {
			if (srv.opConnNum(0) >= MPServer.MAXCONNECTIONS) {
				// 超过最大连接数,通知客户端!
				doRefused();
				toClient.close(); // close the connection to the rejected
				// client.

				// And log it, of course
				srv.log("Connection refused to "
						+ toClient.getInetAddress().getHostAddress() + ":"
						+ toClient.getPort() + ": max connections reached.");
			} else {
				// Log this new connection
				srv.log("Connected to "
						+ toClient.getInetAddress().getHostAddress() + ":"
						+ toClient.getPort() + " on port "
						+ toClient.getLocalPort());

				// do MY business
				srv.opConnNum(1); // +1
				doNewBiz();
				toClient.close();
				srv.opConnNum(-1); // -1
			}
		} catch (IOException e) {
			srv.log(e);
		}
	}

	public void doRefused() {
		// Log it!
		srv.log("===========================");
		srv.log(" 达到最大连接数了,最大连接数是: " + MPServer.MAXCONNECTIONS);
		srv.log("===========================");
		try {
			PrintWriter out = new PrintWriter(toClient.getOutputStream());
			out.print("达到最大连接数了,最大连接数是: " + MPServer.MAXCONNECTIONS + "\n");
			out.flush();
		} catch (IOException e) {
			srv.log(e);
		}
	}

	// do my biz
	public void doNewBiz() {
		// System.out.println(new Date());
		String strIn = null, strOut = null;
		try {
			// 01, 读取客户端发来的信息
			BufferedReader in = new BufferedReader(new InputStreamReader(
					toClient.getInputStream()));
			strIn = in.readLine();
			// while ((strIn = in.readLine()) != null) {
			// strIn += strIn;
			// }
			System.out.println(strIn); // Release 版本要屏蔽掉

			// 02, 处理客户端的信息
			String strSQL = null;
			String strName = null, strGender = null, strWorkunit = null, strCareer = null;
			// 02.1 分析用户的输入,为构造SQL语句准备
			// ....
			strSQL = "select name, gender, workunit, career from mp_tmp_pop;";

			// 02.2 调用数据库,获取数据
			// 加载驱动程序
			Class.forName("com.mysql.jdbc.Driver");
			// 连接数据库
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "");   //FFS
			Statement stmt = conn.createStatement();
			ResultSet rcdst = stmt.executeQuery(strSQL);

			rcdst.last();
			int rowCount = rcdst.getRow();
			strOut = rowCount + " || ";

			if (rowCount > 0) {
				rcdst.first();
				do {
					// 获取数据
					strName = rcdst.getString("name");
					strGender = rcdst.getString("gender");
					strWorkunit = rcdst.getString("workunit");
					strCareer = rcdst.getString("career");

					strOut = strOut + strName + "--" + strGender + "--"
							+ strWorkunit + "--" + strCareer + " || ";
				}while(rcdst.next());
			}
			
			rcdst.close();
			stmt.close();
			conn.close();

			// 03, 把结果发送到客户端
			PrintWriter out = new PrintWriter(toClient.getOutputStream());
			out.print(strOut);
			out.flush();

			// Thread.sleep(25*1000);
		} /*
			 * catch (InterruptedException e) { // block e.printStackTrace(); }
			 */catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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