免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1739 | 回复: 0
打印 上一主题 下一主题

Java multi thread client and server example [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-10 16:48 |只看该作者 |倒序浏览

***************************server.java************************
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class server{
    public static void main(String args[]) {
         int port = 8100;
        int count=1;
         ServerSocket server_socket;

   try {
     //listen and accept
     server_socket = new ServerSocket(port);
     System.out.println("Server running on port " + server_socket.getLocalPort());
     //display init message
     while(true) {
        Socket socket = server_socket.accept();
        System.out.println("New accepted  "+ count);
        count++;
        //System.out.println("New connection accepted " +
        //socket.getInetAddress() + ":" + socket.getPort());
          //create new thread
          try {
              RequestHandler request = new RequestHandler(socket);
               Thread thread = new Thread(request);
                //start thread
                 thread.start();
          }
          catch(Exception e) {
              System.out.println(e);
          }
     }
   }
   catch (IOException e) {
       System.out.println(e);
   }
}
}
class RequestHandler implements Runnable
{
    Socket socket;
    InputStream input;
    OutputStream output;
    DataOutputStream outbound;
    BufferedReader br;
    //constructor
    public RequestHandler(Socket socket) throws Exception
    {
     this.socket = socket;
      this.input = socket.getInputStream();
      this.output = socket.getOutputStream();
     
     outbound = new DataOutputStream(output);
      this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    // implement Runnable interface run()
    public void run()
    {
         try {
             processRequest();
         }
         catch(Exception e) {
             System.out.println(e);
         }
    }
   
    private void processRequest() throws Exception
    {
         //while(true) {
          // read message from client
          //System.out.println("ready to read data from client");
             String str = br.readLine();
            System.out.println("The client request is "+str);
            // output
          outbound.writeBytes("ok\n");
        
    //}

     // close socket
      try {
          br.close();
         socket.close();
     }
     catch(Exception e) {}
     
    }
     
}
************************client.java************************
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class client{
    public static void main(String args[])
    {     
     int number=0;
     
     if(args.length != 1)
     {
         System.out.println("usage: client number");
        System.exit(0);
     }
     else
     {
         number = Integer.parseInt(args[0]);
     }
     
     for(int i=0;i<number;i++)
     {
        try {
              myconnect my = new myconnect();
               Thread thread = new Thread(my);
                 thread.start();
          }
          catch(Exception e) {
              System.out.println(e);
          }
     }//end for
   }// end main
}
class myconnect implements Runnable
{
    Socket socket;
    BufferedReader in;
    DataOutputStream out;
    //constructor
    public myconnect() throws Exception
    {
         socket = new Socket("192.168.20.47",8100);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new DataOutputStream(socket.getOutputStream());
    }
    // implement Runnable interface run()
    public void run()
    {
         try {
             processRequest();
         }
         catch(Exception e) {
             System.out.println(e);
         }
    }
   
    private void processRequest() throws Exception
    {
         //while(true) {
          // read message from client
          out.writeBytes("hello\n");
             String str = in.readLine();
            System.out.println("recv form server =  "+str);
         
     // close socket
      try {
          in.close();
         out.close();
         socket.close();
     }
     catch(Exception e) {}
     
    }
     
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/3063/showart_66773.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP