程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> [JAVA100例]044、多線程服務器:每個人都有份

[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);
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved