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

📄 serverchat.java

📁 一个用JAVA做的可以在局域网聊天的程序
💻 JAVA
字号:
/*
 * serverChat.java
 *
 * Last modified on 17 february 2004, 19:33
 */

import java.io.*; // for input output
import java.lang.*; // for Threads
import java.net.*; // sockets

/**
 * @version 1.6
 * @author  Julien GARINO
 */

public class serverChat {

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

    /*******Declarations**********/
    // Ports and Hosts
    int port = 3000; // port to the PDA client

    // Sockets
    ServerSocket serverSocket = null; // ServerSocket listening to the Client
    Socket clientSocket = null; // Socket with Client

    // Inout/Output for PDA Client
    BufferedReader isClient = null; // BufferedReader from PDA Client
    PrintWriter osClient = null; // OutputStream to PDA Client

    // Strings
    String msg_String = null;
    int msg_hash = 0;

    int i = 0;
    int Clientconnected = 0; // Connection marker
    /********End declarations*********/

    /****************Server Socket Creation**********/
    System.out.print(
        "\nCreation of the ServerSocket\nlistening to port 3000\nfor client.");
    try {
      System.out.print(".");
      serverSocket = new ServerSocket(port); // Creation of the server Socket listening on port 3000
      System.out.print("..started.\n\n");
    }
    catch (IOException e) {
      System.out.print(
          "...failed.\nProblem in the creation of the ServerSocket : " + e);
      System.exit(1);
    }
    try {
      clientSocket = serverSocket.accept(); // now we are connected
      Clientconnected = 1; // we just mark here that we are connected
    }
    catch (IOException e) {
      System.out.println(
          "Unable to deal with BufferedReader and PrintWriter for the clientSocket : " +
          e);
      System.exit(1);
    }
    /***************End of creation of Sockets**********/

    try {
      isClient = new BufferedReader(new InputStreamReader(clientSocket.
          getInputStream())); // InputStream from Client
      osClient = new PrintWriter(clientSocket.getOutputStream()); // prepare an OutputStream for the Client
    }
    catch (IOException e) {
      System.out.println("Cant't deal with the streams : " + e);
    }

    while (true) {
      try {
        msg_String = isClient.readLine(); // taking the String from it
        System.out.println("String from the client : " + msg_String);
      }
      catch (IOException e) {
        System.out.println("Couldn't get I/O for the connection to: ");
        System.exit(1);
      }

      if (msg_String.trim().equals("/bye")) {
        Clientconnected = 0; // the server is now disconnected
        clientSocket.close();
        serverSocket.close();
        System.exit(0);
      }

      if (Clientconnected == 1) { // if the client is connected
        try {
          System.out.print("\nComputing...");
          msg_hash = msg_String.hashCode();
          System.out.print("done");
          osClient.println(msg_hash);
          osClient.flush(); // put it onto the network
          System.out.print("...and sent.\n");
        } //try
        catch (Exception e) {
          System.out.println(e);
          System.exit(1);
        } // catch
      } // if Clientconnected
    } // while
    // System.exit(0);
  } // void main
} // PDAServer class

⌨️ 快捷键说明

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