java Socket实现简单聊天室

时间:2025-01-30 07:07:08

这几天重拾java,整理资料正好发现3年前,用java写的一个很low 的聊天室,还是跟大家分享一下吧,自己也回顾回顾。

package ;


import .*;
import .*;
import .*;
import .*;
import .*;
import .*;

public class LoginFrame extends JFrame implements ActionListener
{
    JPanel          contentPane;
    JLabel          jLabel1=new JLabel();
    JTextField      jTextField1=new JTextField();
    ButtonGroup     buttonGroup1=new ButtonGroup();
    JRadioButton    jRadioButton1=new JRadioButton("男");
    JRadioButton    jRadioButton2=new JRadioButton("女");
    JLabel          jLabel2=new JLabel("性别");
    JButton         jButton1=new JButton();
    JButton         jButton2=new JButton(); 
    String          sex="男";
    String          name=null;
    boolean         bool=false;
    Socket          socket;
    DataInputStream  in;
    DataOutputStream  out;

    public LoginFrame()
    {
        setSize(new Dimension(400,210));
        setTitle("登录界面");
        setVisible(true);
        contentPane = (JPanel) getContentPane();
        (null);
        this.setResizable(false);
        (new Font("Dialog",15,15));
        ("请输入您的昵称");
        (53,20,113,44);
        (182,25,154,33);
        (new Font("Dialog",,13));
        (true);
        (186,64,91,25);
        (this);
        (new Font("Dialog",,13));
        (187,90,91,25);
        (this);
        (new Font("Dialog",,15));
        (136,80,34,16);
        (182,138,71,25);
        ("确定");
        (this);
        (264,138,71,25);
        ("取消");
        (this);
        (jLabel1);
        (jTextField1);
        (jRadioButton1);
        (jRadioButton2);
        (jLabel2);
        (jButton2);
        (jButton1);
        (jRadioButton1);
        (jRadioButton2);
        Dimension screenSize = ().getScreenSize();
        Dimension frameSize = getSize();
        if(>)
        {
            =;
        }
        if(>)
        {
            =;
        }
        setLocation(( - )/2,( - )/2);
        setVisible(true);
    }
    public void setClection(Socket soc,DataInputStream din,DataOutputStream dout)
    {
        socket=soc;
        in=din;
        out=dout;
    }
    public String getName()
    {
        return name;
    }
    public void actionPerformed(ActionEvent e)
    {
        if(() == jRadioButton1)  sex="男";
        if(() == jRadioButton2)  sex="女";
        if(() == jButton2)  ("");
        else if(() == jButton1)
        {
            if(().equals(""))
            {
                ("请输入昵称");
                return;
            }
            name=();
            try{
                ("新用户:"+name+"/"+sex);
            }catch(Exception ee){ return; }
        }
    }
}

package ;

import .*;
import .*;
import .*;
import .*;
import .*;
import .*;

public class chatFrame extends JFrame implements ActionListener,Runnable
{
    LoginFrame frame=new LoginFrame();
    Thread thread;
    JPanel contentPane;
    JScrollPane jScrollPane1=new JScrollPane();
    JScrollPane jScrollPane2=new JScrollPane();
    JButton jBtnSend = new JButton("发送");
    JButton jBtnClear = new JButton("清空");
    JTextArea jTextArea1=new JTextArea();
    JTextArea jTextArea2=new JTextArea();
    JScrollPane jScrollPane3=new JScrollPane();
    JList jList1 = new JList();
    Socket socket;
    int port =10000;
    DataInputStream in;
    DataOutputStream out;
    Vector vector=new Vector();
    String chatstr,str;

    public chatFrame()
    {
        setTitle("思维聊天室");
        setSize(602,515);
        this.setResizable(false);
        this.setVisible(false);
        try{
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          addWindowListener (new WindowAdapter()
          {
            public void windowClosing(WindowEvent e)
            {
                try{
                    ("下线了:"++"/"+);
                    ();
                    ();
                    ();
                }catch(Exception ee){  }
                finally
                {
                    ();
                    dispose();

                }
            }
          }
         );
        jbInit();

    }catch(Exception exception)
    { ();}

   }
   void jbInit() throws Exception
   {
       (true);
       (2,2,445,320);
       (1,333,448,95);
       (458,6,139,424);
       (93,441,78,31);
       (185,441,76,31);
       (false);
       contentPane = (JPanel)getContentPane();
       (null);
       (jScrollPane2);
       (jScrollPane1,null);
       (jScrollPane3);
       (jBtnSend);
       (jBtnClear);
       ().add(jTextArea1);
       ().add(jTextArea2);
       ().add(jList1);
       (this);
       (this);
       try{
           InetAddress address=();
           socket=new Socket((),port);
           in=new DataInputStream(());
           out=new DataOutputStream(());

       }catch(Exception e){
           setVisible(true);
           ("无法与服务器连接,请退出....");

       }
       if(thread == null)
       {
           thread=new Thread(this);
           ();
       }
       (socket,in,out);
     }
    public void actionPerformed(ActionEvent e)
    {
     if(()==jBtnSend)
     { sendStr();}
     if(()==jBtnClear)
     { ("");}
    }
    public void sendStr()
    {
        chatstr = ();
        if((""))
        {
            ("please say a word:");
            return;
        }
        try{
            ( + ":"+chatstr);
            ("");
        }catch(Exception ee) {  }

    }
    public void run()
    {
        String sl=null;
        String ss=null;
        String s=null;
        while(true)
        {
            try
            {
                sl=();
            }catch(Exception ee)
            {
                ();
                (vector);
                ("已经与服务器断开...");
                return;

            }
            if(("可以注册"))
            {
                (false);
                setVisible(true);
                setTitle("昵称..."+());
                continue;
            }
            else if(("该用户名已注册"))
            {
                frame.("请重新输入昵称");
                continue;
            }
            else if(("新用户"))
            {
                StringTokenizer st=new StringTokenizer(sl,":");
                ss=null;
                s=null;
                ss=();
                s=();
                (s);
                (vector);
                continue;
            }
            else if(("下线了"))
            {
                StringTokenizer st=new StringTokenizer(sl,":");
                ss=null;
                s=null;
                ss=();
                s=();
                (s);
                (vector);
                continue;
            }
            (sl+"\n");

        }
    }
}

