A client Tester (from Java Network Programming)

Source: Internet
Author: User
/** To change this template, choose tools | templates * and open the template in the editor. */package net. programe. ten; import COM. macfaq. io. *; import Java. io. *; import java.net. *;/***** @ author Arthur */public class clienttester {public static void main (string [] ARGs) {int Port = 80; socket conn = NULL; try {// create a server with port number and queue length of 1 and set it to 1. this server can only receive one connection to serversocket Server = new serversocket (P ORT, 1); system. out. println ("On port" + server. getlocalport () + "listener"); While (true) {// start to receive client connection requests and put the conn returned by each connection into the thread s conn = server. accept (); try {system. out. println ("starting at" + conn + "connection"); // used to read the message thread input = new inputthread (Conn. getinputstream (); input. start (); // used to send a message to the client thread out = new outputthread (Conn. getoutputstream (); out. start (); // Add the main thread to wait for the input and output to end try {input. join (); out. join ();} Catch (interruptedexception ex) {ex. printstacktrace () ;}} catch (ioexception ex) {ex. printstacktrace () ;}// end while} catch (ioexception ex) {ex. printstacktrace ();} finally {If (Conn! = NULL) {try {Conn. close ();} catch (ioexception ex) {ex. printstacktrace () ;}}// end finally} // end main}/*** defines an input stream thread, used to read messages sent from a client * @ author Arthur */class inputthread extends thread {inputstream in; Public inputthread (inputstream in) {This. in = in ;}@ override public void run () {try {While (true) {// read one byte at a time int I = in. read (); // If read, jump out of the loop if (I =-1) {break;} system. out. println (I );} // End while} catch (socketexception ex) {ex. printstacktrace ();} catch (ioexception ex) {system. err. println (Ex);} Try {In. close ();} catch (ioexception ex) {}} // end run}/*** defines an output stream thread, used to send messages to a client * @ author Arthur */class outputthread extends thread {private writer out; Public outputthread (outputstream out) {This. out = new outputstreamwriter (out) ;}@ override public void run () {// used String line entered by the storage console; // obtain data from the console bufferedreader in = new safebufferedreader (New inputstreamreader (system. in); While (true) {try {line = in. readline (); // end the loop with a question mark if (line. equals ("? ") {Break;} // sends the console data out. write (LINE + "\ r \ n"); // refresh out. flush ();} catch (ioexception ex) {ex. printstacktrace () ;}// end while try {out. close ();} catch (ioexception ex) {}} // end run}

Related Article

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.