回覆列表
  • 1 # V小本創業V

    //伺服器端程式碼

    import java.awt.FlowLayout;

    import java.io.DataInputStream;

    import java.io.DataOutputStream;

    import java.io.IOException;

    import java.net.ServerSocket;

    import java.net.Socket;

    import java.util.ArrayList;

    import java.util.Collection;

    import java.util.Iterator;

    import javax.swing.JFrame;

    import javax.swing.JScrollPane;

    import javax.swing.JTextArea;

    public class QLServer extends JFrame{

    public JTextArea jtextarea = null;

    public void lanuchFrame(String str){

    this.setName(str);

    init();

    }

    private void init() {

    setLayout(new FlowLayout());

    jtextarea =new JTextArea(20, 17);

    jtextarea.setLineWrap(true);

    jtextarea.setEditable(false);

    this.getContentPane().add(new JScrollPane(jtextarea));

    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(200,400);

    setLocationRelativeTo(null);

    setResizable(false);

    }

    ServerSocket server = null;

    Collection cClients = new ArrayList<ClientConn>();//加個泛型

    public void startServer() throws IOException{

    while(true){

    Socket s = server.accept();

    cClients.add(new ClientConn(s));

    jtextarea.append("new client login" + s.getInetAddress() + ":" + s.getPort()+"\n");

    }

    }

    public QLServer(int port,String str) throws IOException{

    server = new ServerSocket(port);

    lanuchFrame(str);

    }

    class ClientConn implements Runnable

    {

    Socket s = null;

    public ClientConn(Socket s)

    {

    this.s = s;

    (new Thread(this)).start();

    }

    public void send(String str) throws IOException

    {

    DataOutputStream dos = new DataOutputStream(s.getOutputStream());

    dos.writeUTF(str);

    }

    public void dispose()//客戶端下線

    {

    try {

    if (s != null) s.close();

    cClients.remove(this);

    jtextarea.append("A client out! \n");

    jtextarea.append("client count: " + cClients.size() + "\n\n");

    }

    catch (Exception e)

    {

    e.printStackTrace();

    }

    }

    public void run()

    {

    try {

    DataInputStream dis = new DataInputStream(s.getInputStream());

    String str = dis.readUTF();

    while(str != null && str.length() !=0)

    {

    System.out.println(str);

    for(Iterator it = cClients.iterator(); it.hasNext(); )

    {

    ClientConn cc = (ClientConn)it.next();

    if(this != cc)

    {

    cc.send(str+" "+s.getInetAddress().getHostName());

    }

    }

    str = dis.readUTF();//少了這句話會無限輸出

    //send(str);

    }

    this.dispose();

    }

    catch (Exception e)

    {

    this.dispose();

    }

    }

    }

    public static void main(String[] args) {

    try {

    QLServer qlserver = new QLServer(8888,"QLServer");

    qlserver.startServer();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }import java.awt.FlowLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.io.BufferedReader;

    import java.io.DataInputStream;

    import java.io.DataOutputStream;

    import java.io.IOException;

    import java.io.InputStreamReader;

    import java.net.InetAddress;

    import java.net.Socket;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JScrollPane;

    import javax.swing.JTextArea;

    //客戶端程式碼

    public class QLClient extends JFrame implements ActionListener{

    public JTextArea jtextarea1 = null;

    public JTextArea jtextarea2 = null;

    public JButton button = null;

    Socket s =null;

    public void launchFrame(String str){

    this.setName(str);

    init();

    }

    public QLClient(String str) throws IOException{

    launchFrame(str);

    s = new Socket("127.0.0.1",8888);//哪臺電腦做伺服器,IP地址改成那臺機子的IP

    (new Thread(new ServeConn())).start();

    }

    private void init() {

    setLayout(new FlowLayout());

    jtextarea1 =new JTextArea(17, 16);

    jtextarea2 =new JTextArea(4, 16);

    jtextarea1.setLineWrap(true);

    jtextarea1.setEditable(false);

    jtextarea2.setLineWrap(true);

    button = new JButton("傳送");

    button.addActionListener(this);//繫結button事件

    this.getContentPane().add(new JScrollPane(jtextarea1));

    this.getContentPane().add(new JScrollPane(jtextarea2));

    add(button);

    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(200,470);

    setLocationRelativeTo(null);

    setResizable(false);

    }

    public void send(String str) throws IOException{

    DataOutputStream dos = new DataOutputStream(s.getOutputStream());

    dos.writeUTF(str);

    }

    public void actionPerformed(ActionEvent e) {

    if(e.getSource()==button){

    String sendStr = jtextarea2.getText();

    if(sendStr.trim().length()==0){

    return;

    }

    try {

    this.send(sendStr);

    jtextarea2.setText("");

    InetAddress a;

    a = InetAddress.getLocalHost();

    String hostname = a.getHostName();

    jtextarea1.append(sendStr+"("+hostname+")"+"\n");

    } catch (IOException e1) {

    // TODO Auto-generated catch block

    e1.printStackTrace();

    }

    }

    }

    class ServeConn implements Runnable{

    public void run() {

    if(s == null) return;

    try {

    DataInputStream dis = new DataInputStream(s.getInputStream());

    String str = dis.readUTF();

    while (str != null && str.length() != 0)

    {

    //System.out.println(str);

    QLClient.this.jtextarea1.append(str + "\n");//內部類用外類中的變數或方法加外類名

    str = dis.readUTF();

    }

    }

    catch (Exception e)

    {

    e.printStackTrace();

    }

    }

    }

    //main主函式入口

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

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    QLClient qlclient = new QLClient("QLClient");

    String str = br.readLine();

    while(str!=null&&str.length()!=0){

    qlclient.send(str);

    str = br.readLine();//防止死迴圈

    }

    qlclient.s.close();

    }

    }

  • 中秋節和大豐收的關聯?
  • 波特五力模型?