package ;

import .*;
import .*;
import .*;

public class Chat{
    boolean  packFrame=false;
    Chat(){
        chatFrame frame=new chatFrame();
        Dimension screenSize=().getScreenSize();
        Dimension frameSize=();
        if(>)
        {
            =;
        }
        if(>)
        {
            =;
        }
        (( - )/2,( - )/2);
    }
public static void main(String[] args)
{
    (new Runnable()
    {
        public void run()
        {
            new Chat();}
    });
  }
}

package ;


import .*;
import .*;
import .*;

public class Server extends ServerSocket
{
   static final int SERVER_PORT=10000;
   Vector vector1=new Vector();
   Vector vector2=new Vector();

   public Server() throws IOException
  {
    super(SERVER_PORT);
    try{
         while(true)
        {
          ("等待客户连接...");
          Socket socket=accept();
          new CreateServerThread(socket,vector1,vector2);
        }
       }catch(IOException e){("客户连接错误");}
       finally
      {
         close();
      }
  }
  public static void main(String[] args)throws IOException
  {
       new Server();
  }
}
  class CreateServerThread extends Thread
 {
    Socket  socket;
    DataInputStream  in;
    DataOutputStream out;
    Vector   vector1;
    Vector   vector2;
    public boolean  bool=false;
    String     revStr=null;
    String     string=null;
    int        userID;
    String     str=null;
    Enumeration   enu;

    public CreateServerThread(Socket s,Vector vec1,Vector vec2)throws IOException
   {
     socket=s;
     vector1=vec1;
     vector2=vec2;
     out=new DataOutputStream(());
     in=new DataInputStream(());
     start();
   }

   public void run()
  {
     StringTokenizer st;
     StringTokenizer stc;
     try{
           while(true)
          {
                revStr=();
                if(("新用户"))
               {
                   if((revStr))
                   {
                       ("该用户已注册");
                   }
                  else
                  {
                      ("收到客户的登陆信息....."+revStr);
                      ("可以注册");
                      str=revStr;
                      (revStr);
                      Enumeration enu=();
                      while(())
                      {
                         ((String)());
                      }
                      bool=true;
                      Enumeration enuc=();
                      while(())
                     {
                         CreateServerThread th=(CreateServerThread)();
                         (revStr);
                      }
                      stc=new StringTokenizer(revStr,":");
                      string=();
                      string=();
                      Enumeration enuc1=();
                      while(())
                      {
                             CreateServerThread th=(CreateServerThread)();
                             (string+"...上线了");
                      }
                      (this);
                  }
               }
               else if(("下线了")||())
               {
                   st=new StringTokenizer(revStr,":");
                   string=();
                   string=();
                   userID=(str);
                   (userID);
                   Enumeration enu=();
                   while(())
                  {
                       CreateServerThread th=(CreateServerThread)();
                       if(th!=this&&())
                           ("下线了:"+str.substring(str.indexOf(":")+1));
                       (str.substring(str.indexOf(":")+1)+"下线了");
                  }
                  (this);
                  break;
               }
               else
               {
                     enu=();
                     while(())
                     {
                             CreateServerThread th=(CreateServerThread)();
                             (revStr);
                     }
               }
          }
      }
      catch(IOException e)
      {
          try{
                userID=(str);
                (userID);
                Enumeration enun=();
                while(())
                {
                       CreateServerThread th=(CreateServerThread)();
                       if(th!=this&&())
                       ("下线了:"+str.substring(str.indexOf(":")+1));
                       (str.substring(str.indexOf(":")+1)+"...下线了");
                }
                (this);
                return;
             }catch(Exception ee){return;}
       }
  }
 }     

看到自己大三的程序还是倍感亲切的,我也米有运行,有错的地方,欢迎指正。毕竟*年没写过java啦!