Chinaunix首页 | 论坛 | 博客
  • 博客访问: 176755
  • 博文数量: 77
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 990
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-21 18:13
文章分类

全部博文(77)

文章存档

2011年(1)

2009年(76)

我的朋友

分类: Java

2009-07-10 23:04:33

/**

 *1.AtomsData.java 对象类,实现Serializable接口

 */

 

import java.io.Serializable;
  
public class AtomsData implements Serializable
{
    private static final long serialVersionUID = 1L;
  
    private String version;
  
    private int[] atomsArray;
  
    AtomsData()
    {
    }
  
    AtomsData(String version, int[] atomsArray)
    {
        this.version = version;
        this.atomsArray = atomsArray;
    }
  
    public int[] getAtomsArray()
    {
        return atomsArray;
    }
  
    public void setAtomsArray(int[] atomsArray)
    {
        this.atomsArray = atomsArray;
    }
  
    public String getVersion()
    {
        return version;
    }
  
    public void setVersion(String version)
    {
        this.version = version;
    }
  
    public String toString()
    {
        return "AtomsData info:" + this.version;
    }
}

 

 

/**

 *2. ServerHandle.java 服务器对象操作类

 */

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
  
public class ServerHandle implements Runnable
{
    private Socket socket;
  
    public ServerHandle(Socket socket)
    {
        this.socket = socket;
    }
  
    private ObjectInputStream getAtomsReceive(Socket socket) throws IOException
    {
        return new ObjectInputStream(socket.getInputStream());
    }
  
    public void run()
    {
        try
        {
            ObjectInputStream ois = this.getAtomsReceive(socket);
            Object o = ois.readObject();
            while (o != null)
            {
                AtomsData atomsdata = (AtomsData) o;
                System.out.println("The version of operation is " 

                                + atomsdata.getVersion() + " .");
            }
        } catch (IOException e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (socket != null)
                    socket.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

 

 

/**

 * 3.AtomsServer.java 服务类

 */

 

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
  
public class AtomsServer
{
    private int port = 8833;
  
    private ServerSocket serverSocket;
  
    private ExecutorService executorService;
  
    private final int POOL_SIZE = 10;
  
    public AtomsServer()
    {
        try
        {
            serverSocket = new ServerSocket(port);
            executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_SIZE);
            System.out.println("The server has started!");
        } catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
  
    public void serviceStart()
    {
        while (true)
        {
            Socket socket = null;
            try
            {
                socket = serverSocket.accept();
                executorService.execute(new ServerHandle(socket));
  
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
  
    public static void main(String[] args)
    {
        new AtomsServer().serviceStart();
    }
}


 

 

 

 

/**

 *4.AtomsClient.java 客户端类

 */

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
  
public class AtomsClient
{
    public static void main(String args[]) throws InterruptedException
    {
        ExecutorService exec = Executors.newFixedThreadPool(2);
        for (int index = 0; index < 10; index++)
        {
            Runnable run = new Runnable()
            {
                private Socket socket = null;
  
                private int port = 8833;
  
                AtomsData atomsdata;
  
                ObjectOutputStream oos = null;
  
                public void run()
                {
                    while (true)
                    {
                        try
                        {
                            socket = new Socket("localhost", port);
                            atomsdata = new AtomsData();
                            atomsdata.setVersion((new Double((long) (Math.random() * 1000))).toString());
                            oos = new ObjectOutputStream(socket.getOutputStream());
                            oos.writeObject(atomsdata);
                        } catch (IOException e)
                        {
                            e.printStackTrace();
                        } finally
                        {
                            try
                            {
                                oos.close();
                            } catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            };
            exec.execute(run);
        }
        exec.shutdown();
    }
}

阅读(1221) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~