- package com.xyz.Internet;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import javax.swing.JFrame;
- public class MulitThreadServer extends JFrame{
- public static void main(String[] args) {
- }
- public MulitThreadServer(){
- try {
- while(true){
- ServerSocket serversocket = new ServerSocket(8000);
- Socket socket = serversocket.accept();
- int clientNo = 1;
- HandleClient task = new HandleClient(socket);
- task.start();
- clientNo++;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- class HandleClient implements Runnable{
- private Socket socket;
- public HandleClient(Socket socket) {
- this.socket = socket;
- }
- public void run() {
- try {
- DataInputStream input = new DataInputStream(socket.getInputStream());
- DataOutputStream output = new DataOutputStream(socket.getOutputStream());
- while (true){
- double radius = input.readDouble();
- double area = radius* radius* Math.PI;
- output.writeDouble(area);
- output.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2610201513914.html
来源: http://www.codesnippet.cn/detail/2610201513914.html