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

📄 server.java

📁 JAVA聊天系统!又一个版本
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

import javax.swing.JOptionPane;
class Server implements WindowListener,ActionListener 
{
    Frame f;
    int port;
    Label lb1;
    Label lb2;
    Label lb3;
    TextArea ta;
    TextField tf;
    TextField tf2;
    Button bt;
    String hostname;
    String name;
    DatagramSocket receiveSocket, sendSocket;
    DatagramPacket receivePacket ,sendPacket;
    InetAddress ip;
    Button btMember;
    public static void main(String[] args)
    {   JOptionPane.showMessageDialog(null,"欢迎来到我们的聊天系统!");
        Server server=new Server();
        server.creatwindow();
        server.start();
        server.receiveMessage();
        
    } 
    void creatwindow()
    {
      f=new Frame("聊天程序服务器端");
      lb1=new Label("对话框");
      lb2=new Label("发送消息");
      lb3=new Label("你的姓名:");
      ta=new TextArea(10,55);
      tf=new TextField(55);
      tf2=new TextField(20);
      bt=new Button("发送");
      btMember=new Button("本组成员列表");
      ta.setEditable(false);
      f.add(lb1);
      f.add(ta);
      f.add(lb2);
      f.add(tf);
      f.add(lb3);
      f.add(tf2);
      f.add(bt);
      f.addWindowListener(this);
      bt.addActionListener(this);
      btMember.addActionListener(this);
      f.add(btMember);
      f.setLayout(new FlowLayout());
      f.setSize(500,300);
      f.setVisible(true);
      f.setLocation(500,200); 
      ta.append("注意,请输入姓名");
      ta.append("\n");
    
    }
    public void windowClosing(WindowEvent e)
   { 
        receiveSocket.close();
        sendSocket.close();
        System.exit(0); 
   
   }
   public void windowClosed(WindowEvent e){}
   public void windowOpened(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void start()
   {
   try
   {
       sendSocket=new DatagramSocket(7600);//服务器发送端口
       
   }
   catch(Exception e)
   {
       ta.append(e+"\n"); 
   }
   
   
   }
   public void receiveMessage()//服务器发送消息
   {
      try
      { 
        receiveSocket=new DatagramSocket(9000);//服务器接受端口
        
        while(true)
        {
           
           
            byte[] buf=new byte[200];
            receivePacket=new DatagramPacket(buf,buf.length);
            receiveSocket.receive(receivePacket);
            ip=receivePacket.getAddress();
            port=receivePacket.getPort(); 
            if(receivePacket.getLength()==0)
            {
             ta.append("空消息\n"); 
            continue;
            }
            ByteArrayInputStream bin=new ByteArrayInputStream(receivePacket.getData());
            BufferedReader read=new BufferedReader(new InputStreamReader(bin));
            ta.append("客户端:"+read.readLine());
            ta.append("\n");
            read.close();
            bin.close();
            
        }
        }
           catch(Exception e)
           {
             ta.append(e+"sendmessage error\n"); 
           
           }
            
   }
   public void sendMessage()
   {
     try{
     String s=tf.getText();
     name=tf2.getText();
     int nameLength=name.length();
     if(nameLength==0) 
     {
      JOptionPane.showMessageDialog(null,"请输入姓名,对方不能确定收到谁的数据!");
     }
     s=name+"发送的数据是:"+s;
     tf.setText("");
     ta.append("服务器:"+s);
     ta.append("\n");
     ByteArrayOutputStream out=new ByteArrayOutputStream();
     PrintStream pout=new PrintStream(out);
     pout.print(s);
     byte[] buf=out.toByteArray();
     sendPacket=new DatagramPacket(buf,buf.length,ip,3000);
     sendSocket.send(sendPacket);
     buf=null;
     }
     catch(Exception e)
     {
     ta.append(e+"\n"); 
       
     }
    }
   public void actionPerformed(ActionEvent e)
   {
         if(e.getSource()==bt)
         {
            sendMessage(); 
         
         } 
         if(e.getSource()==btMember)
         {
        	 Mem members=new Mem();
				members.setSize(250,233);
         
         } 
   }
}

⌨️ 快捷键说明

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