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

📄 server.java

📁 使用java实现的计算机自主发短信到手机。 运行界面上
💻 JAVA
字号:
// Server.java
package keti2;

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

public class Server
    extends Thread {
  public final static int Default_Port = 6543;
  protected int port;
  protected ServerSocket listen_socket;

  //add 12.25.04
  public static boolean Server_Flag = true;

  public static Frame1 fframe;

// 定义出错例程:如果出现异常错误,退出程序。
  public static void fail(Exception e, String msg) {
    System.err.println(msg + ": " + e);
    System.exit(1);
  }

// 定义并启动服务器的Socket 例程,监听客户机的连接请求。
  public Server(int port) {
    if (port == 0) port = Default_Port;
    this.port = port;
    this.fframe = null;
    try {
      listen_socket = new ServerSocket(port);
    }
    catch (IOException e) {
      fail(e, "Exception creating server socket");
    }
    System.out.println("Server: listening on port" + port);
    this.start();
  }

  public Server(int port, Frame1 ff) {
    if (port == 0) port = Default_Port;
    this.port = port;
    this.fframe = ff;
    try {
      listen_socket = new ServerSocket(port);
    }
    catch (IOException e) {
      fail(e, "Exception creating server socket");
    }
    System.out.println("Server: listening on port" + port);
    this.start();
  }

  /* 下面为服务器监听线程的主程序。该线程一直循环执行,监听并接受客户机发出的连接
   请求。对每一个连接,均产生一个连接对象与之对应,通过Socket 通道进行通信。*/

  public void run() {
    try {
      while (Server_Flag) {
        Socket client_socket = listen_socket.accept();
        Connection c = new Connection(client_socket);
      }
      System.out.println("Server:" + port + "is finished");
    }
    catch (IOException e) {
      fail(e, "Exception while listening for connections");
    }
  }

// 启动服务器主程序

  public static void main(String args[]) {
    int port = 0;
    if (args.length == 1) {
      try {
        port = Integer.parseInt(args[0]);
      }
      catch (NumberFormatException e) {
        port = 0;
      }
    }
    new Server(port);
  } // End of the main

  public static void EndServer() {
    Server_Flag = false;
//    Client client
  }

} // End of Server class

//以下定义了Connection 类,它是用来处理与客户机的所有通信的线程。
class Connection
    extends Thread {
  protected Socket client;
  protected DataInputStream in;
  protected PrintStream out;

// 初始化通信流并启动线程
  public Connection(Socket client_socket) {
    client = client_socket;

    try {
      in = new DataInputStream(client.getInputStream());
      out = new PrintStream(client.getOutputStream());
    }
    catch (IOException e) {
      try {
        client.close();
      }
      catch (IOException e2) {
        ;
      }

      System.err.println("Exception while getting socket streram: " + e);
      return;
    }

    this.start();
  } // End of Connection method

// 服务例程:读出一行文本;反转文本;返回文本。
  public void run() {
    String line;
    StringBuffer revline;
    int len;

    try {
      for (; ; ) {
        // Read a line
        line = in.readLine();
        if (line == null) {
          break;
        }
        System.out.println(line);

        Frame1.sendShortMessage("发现不明物体", "13936181240");
//        Frame1.sendShortMessage("发现不明物体", "13945667841");

        // Reverse the line
        len = line.length();
        revline = new StringBuffer(len);

        for (int i = len - 1; i >= 0; i--) {
          revline.insert(len - 1 - i, line.charAt(i));
        }

        // Write out the reverse line
        out.println(revline);

//        System.out.println(revline);
      }
    }
    catch (IOException e) {
      ;
    }

    try {
      client.close();
    }
    catch (IOException e2) {
      ;
    }

  } // End of run method
} // End of Connection class

⌨️ 快捷键说明

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