⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 此聊天室可以实现服务器端与客户端的信息交流
💻 JAVA
字号:

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server
{  public static void main(String args[])
   {  new A("小猫聊天室");
   }
}
class A extends JFrame implements ActionListener ,Runnable
{
	JTextArea main,part;
	JLabel zhu,fu;
	JButton sent;
	JScrollPane aa,bb;
	JTextField show1,show2;
	Socket you=null;
   DataInputStream in=null;
   DataOutputStream out=null;
   Thread thread1; 
    ServerSocket server=null;
	A(String s)
	{super(s);
		setBounds(10,30,620,620);
 	  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		thread1=new Thread(this);
		you=new Socket();
	  
		main=new JTextArea("");
		main.setEditable(false);
		main.setLineWrap(true);
		aa=new JScrollPane(main);
		part=new JTextArea();
		
		part.setLineWrap(true);
		bb=new JScrollPane(part);
		zhu=new JLabel(new ImageIcon("dog.jpg"));
		fu=new JLabel(new ImageIcon("cat.jpg"));
	
		sent=new JButton("发送信息");
		sent.setEnabled(false);
		sent.addActionListener(this);
		show1=new JTextField("远程小狗");
		show2=new JTextField("本地小猫");
	
		show1.setEditable(false);
		show2.setEditable(false);
		 Font f=new Font("华文中宋",Font.BOLD,17);
		 main.setFont(f); part.setFont(f); show1.setFont(f); 
		 show2.setFont(f);sent.setFont(f);
		 Container con=getContentPane();
		 con.setLayout(null);
		 aa.setBounds(0,0,450,350);
		 bb.setBounds(0,360,450,150);
		 
		 sent.setBounds(260,535,120,40);
		 zhu.setBounds(470,50,135,135);
		 show1.setBounds(470,235,135,30);
		 fu.setBounds(470,360,135,135);
		 show2.setBounds(470,530,135,30);
		 con.add(aa);con.add(bb);
		 con.add(sent);con.add(zhu);con.add(show1);
		 con.add(fu);
		 con.add(show2);
		 con.validate();
 	   setVisible(true);
        while(true)
      	   {
      		try
      		{server=new ServerSocket(4331);
      	  }
      	catch(IOException e1)
      	 { 
               main.append("提示:已连接!"+'\n');
                sent.setEnabled(true);
      	 } 
      	  try{ 
      	  	   
                you=server.accept();
             
             }
         catch (IOException e4)
             {  
                main.append("提示:未连接!"+'\n');
             }
             if(you!=null)
             {
               try{ 
                      out=new DataOutputStream(you.getOutputStream());
                       in=new DataInputStream(you.getInputStream());
             	     thread1.start(); 
             	  }
             	catch(IOException ee){}
             }
        }
		 
	}
	public void actionPerformed(ActionEvent e)
   { 
      
     if(e.getSource()==sent)
      {  String s=part.getText();
         if(s!=null)
           {  try { 
           	        out.writeUTF(s);
           	        main.append("本地小猫:"+s+"\n");
           	        part.setText("");
           	        
                  }
              catch(IOException e1){} 
           }               
      }
   }
   
  public void run()
  {
  	String s=null;
  	while(true)
  	{
  		 try{
  	        s=in.readUTF();
  	        main.append("远程小狗:"+s+"\n");
               }
           catch(IOException e) 
               {  main.append("与客户端已断开");
                      break;
               }   
  	  }
  	
    }

}

⌨️ 快捷键说明

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