当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:14:00  来源:本站整理

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

赞助商链接



  本文“[JAVA100例]044、多线程服务器:每个人都有份[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

// 文件名: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...\n");
  //利用8000端口供应服务
  ServerSocket server = new ServerSocket (8000);
  while (true)
  {
   //阻塞,直到有客户衔接
   Socket sk = server.accept ();
   System.out.println ("Accepting Connection...\n");
   //启动服务线程
   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...\n");
    //最后释放资源
    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);
  }
}


  以上是“[JAVA100例]044、多线程服务器:每个人都有份[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .