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

📄 servethread.java

📁 本工程模拟网上社区,其中包括银行,软件销售公司,软件投资公司,茶餐馆,和多个服务器.
💻 JAVA
字号:
package softwarecompanyserver;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.util.StringTokenizer;

public class ServeThread implements Runnable{
    private String[] text ;
    static boolean shutdown = false;
    private Socket socket;
    private DataInputStream in;
    private DataOutputStream out;
    public ServeThread(Socket s) {
        socket = s;
        try {
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
        Thread t=new Thread(this);
        t.start();
    }

    public void run() {
        while(!shutdown)
        {
            String str = null;
            try {
                str = in.readUTF();
            } catch (IOException ex1) {
                System.out.println();
            }
            StringTokenizer st = new StringTokenizer(str, "/");
            String classify = null; //命令类型
            int i = st.countTokens();
            System.out.println("countToken"+i);
            classify = st.nextToken();
            text = new String[i--];
            for (int j = 0; j < i; j++) {
                text[j] = st.nextToken();
            }
            System.out.println("read:"+classify);
            switch(Integer.parseInt(classify))
            {
                case 0:    //退出线程
                    death();
                    break;
                case 1:    //客户端初始化
                    initialize();
                    break;
                case 2:    //新建公司
                    Company aCompany = new Company(text[0],text[1],text[2],text[3],text[4],text[5]);
                    inSoftCompany dialog1 = new inSoftCompany(socket,aCompany);
                    break;
                case 3:    //新建项目
                    Item aItem = new Item(text[0],text[1],text[2],text[3],text[4]);
                    inSoftCompany dialog2 = new inSoftCompany(socket,aItem,text[5]);;
                    break;
            }
        }
    }
    public void death()
    {
        try {
            in.close();
            out.close();
            socket = null;
        } catch (IOException ex){
            System.out.println(ex.toString());
        }
        shutdown = true;
        System.out.println("shutdown:"+shutdown);
    }
    public void initialize()
    {
        CompanyDA.initialize();
        System.out.println("数据库已连接!");
        int k = CompanyDA.getAll().size();
        String[] string = new String[k];
        Company aCompany;
        String str = String.valueOf(k);
        for (int i = 0; i < k; i++) {
            aCompany = (Company) Company.getAll().get(i);
            string[i] = '/' + aCompany.getName() + '/' + aCompany.getId()
                        + '/' + aCompany.getAccount() + '/' + aCompany.getNum()
                        + '/' + aCompany.getTime() + '/' + aCompany.getInfo();
            str += string[i];
        }
        Company.terminate();
        try {
            out.writeUTF(str);
            out.flush();
        } catch (IOException ex) {
            System.out.println(ex.toString());
            System.exit(0);
        }
    }

}

⌨️ 快捷键说明

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