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

2016-02-19 17:02 4 1 收藏

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐[JAVA100例]044、多线程服务器:每个人都有份,赶紧看过来吧!

【 tulaoshi.com - 编程语言 】

  

// 文件名:moreServer.java
import java.io.*;
import java.net.*;
import java.util.*;
/**
 * pTitle: 多线程服务器/p
 * pDescription: 本实例使用多线程实现多服务功能。/p
 * pCopyright: Copyright (c) 2003/p
 * pFilename: /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;i10;i++)
     new SocketThreadClient(addr);
    Thread.currentThread().sleep(1000);
  }
}

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

来源:http://www.tulaoshi.com/n/20160219/1613926.html

延伸阅读
import java.awt.*; import java.awt.event.*; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ImageIcon; /**  * pTitle: 按钮演示/p  * pDescription: 提供一个按钮的演示。如何实现按钮和是一个按钮失效/p  * pCopyright:...
/**  * pTitle: 实现Runnable接口,获得线程。/p  * pDescription: 通过实现Runnable接口来获得自己的线程(t2)。/p  * pCopyright: Copyright (c) 2003/p  * pFilename: twothread.java/p  * @version 1.0  */ public class twothread implements Runnable { /**  *br方法说明:构造器。实际线程,并启动...
import java.io.*; /**  * pTitle: 读取随机文件/p  * pDescription: 演示使用RandomAccessFile类读取文件。/p  * pCopyright: Copyright (c) 2003/p  * pFilename: RandFile.java/p  * @version 1.0  */ public class RandFile{ /**  *br方法说明:主方法  *br输入参数:  *br返回类型: &...
public class flowDemo{ public static void main(String[] arges){ int iPara1,iPara2,iEnd; if(arges.length!=3) { System.out.println("USE :java flowDome parameter1 parameter2 circle"); System.out.println("parameter1 : 比较条件1,数字类型"); System.out.println...
import java.awt.*; import java.awt.event.*; import javax.swing.*; /**  * pTitle: 检查盒演示/p  * pDescription: 选择不同的选择框显示不同的图片/p  * pCopyright: Copyright (c) 2003/p  * pFilename: CheckBoxDemo.java/p  * @version 1.0  */ public class CheckBoxDemo extends JPanel ...

经验教程

260

收藏

73
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部