Echoserver限于许多具有GUI的客户端

时间:2019-05-15 09:40:41

标签: java echo-server

我正在用GUI编写echoserver Java程序(服务器和客户端)。我完成了代码;但是,我遇到了以下四个问题:

  1. 将与服务器的开放客户端连接数限制为最大。
  2. 服务器应该在每个新的或关闭的连接之后给出一条消息,指定剩余的允许连接。
  3. 服务器还应该向每个拒绝的客户端发送一条消息(超过最大连接数)。
  4. 服务器的GUI无法与我一起使用。

以下是客户端和服务器代码。

//echoserver Code
import java.net.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.*;

public class EchoServer {
    boolean quite=false;
     final AtomicInteger runningCount = new AtomicInteger(0);
 final Integer limit = 1;

ServerSocket m_ServerSocket;
public EchoServer() {
    try {
        m_ServerSocket = new ServerSocket(12111);


    System.out.println("Listening for clients on 12111....");
    int id = 0;


    while (!quite) {


            Socket clientSocket = m_ServerSocket.accept();
            if (runningCount.incrementAndGet() < limit){
            ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++ ,runningCount::decrementAndGet);
            cliThread.start();
            }
        else {
            runningCount.decrementAndGet();
            clientSocket.close();
            System.out.println("limit exceeded");}

            } }catch (IOException ioe) {
            System.out.println("Exception encountered on accept.Ignoring.StackTrace :");
            ioe.printStackTrace();
        }
   }

public static void main(String args[]) {
    new EchoServer();
}
interface Callback {
       void call();
    }
class ClientServiceThread extends Thread {
    Socket m_clientSocket;
    int m_clientID = -1;
    final Callback callbackOnFinish;
    boolean m_bRunThread = true;

    ClientServiceThread(Socket s, int clientID, Callback callbackOnFinish) {
        m_clientSocket = s;
        m_clientID = clientID;
        this.callbackOnFinish = callbackOnFinish;
    }
    public void run() {
        BufferedReader in = null;
        PrintWriter out = null;
        System.out.println("Accepted Client :ID - " + m_clientID + " :Address - " + m_clientSocket.getInetAddress().getHostName());
        try {

            in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
            out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
            while (m_bRunThread) {
                String clientCommand = in.readLine();
                System.out.println("Client says :" + clientCommand);
                if (clientCommand.equalsIgnoreCase("quit")) {
                    m_bRunThread = false;
                    System.out.println("Stopping client thread for client :" + m_clientID);
                    System.exit(0);
                } else {
                    out.println(clientCommand);
                    out.flush();
                }
            }



        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
                m_clientSocket.close();
                System.out.println("...STOPPED");
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
                callbackOnFinish.call();
            }
        }
    }
}


//Client Code
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class client extends JFrame {
    JTextArea clientText;
    JTextField msg;
    JPanel clip;
    JScrollPane clientScroll;
    JButton closeButton = new JButton("Close");
    JButton send = new JButton("Send");
    JLabel label1 = new JLabel("Echo Client - Multithreaded");
    Container cont;
client() {
    cont = getContentPane();
    setSize(250, 500);
    setTitle("GUI Client");
    clip = new JPanel();
    msg = new JTextField(20);
    clip.setLayout(new FlowLayout(FlowLayout.CENTER));
    clientText = new JTextArea(20, 20);
    clientScroll = new JScrollPane(clientText);
    clip.add(label1);
    clip.add(clientText);
    clip.add(msg);
    clip.add(send);
    clip.add(closeButton);
    cont.add(clip);
    send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Socket s = null;
            try {
                s = new Socket("localhost", 12111);
            } catch (UnknownHostException uhe) {
                clientText.setText("UnknownHost: + localhost");
                s = null;
            } catch (IOException ioe) {
                clientText.setText("Cant connect to server at 12111.Make sure it is running");
                s = null;
            }
            if (s == null) {
                System.exit(-1);
            }
            BufferedReader in = null;
            PrintWriter out = null;
            try {
                in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
                out.println(msg.getText());
                out.flush();
                String temp = clientText.getText();
                if (temp.equalsIgnoreCase("quit")) {
                    System.exit(0);
                }
                msg.setText("");
                clientText.append("\nServer says:" + in.readLine());
            } catch (IOException ioe) {
                clientText.setText("Exception during communication.Server probably closed connection.");
            } finally {
                try {
                    out.close();
                    in.close();
                    s.close();
                } catch (Exception es) {
                    es.printStackTrace();
                }
            }
        }
    });
    closeButton.addActionListener(
            new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
}
public static void main(String args[]) {
    client clientFrame = new client();
    clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    clientFrame.setVisible(true);
}

}

0 个答案:

没有答案