simpleserver.java

来自「java网络高级编程的配套源码,java网络高级编程为清华出版社出版.」· Java 代码 · 共 143 行

JAVA
143
字号
/*源程序清单12-1*/

package server;

import java.awt.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;

public class SimpleServer extends Frame 
{
  protected ServerSocket listenSock;
  GroupBox groupBox1 = new GroupBox();
  Button button1 = new Button();
  Button button2 = new Button();
  PaneLayout paneLayout1 = new PaneLayout();
  List list1 = new List();
  PaneLayout paneLayout2 = new PaneLayout();

  public SimpleServer(int listenPort) throws Exception 
  {
    listenSock=new ServerSocket(listenPort);
    try 
{
      jbInit();
    }
    catch (Exception e) 
{
      e.printStackTrace();
    }
  }

  public void waitForClients()
  {
    while(true)
{
      try
  {
        Socket newClient=listenSock.accept();
        newConn = new ServerConn(this,newClient);
        users.addElement(newConn);
      }
  catch(Exception bad)
  {
        bad.printStackTrace();
      }
    }
  }

  public synchronized String processString(String instr)
  {
    return instr;
  }

  public static void main(String[] args) 
  {
    try
    {
      SimpleServer simpleServer = new SimpleServer(4321);
      simpleServer.setLocation(100,50);
      simpleServer.setSize(400,300);
      simpleServer.show();
      simpleServer.waitForClients();
    }
catch(Exception e)
{
      System.out.println("GotErrotr");
      e.printStackTrace();
    }
  }
  
  public SimpleServer() {
    try  
{
      jbInit();
    }
    catch (Exception e) 
{
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception 
  {
    this.setLayout(paneLayout1);
    this.setSize(new Dimension(503, 354));
    this.setTitle("服务器端");
    groupBox1.setLayout(paneLayout2);
    groupBox1.setLabel("在线用户");
    button1.setLabel("关闭");
    button1.addActionListener(new SimpleServer_button1_actionAdapter(this));
    button2.setLabel("踢出");
    button2.addActionListener(new SimpleServer_button2_actionAdapter(this));
    groupBox1.add(list1, new PaneConstraints("list1", "list1", PaneConstraints.ROOT, 1.0f));
    this.add(groupBox1, new PaneConstraints("groupBox1", "groupBox1", PaneConstraints.ROOT, 0.5f));
    this.add(button2, new PaneConstraints("button2", "groupBox1", PaneConstraints.BOTTOM, 0.1469534f));
    this.add(button1, new PaneConstraints("button1", "button2", PaneConstraints.RIGHT, 0.5303644f));
  }

  void button2_actionPerformed(ActionEvent e) 
  {
  }
  
  void button1_actionPerformed(ActionEvent e) 
  {
    System.exit(0);
  }
}

class SimpleServer_button2_actionAdapter implements java.awt.event.ActionListener 
{
  SimpleServer adaptee;

  SimpleServer_button2_actionAdapter(SimpleServer adaptee) 
{
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) 
{
    adaptee.button2_actionPerformed(e);
  }
}

class SimpleServer_button1_actionAdapter implements java.awt.event.ActionListener 
{
  SimpleServer adaptee;

  SimpleServer_button1_actionAdapter(SimpleServer adaptee) 
{
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) 
{
    adaptee.button1_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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