[JAVA100 example]044, multithreaded server: Everyone has a share

Source: Internet
Author: User

FileName: Moreserver.java


import java.io.*;


import java.net.*;


import java.util.*;


/**


* <p>title: Multi-threaded server </p>


* <p>description: This example uses multithreading to implement multiple service functions. </p>


* <p>copyright:copyright (c) 2003</p>


* <p>filename: </p>


* @version 1.0


*/


class Moreserver


{


public static void Main (String [] args) throws IOException


{


System.out.println ("Server starting...\n");


//Service with 8000 ports


serversocket Server = new ServerSocket (8000);


while (true)


  {


//block until customer is connected


Socket SK = server.accept ();


System.out.println ("accepting connection...\n");


//Start service thread


New Serverthread (SK). Start ();


  }


}


}


//using threads, servicing multiple clients


class Serverthread extends Thread


{


private Socket SK;





Serverthread (Socket SK)


{


this.sk = SK;


}


//Thread run entity


public void Run ()


{


BufferedReader in = null;


printwriter out = null;


try{


InputStreamReader ISR;


ISR = new InputStreamReader (Sk.getinputstream ());


in = new BufferedReader (ISR);


out = new PrintWriter (


New BufferedWriter (


New OutputStreamWriter (


Sk.getoutputstream ()), true);


while (true) {


//receives requests from clients and returns different information according to different commands.


String cmd = In.readline ();


System.out.println (CMD);


if (cmd = null)


break;


cmd = Cmd.touppercase ();


if (Cmd.startswith ("BYE")) {


out.println ("BYE");


break;


}else{


out.println ("Hello, I am the server!") ");


    }


   }


}catch (IOException e)


   {


System.out.println (e.tostring ());


   }


finally


   {


System.out.println ("Closing connection...\n");


//FINAL release of resources


try{


if (in!= null)


In.close ();


if (out!= null)


Out.close ();


if (SK!= null)


Sk.close ();


    }


catch (IOException e)


    {


System.out.println ("Close err" +e);


    }


   }


}


}


//filename: Socketclient.java


import java.io.*;


import java.net.*;


class Socketthreadclient extends Thread


{


public static int count = 0;


//constructors, implementing services


public socketthreadclient (inetaddress addr)


{


count++;


BufferedReader in = null;


printwriter out = null;


Socket SK = null;


try{


//Using 8000-Port


SK = new Socket (addr, 8000);


InputStreamReader ISR;


ISR = new InputStreamReader (Sk.getinputstream ());


in = new BufferedReader (ISR);


//Set up output


out = new PrintWriter (


New BufferedWriter (


New OutputStreamWriter (


Sk.getoutputstream ()), true);


//Send request to server


System.out.println ("Count:" +count);


out.println ("Hello");


System.out.println (In.readline ());


out.println ("BYE");


System.out.println (In.readline ());


}


catch (IOException e)


  {


System.out.println (e.tostring ());


  }


finally


  {


out.println ("End");


//Free resources


Try


  {


if (in!= null)


In.close ();


if (out!= null)


Out.close ();


if (SK!= null)


Sk.close ();


  }


catch (IOException e)


  {


  }


  }


}


}


//Client


public class socketclient{


public static void Main (string[] args) throws Ioexception,interruptedexception


  {


inetaddress addr = Inetaddress.getbyname (null);


for (int i=0;i<10;i++)


new Socketthreadclient (addr);


Thread.CurrentThread (). Sleep (1000);


  }


}

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.