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

📄 例13-10.txt

📁 这是一本java基础教程 对新手上路有很大帮助
💻 TXT
字号:
【例13-10】
① 客户端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ChatClient extends Applet implements Runnable,ActionListener
{  Button send;
   TextField inputName,inputContent;
   TextArea chatResult;
   Socket socket=null;
   DataInputStream in=null;
   DataOutputStream out=null;
   Thread thread; 
   String name="";
   public void init()
   {   setLayout(new BorderLayout());
       Panel pNorth,pSouth;
       pNorth=new Panel();
       pSouth=new Panel();
       inputName=new TextField(6);
       inputContent=new TextField(22);
       send=new Button("发送");
       send.setEnabled(false); 
       chatResult=new TextArea();
       pNorth.add(new Label("输入妮称(回车):"));
       pNorth.add(inputName);
       pSouth.add(new Label("输入聊天内容:"));
       pSouth.add(inputContent);
       pSouth.add(send);
       send.addActionListener(this);
       inputName.addActionListener(this);
       thread=new Thread(this); 
       add(pNorth,BorderLayout.NORTH);
       add(pSouth,BorderLayout.SOUTH);
       add(chatResult,BorderLayout.CENTER);
   }
   public void start(){
       try{  socket=new Socket(this.getCodeBase().getHost(), 4331); 
             in=new DataInputStream(socket.getInputStream());
             out=new DataOutputStream(socket.getOutputStream());
       } 
       catch (IOException e){}
       if(!(thread.isAlive())){
            thread=new Thread(this); 
            thread.start();
       }
   }
   public void actionPerformed(ActionEvent e){
      if(e.getSource()==inputName){
            name=inputName.getText();
            send.setEnabled(true); 
            try{ out.writeUTF("姓名:"+name);
            }
            catch(IOException exp){}   
      }
      if(e.getSource()==send)
      {  String s=inputContent.getText();
         if(s!=null)
           {  try { out.writeUTF("聊天内容:"+name+":"+s);
                  }
              catch(IOException e1){} 
           }               
      }
   }
   public void run(){
      String s=null;
      while(true){
         try{ s=in.readUTF();
              chatResult.append("\n"+s);
         }
         catch(IOException e){ 
              chatResult.setText("和服务器的连接关闭");
              break;
         }   
      }
   }
}
② 服务器端
import java.io.*;
import java.net.*;
import java.util.*;
public class Server{
    public static void main(String args[]){
        ServerSocket server=null;
        Socket you=null;
        Hashtable peopleList; 
        peopleList=new Hashtable(); 
        while(true){ 
             try  {  server=new ServerSocket(4331);
             }
             catch(IOException e1){
                    System.out.println("正在监听");
              } 
             try  {  you=server.accept();                 
                     InetAddress address=you.getInetAddress();
                     System.out.println("客户的IP:"+address);
             }
             catch (IOException e) {}
             if(you!=null){
                 Server_thread peopleThread=new Server_thread(you,peopleList);
                 peopleThread.start();               
              }
             else  continue;
       }
    }
}
class Server_thread extends Thread{
    String name=null; 
    Socket socket=null;
    File file=null;
    DataOutputStream out=null;
    DataInputStream  in=null;
    Hashtable peopleList=null;
    Server_thread(Socket t,Hashtable list){
         peopleList=list;
         socket=t;
         try {  in=new DataInputStream(socket.getInputStream());
                out=new DataOutputStream(socket.getOutputStream());
         }
         catch (IOException e) {}
    }  
    public void run(){
         while(true){
            String s=null;   
            try{  s=in.readUTF(); 
                 if(s.startsWith("姓名:")){ 
                     name=s;
                     boolean boo=peopleList.containsKey(name);
                    if(boo==false) 
                        peopleList.put(name,this);          
                    else{
                        out.writeUTF("请换妮称:");
                        socket.close();
                        break; 
                    }
                 }
                 else if(s.startsWith("聊天内容")){
                    String message=s.substring(s.indexOf(":")+1);
                    Enumeration chatPersonList=peopleList.elements();
                    while(chatPersonList.hasMoreElements()){
                         ((Server_thread)chatPersonList.nextElement()).out.writeUTF (message);
                    }  
                 }
            }
            catch(IOException ee){
                Enumeration chatPersonList=peopleList.elements();    
                while(chatPersonList.hasMoreElements()){
                   try {  Server_thread  th=(Server_thread)chatPersonList.nextElement();
                          if(th!=this&&th.isAlive())
                             th.out.writeUTF("客户离线:"+name);
                   }
                   catch(IOException eee){}
                } 
                peopleList.remove(name); 
                try { socket.close();
                }                    
                catch(IOException eee){}
                System.out.println(name+"客户离开了");
                break;                                 
            }             
         } 
    }
}

⌨️ 快捷键说明

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