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

📄 clientchat.java

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

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

/**
 * @version 1.8
 * @author  Julien GARINO
 */

public class clientChat {

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

    /*******Declarations**********/
    // Ports and Hosts
    int port = 3000; // port
    String myHost = "localhost"; // just change it with args[0] and will work through the network

    // Sockets
    Socket clientSocket = null; // Socket with PDA Client

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

    // Strings
    String myString = null;
    String sString = null;
    int i = 0; // Translation counter
    int Clientconnected = 0; // Connection marker
    /********End declarations*********/

    try {
      System.out.print("\nClient.");
      clientSocket = new Socket(myHost, port);
      System.out.print("..started.\n\n");
    }
    catch (IOException e) {
      System.out.print("..failed.\nProblem in the creation of the Socket : " +
                       e);
      System.exit(1);
    } //catch

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

    try {
      while (true) {
        myString = readfromline.readLine();
        if (myString.trim().equals("/bye")) {
          osClient.println("/bye");
          osClient.flush();
          clientSocket.close();
          System.exit(1);
        }

        osClient.println(myString); // Write it
        osClient.flush(); // put it onto the network
        System.out.println("\nSending...: '" + myString + "' ...done.");
        myString = isClient.readLine();
        System.out.println("From server :-[===> " + myString + "\n");
      } //while
    } //try
    catch (Exception e) {
      System.out.println("Exception while sending data" + e);
      System.exit(1);
    } // catch
    System.out.println("Closing connection to client");
    // System.exit(0);
  } // void main
} // clientChat class

⌨️ 快捷键说明

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