用socket實現的檔案伺服器(5)

來源:互聯網
上載者:User

/**
 * 資料包內容解析
 *
 * @author HongSoft
 */
public class PacketParserThread extends Thread
{
    private Socket socket;

    private BufferedReader in;

    private PrintWriter out;

    public PacketParserThread(Socket s) throws IOException
    {
        socket = s;
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                .getOutputStream())), true);
        start();
    }

    public void run()
    {
        try
        {
            String str = in.readLine();
            if (str == null || str.length() == 0)
            {
                // do nothing
            }
            else
            {
               doParse(str,out);             
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    /**
     * 中間全部用ox1e分割
     * @param str
     * @param out
     */
    public static void doParse(String str,PrintWriter out)
    {
        String[] strs=str.split("/0x1e");     
        if("0".equals(strs[0]))//讀主題貼
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            out.println(ForumFileOperator.getTopicContent(forumId,topicId));
        }
        else if("1".equals(strs[0]))//讀回帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            List list=ForumFileOperator.getReplyList(forumId,topicId);
            //在這裡進行寫出工作
            Iterator it=list.iterator();
            while(it.hasNext())
            {
                out.println(it.next().toString());
            }
        }
        else if("2".equals(strs[0]))//發帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.addTopic(forumId,topicId,strs[3]);
        }
        else if("3".equals(strs[0]))//續帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.resumeTopic(forumId,topicId,strs[3]);
        }
        else if("4".equals(strs[0]))//回帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.addReply(forumId,topicId,strs[3],strs[4],strs[5]);
        }
        else if("5".equals(strs[0]))//刪主題貼
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.deleteTopic(forumId,topicId);
        }
        else if("6".equals(strs[0]))//刪回貼中的某一個
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            int replyId=Integer.parseInt(strs[3]);
            ForumFileOperator.deleteReply(forumId,topicId,replyId);
        }
      
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.