频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

[JAVA100例]044、多线程服务器:每个人都有份

来源: 作者: 出处:巧巧读书 2006-09-25 进入讨论组
引用:http://www.qqread.com/java/2006/09/n211544.html

// 文件名:moreServer.java
import java.io.*;
import java.net.*;
import java.util.*;
/**
 * <p>Title: 多线程服务器</p>
 * <p>Description: 本实例使用多线程实现多服务功能。</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... ");
   //使用8000端口提供服务
   ServerSocket server = new ServerSocket (8000);
   while (true)
   {
    //阻塞,直到有客户连接
     Socket sk = server.accept ();
     System.out.println ("Accepting Connection... ");
     //启动服务线程
     new ServerThread (sk).start ();
   }
 }
}
//使用线程,为多个客户端服务
class ServerThread extends Thread
{
 private Socket sk;
 
 ServerThread (Socket sk)
 {
  this.sk = sk;
 }
//线程运行实体
 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){
      //接收来自客户端的请求,根据不同的命令返回不同的信息。
      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 ("你好,我是服务器!");
      }
    }
    }catch (IOException e)
    {
       System.out.println (e.toString ());
    }
    finally
    {
      System.out.println ("Closing Connection... ");
      //最后释放资源
      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);
      }
    }
 }
}


 


 


//文件名:SocketClient.java
import java.io.*;
import java.net.*;
class SocketThreadClient extends Thread
{
 public static int count = 0;
//构造器,实现服务
 public SocketThreadClient (InetAddress addr)
 {
  count++;
  BufferedReader in = null;
  PrintWriter out = null;
  Socket sk = null;
  try{
  //使用8000端口
   sk = new Socket (addr, 8000);
   InputStreamReader isr;
   isr = new InputStreamReader (sk.getInputStream ());
   in = new BufferedReader (isr);
   //建立输出
   out = new PrintWriter (
          new BufferedWriter(
           new OutputStreamWriter(
             sk.getOutputStream ())), true);
   //向服务器发送请求
   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");
   //释放资源
   try
   {
    if (in != null)
     in.close ();
    if (out != null)
     out.close ();
    if (sk != null)
     sk.close ();
   }
   catch (IOException e)
   {
   }
  }
 }
}
//客户端
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);
  }
}

更多文章 更多内容请看FTP服务器双核服务器技术网站服务器的选型专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章