當前位置:編程學習大全網 - 源碼下載 - Java Socket***享的設計原理深層透析

Java Socket***享的設計原理深層透析

 Java Socket***享在使用的時候有很多的問題需要我們解決 其實有不少的問題都是在源代碼中可以找到答案的 下面我們就來看看如何才能更好的使用有關的Java <> Socket***享服務

 在實際的網絡環境裏 同壹時間只對壹個用戶服務是不可行的 壹個優秀的網絡服務程序除了能處理用戶的輸入信息 還必須能夠同時響應多個客戶端的連接請求 在Java Socket***享中 實現以上功能特點是非常容易的

 Java Socket***享的設計原理

 主程序監聽壹端口 等待客戶接入;同時構造壹個線程類 準備接管會話 當壹個Socket會話產生後 將這個會話交給線程處理 然後主程序繼續監聽 運用Thread類或Runnable接口來實現是不錯的辦法

 {實現消息***享}

  import java io *;

  import *;

  public class Server extends ServerSocket

  {

  private static final int SERVER_PORT = ;

  public Server() throws IOException

  {

  super(SERVER_PORT);

  try

  {

  while (true)

  {

  Socket socket = accept();

  new CreateServerThread(socket);

  }

  }

  catch (IOException e)

  {}

  finally

  {

  close();

  }

  }

  // CreateServerThread

  class CreateServerThread extends Thread

  {

  private Socket client;

  private BufferedReader in;

  private PrintWriter out;

  public CreateServerThread(Socket s) throws IOException

  {

  client = s;

  in = new BufferedReader(new InputStreamReader(client getInputStream() GB ));

  out = new PrintWriter(client getOutputStream() true);

  out println( Wele );

  start();

  }

  public void run()

  {

  try

  {

  String line = in readLine();

  while (!line equals( bye ))

  {

  String msg = createMessage(line);

  out println(msg);

  line = in readLine();

  }

  out println( See you bye! );

  client close();

  }

  catch (IOException e)

  {}

  }

  private String createMessage(String line)

  {

  xxxxxxxxx;

  }

  }

  public static void main(String[] args) throws IOException

  {

  new Server();

  }

  }

lishixinzhi/Article/program/Java/hx/201311/25608

  • 上一篇:靠譜的貸款公眾號
  • 下一篇:手機麻將的原理與技巧
  • copyright 2024編程學習大全網