Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1096656
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2009-06-24 15:11:50

import java.io.*;
import java.net.*;

class Handler extends Thread
{
        Socket socket;
        DataInputStream in;
        DataOutputStream out;

        Handler(Socket socket)
        {
                this.socket = socket;
        }

        public void run()
        {
                try
                {
                        in = new DataInputStream(socket.getInputStream());
                        out = new DataOutputStream(socket.getOutputStream());
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }

                byte[] data = new byte[1024];
                int dlen;


                while (true)
                {
                        try
                        {
                                dlen = in.read(data, 0, 1024);
                                if (dlen == -1)
                                {
                                        break;
                                }
                                out.write(data, 0, dlen);
                        }
                        catch(Exception e)
                        {
                                System.out.println("exception: " + e.getMessage());
                                break;
                        }
                }

                try
                {
                        in.close();
                        out.close();
                        socket.close();
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
}

public class server
{
        public static void main(String args[]) throws Exception
        {
                ServerSocket serv = new ServerSocket(8888);

                while (true)
                {
                        Socket socket = serv.accept();

                        Handler h = new Handler(socket);
                        h.start();
                }
        }
}


import java.io.*;
import java.net.*;

class Sender extends Thread
{
        String msg;
        Socket socket;
        DataInputStream in;
        DataOutputStream out;

        Sender(String str)
        {
                msg = str;
        }

        public void run()
        {
                try
                {
                        socket = new Socket("127.0.0.1", 8888);
                        in = new DataInputStream(socket.getInputStream());
                        out = new DataOutputStream(socket.getOutputStream());

                        DataOutputStream stdout = new DataOutputStream(System.out);

                        byte[] data;
                        data = msg.getBytes();
                        out.write(data, 0, data.length);

                        byte[] buf = new byte[1024];
                        int dlen;


                        while (true)
                        {
                                dlen = in.read(buf, 0, 1024);
                                if (dlen == -1)
                                {
                                        System.out.println("client in.read -1.");
                                        break;
                                }
                                out.write(buf, 0, dlen);

                                stdout.write(buf, 0, dlen);
                        }
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
}

public class client
{
        public static void main(String args[])
        {
                for (int i = 0; i < 100; i++)
                {
                        Sender sender = new Sender(String.valueOf(i));
                        sender.start();
                }
        }
}

阅读(508) | 评论(0) | 转发(0) |
0

上一篇:java发送邮件

下一篇:java nio echo 服务

给主人留下些什么吧!~~