Java服务器与客户端的通信

Java服务器与客户端的通信,java,server,Java,Server,我对java中的服务器概念是全新的。我已经设置了一个非常简单的服务器(大部分是从网络复制的),我希望它以什么“状态”发送,但我希望它只发送一次。这是我的代码,可能更清楚 服务器: package components; import java.io.DataInputStream; import java.io.PrintStream; import java.io.IOException; import java.net.Socket; import java.net.ServerS

我对java中的服务器概念是全新的。我已经设置了一个非常简单的服务器(大部分是从网络复制的),我希望它以什么“状态”发送,但我希望它只发送一次。这是我的代码,可能更清楚

服务器:

    package components;

import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;

public class mainServer {

  // The server socket.
  private static ServerSocket serverSocket = null;
  // The client socket.
  private static Socket clientSocket = null;

  // This chat server can accept up to maxClientsCount clients' connections.
  private static final int maxClientsCount = 3;
  private static final clientThread[] threads = new clientThread[maxClientsCount];

  public static void main(String args[]) {

    // The default port number.
    int portNumber = 3333;
    if (args.length < 1) {
      System.out
          .println("Usage: java MultiThreadChatServer <portNumber>\n"
              + "Now using port number=" + portNumber);
    } else {
      portNumber = Integer.valueOf(args[0]).intValue();
    }

    /*
     * Open a server socket on the portNumber (default 2222). Note that we can
     * not choose a port less than 1023 if we are not privileged users (root).
     */
    try {
      serverSocket = new ServerSocket(portNumber);
    } catch (IOException e) {
      System.out.println(e);
    }

    /*
     * Create a client socket for each connection and pass it to a new client
     * thread.
     */
    while (true) {
      try {
        clientSocket = serverSocket.accept();
        int i = 0;
        for (i = 0; i < maxClientsCount; i++) {
          if (threads[i] == null) {
            (threads[i] = new clientThread(clientSocket, threads)).start();
            break;
          }
        }
        if (i == maxClientsCount) {
          PrintStream os = new PrintStream(clientSocket.getOutputStream());
          os.println("Server too busy. Try later.");
          os.close();
          clientSocket.close();
        }
      } catch (IOException e) {
        System.out.println(e);
      }
    }
  }
}

/*
 * The chat client thread. This client thread opens the input and the output
 * streams for a particular client, ask the client's name, informs all the
 * clients connected to the server about the fact that a new client has joined
 * the chat room, and as long as it receive data, echos that data back to all
 * other clients. When a client leaves the chat room this thread informs also
 * all the clients about that and terminates.
 */
class clientThread extends Thread {

  private DataInputStream is = null;
  private PrintStream os = null;
  private Socket clientSocket = null;
  private final clientThread[] threads;
  private int maxClientsCount;

  public clientThread(Socket clientSocket, clientThread[] threads) {
    this.clientSocket = clientSocket;
    this.threads = threads;
    maxClientsCount = threads.length;
  }

