⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketrequestdispatcher.java

📁 java实现的遗传算法
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.distr.sockets;

import java.io.*;
import java.net.*;

import org.jgap.distr.*;

/**
 * Sends a request over sockets from one host to another
 * (= KnockKnockClient).
 *
 * @author Klaus Meffert
 * @since 2.4
 */
public class SocketRequestDispatcher
    extends RequestDispatcher {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.3 $";

  /**
   * Default constructor
   * @author Klaus Meffert
   * @since 2.4
   */
  public SocketRequestDispatcher() {
    super();
  }

  public void dispatch(IWorker a_worker, WorkerCommand a_command)
      throws IOException {
    Socket kkSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    try {
      kkSocket = new Socket("127.0.0.1", 4445);//4445= port of worker
      out = new PrintWriter(kkSocket.getOutputStream(), true);
      in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
    }
    catch (UnknownHostException e) {
      System.err.println("Don't know about host: 127.0.0.1.");
      System.exit(1);
    }
    catch (IOException e) {
      System.err.println("Couldn't get I/O for the connection to: 127.0.0.1.");
      System.exit(1);
    }
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer;
    String fromUser;
    while ( (fromServer = in.readLine()) != null) {
      System.out.println("Server: " + fromServer);
      if (fromServer.equals("Bye."))
        break;
      fromUser = stdIn.readLine();
      if (fromUser != null) {
        System.out.println("Client: " + fromUser);
        out.println(fromUser);
      }
    }
    out.close();
    in.close();
    stdIn.close();
    kkSocket.close();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -