Java TCP/IP socket programming (9)

Source: Internet
Author: User
3.5.3 sending and receiving

The following is a service used by the voting server to process the voting information received by the server.

Package COM. suifeng. tcpip. chapter3.vote; import Java. util. hashmap; import Java. util. map;/*** the server processes the obtained voting information * @ author administrator **/public class voteservice {private Map <integer, long> Results = new hashmap <integer, long> (); Public votemsg handlerequest (votemsg MSG) {If (MSG. isresponse () {return MSG;} // sets the return flag of the voting information. setresponse (true); // voter No. Int candidate = MSG. getcandidate (); // obtain the total number of votes of the current user long co Unt = results. Get (candidate); // If (COUNT = NULL) {COUNT = 0l;} // If (! MSG. isinquery () {results. Put (candidate, ++ count);} // set the total number of votes msg. setvotecount (count); Return MSG ;}}

3.5.3.1
Based on text codec and TCP socket

Client

Package COM. suifeng. tcpip. chapter3.vote; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import java.net. socket; import java.net. unknownhostexception; import COM. suifeng. tcpip. chapter3.framer. framer; import COM. suifeng. tcpip. chapter3.framer. lengthframer; public class votetcptextclient {Private Static final int candidate = 888;/*** @ Param ARGs * @ throws ioexception * @ th Rows unknownhostexception */public static void main (string [] ARGs) throws unknownhostexception, ioexception {If (ARGs. length! = 2) {Throw new illegalargumentexception ("parameter (s): <Server> <port>") ;}// sername or IP addressstring Server = ARGs [0]; // obtain the port number int SERVERPORT = integer. parseint (ARGs [1]); // use the specified server and port to create socketsocket socket = new socket (server, SERVERPORT); system. out. println ("connected to server ..... sending echo string "); inputstream in = socket. getinputstream (); outputstream out = socket. getoutputstream (); votemsgcoder coder = new votemsgtextcoder (); framer = new lengthframer (in); // The first parameter is true, indicating that the message is queried, the second parameter is false, indicating that the non-returned message votemsg = new votemsg (true, false, candidate, 0); // uses text encoding byte [] encodemsg = coder. towire (MSG); // initiate the query system. out. println ("++ + "); system. out. println ("sending inquery (" + encodemsg. length + "bytes)"); system. out. println (MSG); framer. framemsg (encodemsg, out); // initiate a vote MSG. setinquery (false); encodemsg = coder. towire (MSG); framer. framemsg (encodemsg, out); system. out. println ("sending inquery (" + encodemsg. length + "bytes)"); system. out. println (MSG); system. out. println ("++ + "); system. out. println (""); // receives the query and returns encodemsg = framer. nextmsg (); MSG = coder. fromwire (encodemsg); system. out. println ("++ + "); system. out. println ("received response (" + encodemsg. length + "bytes)"); system. out. println (MSG); // receive the vote and return MSG = coder. fromwire (framer. nextmsg (); system. out. println ("received response (" + encodemsg. length + "bytes)"); system. out. println (MSG); system. out. println ("++ + "); system. out. println (""); // the second round of voting MSG. setresponse (false); encodemsg = coder. towire (MSG); system. out. println ("second sending inquery (" + encodemsg. length + "bytes)"); framer. framemsg (encodemsg, out); system. out. println (MSG); system. out. println ("++ + "); // message = coder is returned for the second receive vote. fromwire (framer. nextmsg (); system. out. println ("second received ed response (" + encodemsg. length + "bytes)"); system. out. println (MSG); system. out. println ("++ + "); socket. close ();}}

Server

Package COM. suifeng. tcpip. chapter3.vote; import Java. io. ioexception; import java.net. serversocket; import java.net. socket; import COM. suifeng. tcpip. chapter3.framer. framer; import COM. suifeng. tcpip. chapter3.framer. lengthframer; public class votetcptextserver {public static void main (string [] ARGs) throws ioexception {If (ARGs. length! = 1) {Throw new illegalargumentexception ("parameter: <port>");} // obtain the server port int SERVERPORT = integer. parseint (ARGs [0]); // create the socketserver instance serversocket Server = new serversocket (SERVERPORT) for client connection; system. out. println ("server has started !!!! "); Votemsgcoder coder = new votemsgtextcoder (); voteservice service = new voteservice (); While (true) {Socket socket = server. accept (); system. out. println ("Handling Client at" + socket. getremotesocketaddress (); // use the length-based frame forming method framer = new lengthframer (socket. getinputstream (); byte [] req; while (req = framer. nextmsg ())! = NULL) {system. out. println ("++ "); // first decode the voting information and then process votemsg resmsg = service. handlerequest (coder. fromwire (req); // framer is returned after the voting information is encoded. framemsg (coder. towire (resmsg), socket. getoutputstream (); system. out. println ("resonpnse message:" + resmsg); system. out. println ("++"); system. out. println ("");} socket. close ();}}}

Start the server and listen to port 39393

Start the client

View the server again

Start the client again

Check the server

3.5.3.2 based on binary codec and TCP socket

Client

Package COM. suifeng. tcpip. chapter3.vote; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import java.net. socket; import java.net. unknownhostexception; import COM. suifeng. tcpip. chapter3.framer. framer; import COM. suifeng. tcpip. chapter3.framer. lengthframer; public class votetcpbinaryclient {Private Static final int candidate = 888;/*** @ Param ARGs * @ throws ioexception *@ Throws unknownhostexception */public static void main (string [] ARGs) throws unknownhostexception, ioexception {If (ARGs. length! = 2) {Throw new illegalargumentexception ("parameter (s): <Server> <port>") ;}// sername or IP addressstring Server = ARGs [0]; // obtain the port number int SERVERPORT = integer. parseint (ARGs [1]); // use the specified server and port to create socketsocket socket = new socket (server, SERVERPORT); system. out. println ("connected to server ..... sending echo string "); inputstream in = socket. getinputstream (); outputstream out = socket. getoutputstream (); votemsgcoder coder = new votemsgbinarycoder (); framer = new lengthframer (in); // if the first parameter is true, the query message is displayed, the second parameter is false, indicating that the non-returned message votemsg = new votemsg (true, false, candidate, 0); // uses text encoding byte [] encodemsg = coder. towire (MSG); // initiate the query system. out. println ("++ + "); system. out. println ("sending inquery (" + encodemsg. length + "bytes)"); system. out. println (MSG); framer. framemsg (encodemsg, out); // initiate a vote MSG. setinquery (false); encodemsg = coder. towire (MSG); framer. framemsg (encodemsg, out); system. out. println ("sending inquery (" + encodemsg. length + "bytes)"); system. out. println (MSG); system. out. println ("++ + "); system. out. println (""); // receives the query and returns encodemsg = framer. nextmsg (); MSG = coder. fromwire (encodemsg); system. out. println ("++ + "); system. out. println ("received response (" + encodemsg. length + "bytes)"); system. out. println (MSG); // receive the vote and return MSG = coder. fromwire (framer. nextmsg (); system. out. println ("received response (" + encodemsg. length + "bytes)"); system. out. println (MSG); system. out. println ("++ + "); system. out. println ("");/* // the second time the voting MSG is initiated. setresponse (false); encodemsg = coder. towire (MSG); system. out. println ("second sending inquery (" + encodemsg. length + "bytes)"); framer. framemsg (encodemsg, out); system. out. println (MSG); system. out. println ("++ + "); // message = coder is returned for the second receive vote. fromwire (framer. nextmsg (); system. out. println ("second received ed response (" + encodemsg. length + "bytes)"); system. out. println (MSG); system. out. println ("++ + "); */socket. close ();}}

Server

Package COM. suifeng. tcpip. chapter3.vote; import Java. io. ioexception; import java.net. serversocket; import java.net. socket; import COM. suifeng. tcpip. chapter3.framer. framer; import COM. suifeng. tcpip. chapter3.framer. lengthframer; public class votetcpbinaryserver {public static void main (string [] ARGs) throws ioexception {If (ARGs. length! = 1) {Throw new illegalargumentexception ("parameter: <port>");} // obtain the server port int SERVERPORT = integer. parseint (ARGs [0]); // create the socketserver instance serversocket Server = new serversocket (SERVERPORT) for client connection; system. out. println ("server has started !!!! "); Votemsgcoder coder = new votemsgbinarycoder (); voteservice service = new voteservice (); While (true) {Socket socket = server. accept (); system. out. println ("Handling Client at" + socket. getremotesocketaddress (); framer = new lengthframer (socket. getinputstream (); byte [] req; while (req = framer. nextmsg ())! = NULL) {system. out. println ("++"); system. out. println ("received message (" + req. length + ") byte. "); // first decodes the voting information and then processes votemsg resmsg = service. handlerequest (coder. fromwire (req); // framer is returned after the voting information is encoded. framemsg (coder. towire (resmsg), socket. getoutputstream (); system. out. println ("resonpnse message:" + resmsg); system. out. println ("++"); system. out. println ("");} socket. close ();}}}

Start the server and listen to port 39393

Start the client

View servers

Start the client again

View the server again

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.