  public void run() {
    int maxClientsCount = this.maxClientsCount;
    clientThread[] threads = this.threads;

    try {
      /*
       * Create input and output streams for this client.
       */
      is = new DataInputStream(clientSocket.getInputStream());
      os = new PrintStream(clientSocket.getOutputStream());
      os.println("Enter your name.");

      while (true) {
        String line = is.readLine();
        if (line.startsWith("/quit")) {
            break;
        }
        if (line.startsWith("Home")) {
            os.println("Server in Home mode");
        }
        if (line.startsWith("Search / Modify")){
            System.out.println("Server in search and modify mo");
        }
      }
      for (int i = 0; i < maxClientsCount; i++) {
        if (threads[i] != null && threads[i] != this) {
          threads[i].os.println("*** The user "
              + " is leaving the chat room !!! ***");
        }
      }
      os.println("*** Bye " + " ***");

      /*
       * Clean up. Set the current thread variable to null so that a new client
       * could be accepted by the server.
       */
      for (int i = 0; i < maxClientsCount; i++) {
        if (threads[i] == this) {
          threads[i] = null;
        }
      }

      /*
       * Close the output stream, close the input stream, close the socket.
       */
      is.close();
      os.close();
      clientSocket.close();
    } catch (IOException e) {
    }
  }
}
封装组件;
导入java.io.DataInputStream;
导入java.io.PrintStream;
导入java.io.IOException;
导入java.net.Socket;
导入java.net.ServerSocket;
公共类主服务器{
//服务器套接字。
私有静态ServerSocket ServerSocket=null;
//客户端套接字。
私有静态套接字clientSocket=null;
//此聊天服务器最多可以接受maxClientsCount客户端的连接。
私有静态final int MaxClientScont=3;
私有静态最终clientThread[]线程=新clientThread[MaxClientScont];
公共静态void main(字符串参数[]){
//默认端口号。
int端口号=3333;
如果(参数长度<1){
系统输出
.println(“用法:java多线程聊天服务器\n”
+“正在使用端口号=“+portNumber”);
}否则{
portNumber=Integer.valueOf(args[0]).intValue();
}
/*
*打开端口号上的服务器套接字(默认2222)。请注意,我们可以
*如果我们不是特权用户(root),请不要选择小于1023的端口。
*/
试一试{
serverSocket=新的serverSocket(端口号);
}捕获(IOE异常){
系统输出打印ln(e);
}
/*
*为每个连接创建一个客户端套接字,并将其传递给新的客户端
*线。
*/
while(true){
试一试{
clientSocket=serverSocket.accept();
int i=0;
对于(i=0;i
主要内容: 因此,此时它不断地通过输出流发送单词home,然后服务器发送回字符串“服务器处于home状态”,然后打印出来。如何让它只发送一次,这对于我能够检测gui上按下的按钮并在按下时向服务器发送命令非常有用

package components;

//Import all libraries
import java.awt.EventQueue;

public class MainFrame implements Runnable {
    //declare Jpanel
    private static JFrame frmHome;

    // The client socket
    private static Socket clientSocket = null;
    // The output stream
    private static PrintStream os = null;
    // The input stream
    private static DataInputStream is = null;
    private static BufferedReader inputLine = null;
    private static boolean closed = false;

    public static void main(String[] args){
        // The default port.
        int portNumber = 3333;
        // The default host.
        String host = "localhost";

          System.out
              .println("Usage: java MultiThreadChatClient <host> <portNumber>\n"
                  + "Now using host=" + host + ", portNumber=" + portNumber);
        /*
         * Open a socket on a given host and port. Open input and output streams.
         */
        try {
          clientSocket = new Socket(host, portNumber);
          inputLine = new BufferedReader(new InputStreamReader(System.in));
          os = new PrintStream(clientSocket.getOutputStream());
          is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
          System.err.println("Don't know about host " + host);
        } catch (IOException e) {
          System.err.println("Couldn't get I/O for the connection to the host "
              + host);
        }

        /*
         * If everything has been initialized then we want to write some data to the
         * socket we have opened a connection to on the port portNumber.
         */
        if (clientSocket != null && os != null && is != null) {
          try {

            /* Create a thread to read from the server. */
            new Thread(new MainFrame()).start();
            while (!closed) {
              os.println("Home");
            }
            /*
             * Close the output stream, close the input stream, close the socket.
             */
            os.close();
            is.close();
            clientSocket.close();
          } catch (IOException e) {
            System.err.println("IOException:  " + e);
          }
        }
      }

      /*
       * Create a thread to read from the server. (non-Javadoc)
       * 
       * @see java.lang.Runnable#run()
       */
      public void run() {
        /*
         * Keep on reading from the socket till we receive "Bye" from the
         * server. Once we received that then we want to break.
         */
          MainFrame window = new MainFrame();
          MainFrame.frmHome.setVisible(true);
        String responseLine;
        try {
          while ((responseLine = is.readLine()) != null) {
            System.out.println(responseLine);
            if (responseLine.indexOf("*** Bye") != -1)
              break;
          }
          closed = true;
        } catch (IOException e) {
          System.err.println("IOException:  " + e);
        }
        }

    public MainFrame() {
        initialize();
    }

    //function to make window visible
    void setVisible() {
        main(null);
    }

    private void initialize() {
        //Initialise Main window with 3 options.
        frmHome = new JFrame();
        frmHome.setTitle("Home");
        frmHome.setBounds(100, 100, 300, 372);
        frmHome.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmHome.getContentPane().setLayout(null);
        frmHome.setResizable(false);

        JLabel lblWelcomeToSrs = new JLabel("Welcome to SRS");
        lblWelcomeToSrs.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblWelcomeToSrs.setBounds(86, 183, 112, 14);
        frmHome.getContentPane().add(lblWelcomeToSrs);

        //initialise all buttons and labels of window.
        JButton btnAdStu = new JButton("Add a student");
        btnAdStu.setBounds(10, 207, 126, 23);
        btnAdStu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                AddStudentFrame adus;
                try {
                    adus = new AddStudentFrame();
                    adus.setVisible();
                    frmHome.setVisible(false);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        });
        frmHome.getContentPane().add(btnAdStu);

        JButton btnCheckStud = new JButton("Search / Modify");
        btnCheckStud.setBounds(146, 207, 127, 23);
        btnCheckStud.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                SearchFrame searchFrame;
                searchFrame = new SearchFrame();
                searchFrame.setVisible();
            }
        });
        frmHome.getContentPane().add(btnCheckStud);

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(0, 0, 0, 0);
        frmHome.getContentPane().add(lblNewLabel);

        JLabel lblCreatedByRmi = new JLabel("Created by R\u00E9mi Tuyaerts");
        lblCreatedByRmi.setBounds(147, 318, 184, 14);
        frmHome.getContentPane().add(lblCreatedByRmi);  

        JButton btnNewButton = new JButton("Complete List of Students");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                CompleteListFrame studentList = new CompleteListFrame();
                frmHome.setVisible(false);
                studentList.setVisible();
            }
        });
        btnNewButton.setBounds(52, 241, 184, 23);
        frmHome.getContentPane().add(btnNewButton);

        // Button to set up the database
        JButton btnFillInDatabase = new JButton("Fill in database");
        btnFillInDatabase.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                StudentStoring.student1();
                StudentStoring.student2();
                StudentStoring.student3();
                StudentStoring.student4();
            }
        });
        btnFillInDatabase.setBounds(80, 276, 126, 23);
        frmHome.getContentPane().add(btnFillInDatabase);

        // wonderful pictures of his excellence design by Yasser
        JLabel lblNewLabel_1 = new JLabel("");
        Image img = new ImageIcon(frmHome.getClass().getResource("/michaelchung.jpg")).getImage();
        lblNewLabel_1.setIcon(new ImageIcon(img));
        lblNewLabel_1.setBounds(80, 11, 120, 148);
        frmHome.getContentPane().add(lblNewLabel_1);
    }
}
封装组件;
//导入所有库
导入java.awt.EventQueue;
公共类大型机实现可运行{
//声明Jpanel
私有静态JFrame-frmHome;
//客户端套接字
私有静态套接字clientSocket=null;
//输出流
私有静态PrintStream os=null;
//输入流
私有静态DataInputStream为空;
私有静态BufferedReader inputLine=null;
私有静态布尔闭合=false;
公共静态void main(字符串[]args){
//默认端口。
int端口号=3333;
//默认主机。
String host=“localhost”;
系统输出
.println(“用法:java多线程ChatClient\n”
+“现在正在使用host=“+host+”,portNumber=“+portNumber”);
/*
*打开给定主机和端口上的套接字。打开输入和输出流。
*/
试一试{
clientSocket=新套接字(主机、端口号);
inputLine=新的BufferedReader(新的InputStreamReader(System.in));
os=新的打印流(clientSocket.getOutputStream());
是=