java中的TCP SERVER代码

package tcpserver;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.net.*;
import java.io.*;
/**
 * <p>Title: TCPServer</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Frame1 extends JFrame implements Runnable
{
  ServerSocket server=null;
    Socket socket=null;
    BufferedReader in=null;
    PrintWriter out=null;
    InetAddress myServer=null;
  JPanel contentPane;
  XYLayout xYLayout1 = new XYLayout();
  TextArea textArea1 = new TextArea();
  TextField textField1 = new TextField();
  Label label1 = new Label();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();

  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    Thread thread=new Thread(this);
    thread.start();
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("服务器端");
    label1.setText("消息:");
    textArea1.setText("");
    jButton1.setActionCommand("");
    jButton1.setText("发送消息");
    jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    jButton2.setText("退出");
    jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
    textField1.addKeyListener(new Frame1_textField1_keyAdapter(this));
    textField1.addActionListener(new Frame1_textField1_actionAdapter(this));
    contentPane.add(textField1,  new XYConstraints(78, 222, 274, 32));
    contentPane.add(textArea1,   new XYConstraints(3, 1, 395, 214));
    contentPane.add(label1,        new XYConstraints(37, 227, 75, 23));
    contentPane.add(jButton2,   new XYConstraints(290, 260, -1, -1));
    contentPane.add(jButton1, new XYConstraints(38, 259, -1, -1));
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
public void run()
  {
    try{
      server=new ServerSocket(5438);
      socket=server.accept();
      in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
      out=new PrintWriter(socket.getOutputStream());
      if(socket!=null)
      {
        textArea1.append("system information:client have joined!\n");
        jButton1.setEnabled(true);
      }
      receiver r=new receiver();
      Thread t=new  Thread(r);
      t.start();
       }
       catch(Exception e)
       {
         textArea1.append(e.toString()+"\n");
       }
  }
private class receiver implements Runnable
  {
    public void run()
    {
      String s1=null;
      try
      {
        s1=in.readLine();
        while(s1!="client exit!")
        {
          textArea1.append("client information:"+s1+"\n");
          s1=in.readLine();
        }
        in.close();
        out.close();
        socket.close();
        server.close();
      }
      catch(Exception e)
      {
        jButton1.setEnabled(false);
      }
    }
  }
  void jButton1_actionPerformed(ActionEvent e) {
        try {
          out.println(textField1.getText());
          out.flush();
          textArea1.append("server information:" + textField1.getText() + "\n");
          textField1.setText("");
        }
        catch (Exception ex) {
        }
      }

  void jButton2_actionPerformed(ActionEvent e) {
 try{
      out.print("server exit!");
      out.flush();
    }
    catch(Exception e2)
    {

    }
    finally{
      System.exit(0);
           }
  }

  void textField1_keyPressed(KeyEvent e) {
int j=e.getKeyCode();
    if(j==e.VK_ENTER)
    {
      out.println(textField1.getText());
      out.flush();
      textArea1.append("server information:"+textField1.getText()+"\n");
      textField1.setText("");
    }
  }

  void textField1_actionPerformed(ActionEvent e) {
    try {
      out.println(textField1.getText());
      out.flush();
      textArea1.append("server information:" + textField1.getText() + "\n");
      textField1.setText("");
    }
    catch (Exception ex) {
    }

  }
  }

 

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton2_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class Frame1_textField1_keyAdapter extends java.awt.event.KeyAdapter {
  Frame1 adaptee;

  Frame1_textField1_keyAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void keyPressed(KeyEvent e) {
    adaptee.textField1_keyPressed(e);
  }
}

class Frame1_textField1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_textField1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.textField1_actionPerformed(e);
  }
}

posted @ 2006-04-08 13:38  Tnu  阅读(921)  评论(0编辑  收藏  举报