C/s application entry-level practice
Our work is based on the B/S architecture. A few days ago, I had a c/s assignment, which is roughly as follows:
Make a C/S program, such as a price-based game. The server tells you that the game is low.
For the first time, I had no time to write a script. I used a server for this thing. For the Northeast: What kind of bicycle !!!
The script code is as follows:
<HTML>
<Head>
<Title> test now </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Style>
. Testclass
{
Color: green;
Font: bold 18pt Arial
}
</Style>
<Table>
<Tr>
<TD class = "testclass"> Hello </TD>
<! --
<TD> <input type = "text" name = "some" onchange = "checkidnum (this. Value);"/> </TD>
-->
<TD nowrap> <input type = "text" class = "testclass" size = "60" name = "some" id = "some" onchange = "checknum (this. value); "style =" display: Block "/> </TD>
</Tr>
<Tr>
<TD Height = "20"> </TD>
</Tr>
<Tr>
<TD nowrap colspan = "2"> <input type = "text" class = "testclass" size = "60" name = "cc" id = "cc" readonly = "readonly ""Style =" display: none "> </TD>
</Tr>
<Tr>
<TD Height = "20"> </TD>
</Tr>
<Tr>
<TD nowrap colspan = "2"> <input type = "text" class = "testclass" size = "20" name = "VV" id = "VV" readonly = "readonly ""Style =" display: none "> </TD>
</Tr>
</Table>
<Script language = "JavaScript">
VaR COUNT = 0;
Function checknum (STR ){
VaR A = 500;
VaR ran = math. Random ();
VaR foot = math. Round (Ran * (A-1) + 1;
VaR inputinfo = Document. getelementbyid ("VV ");
Inputinfo. value = correct result: '+ foot;
Inputinfo. style. Display = "Block ";
If (STR! = Foot ){
VaR inputinfo = Document. getelementbyid ("some ");
Inputinfo. value = '';
Inputinfo. style. Display = "NONE ";
VaR inputcc = Document. getelementbyid ("cc ");
Inputcc. value = 'You are input failure! Haha! Please refresh this page! ';
Inputcc. style. Display = "Block ";
}
If (STR = foot ){
If (confirm ("You are input success! Try it again? ")){
VaR inputinfo = Document. getelementbyid ("some ");
Inputinfo. value = 'Please input 0 ~ 500 number again, good luck ';
VaR inputcc = Document. getelementbyid ("cc ");
Count ++;
Inputcc. value = 'You are input success! '+ Count + ''then ';
Inputcc. style. Display = "Block ";
}
}
}
Document. writeln ("this is JavaScript! Use the direct insert method! ");
</SCRIPT>
<HTML>
It looks similar, said the teacher, helpless, had to waste time again!
The following code is written in C/S:
Package knockwork;
Import java. Io .*;
Import java.net .*;
/*
* Add by Kevin. Wang in Shanghai
*/
Public class myclient {
Public static void main (string [] ARGs) throws ioexception {
Socket kksocket = NULL;
Printwriter out = NULL;
Bufferedreader in = NULL;
Try {
Kksocket = new socket ("localhost", 4444 );
Out = new printwriter (kksocket. getoutputstream (), true );
In = new bufferedreader (New inputstreamreader (kksocket. getinputstream ()));
} Catch (unknownhostexception e ){
System. Err. println ("don't know about host: localhost .");
System. Exit (1 );
} Catch (ioexception e ){
System. Err. println ("couldn't get I/O for the connection to: localhost .");
System. Exit (1 );
}
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
String fromserver;
String fromuser;
While (fromserver = in. Readline ())! = NULL ){
System. Out. println ("server:" + fromserver );
If (fromserver. Equals ("bye ."))
Break;
Fromuser = stdin. Readline ();
If (fromuser! = NULL ){
System. Out. println ("client:" + fromuser );
Out. println (fromuser );
}
}
Out. Close ();
In. Close ();
Stdin. Close ();
Kksocket. Close ();
}
}
// The above is the client application
Package knockwork;
Import java.net .*;
Import java. Io .*;
/*
* Add by Kevin. Wang in Shanghai
*/
Public class myserver {
Public static void main (string [] ARGs) throws ioexception {
Serversocket = NULL;
Try {
Serversocket = new serversocket (4444 );
} Catch (ioexception e ){
System. Err. println ("cocould not listen on port: 4444 .");
System. Exit (1 );
}
Socket clientsocket = NULL;
Try {
Clientsocket = serversocket. Accept ();
} Catch (ioexception e ){
System. Err. println ("Accept failed .");
System. Exit (1 );
}
Printwriter out = new printwriter (clientsocket. getoutputstream (), true );
Bufferedreader in = new bufferedreader (
New inputstreamreader (
Clientsocket. getinputstream ()));
String inputline, outputline;
Mybusinessprotocol kkp = new mybusinessprotocol ();
Outputline = kkp. processinput (null, 0 );
Out. println (outputline );
Int clientbid = 0;
While (inputline = in. Readline ())! = NULL ){
Try {
Clientbid = integer. parseint (inputline. Trim ());
} Catch (numberformatexception e ){
Out. println ("You must input int type data, Please rewrite! ");
Continue;
}
Outputline = kkp. processinput (inputline, clientbid );
Out. println (outputline );
If (outputline. Equals ("bye ."))
Break;
}
Out. Close ();
In. Close ();
Clientsocket. Close ();
Serversocket. Close ();
}
}
// The above is the server running program
Package knockwork;
Import java. util. Random;
/*
* Add by Kevin. Wang in Shanghai
*/
Public class mybusinessprotocol {
Private Static final int first = 0;
Private Static final int waiting = 1;
// Private Static final int lower = 2;
// Private Static final int higher = 3;
// Private Static final int OK = 4;
Private Static final int select = 5;
Private Static final int another = 6;
Private Static final int exit = 10;
Private int state = first;
Private int totalspent = 0;
Private int currentbid = 0;
Private int realvalue = 0;
Private stringbuffer itembuffer = new stringbuffer ();
Private string [] items = {"apple", "joybook", "pen", "xylitol", "Mouse "};
Private int joke = 0;
/*
* Main business method
*/
Public String processinput (string theinput, int clientbid ){
String Theoutput = NULL;
If (State = first ){//-----------------------------------------
Initrealvalue ();
Theoutput = "Hello, this is a [" + items [joke] +
"] Please bid! Good luck! ";
State = waiting;
} Else if (State = Waiting ){//-----------------------------------
If (clientbid> = 0 & clientbid <= 500 ){
Totalspent + = clientbid;
}
If (clientbid <0 | clientbid> 500 ){
Theoutput = "OK! Yor are right! Congratulation! "+
"You have spent =" + totalspent +
"! You have obtain: "+ itembuffer. tostring () +
"; Want another? (0/1 )";
State = another;
// Todo have obtain item
}
If (clientbid <realvalue ){
Theoutput = "lower! ";
} Else if (clientbid> realvalue ){
Theoutput = "higher! ";
} Else if (clientbid = realvalue ){
If (itembuffer. Length ()> 0 ){
Itembuffer. append (",");
}
Itembuffer. append (items [joke]);
Theoutput = "OK! Yor are right! Congratulation! "+
"You have spent =" + totalspent +
"! You have obtain: "+ itembuffer. tostring () +
"; Want another? (0/1 )";
// Todo have obtain item
State = another;
}
} Else if (State = select ){//------------------------------------
Theoutput = "want another? (0/1 )";
State = another;
} Else if (State = another ){//-----------------------------------
If (clientbid = 1 ){
Theoutput = "Hello, this is a [" + items [joke] +
"] Please bid! Good luck! ";
State = waiting;
Initrealvalue ();
If (joke> = 4 ){
Joke = 0;
} Else {
Joke ++;
}
} Else {
Theoutput = "bye .";
State = waiting;
}
}
Return Theoutput;
}
/*
* Calculate immediately
*/
Private int testrandom (){
Int num = 500;
Random random = new random ();
Int A = random. nextint (Num );
// System. Out. println ("A =" + );
Return;
}
Public int getrealvalue (){
Return realvalue;
}
Public void initrealvalue (){
This. realvalue = testrandom ();
}
}
// This is the service logic used by the server
Because I don't have any servers, I use them on my own, so the host uses localhost
Running Mode: Run myserver first, and then run myclient.
"Hello, this is a [Apple] Please bid! Good luck! "
You can bid [0 ~ 500]. run and play on your own.