File Server implemented by socket (5)

Source: Internet
Author: User

/**
* Data Packet content Parsing
*
* @ 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 ();
}
}
}
/**
* All are separated by ox1e.
* @ Param Str
* @ Param out
*/
Public static void doparse (string STR, printwriter out)
{
String [] STRs = Str. Split ("/0x1e ");
If ("0". Equals (STRs [0]) // read the topic
{
Int forumid = integer. parseint (STRs [1]);
Int topicid = integer. parseint (STRs [2]);
Out. println (forumfileoperator. gettopiccontent (forumid, topicid ));
}
Else if ("1". Equals (STRs [0]) // read replies
{
Int forumid = integer. parseint (STRs [1]);
Int topicid = integer. parseint (STRs [2]);
List list = forumfileoperator. getreplylist (forumid, topicid );
// Write the job here
Iterator it = List. iterator ();
While (it. hasnext ())
{
Out. println (it. Next (). tostring ());
}
}
Else if ("2". Equals (STRs [0]) // post
{
Int forumid = integer. parseint (STRs [1]);
Int topicid = integer. parseint (STRs [2]);
Forumfileoperator. addtopic (forumid, topicid, STRs [3]);
}
Else if ("3". Equals (STRs [0]) // resume the post
{
Int forumid = integer. parseint (STRs [1]);
Int topicid = integer. parseint (STRs [2]);
Forumfileoperator. resumetopic (forumid, topicid, STRs [3]);
}
Else if ("4". Equals (STRs [0]) // reply
{
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]) // Delete the topic
{
Int forumid = integer. parseint (STRs [1]);
Int topicid = integer. parseint (STRs [2]);
Forumfileoperator. deletetopic (forumid, topicid );
}
Else if ("6". Equals (STRs [0]) // delete
{
Int forumid = integer. parseint (STRs [1]);
Int topicid = integer. parseint (STRs [2]);
Int replyid = integer. parseint (STRs [3]);
Forumfileoperator. deletereply (forumid, topicid, replyid );
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.