zoukankan      html  css  js  c++  java
  • [转]Java写的聊天程序

    客户端:

    package zju.lh;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    
    @SuppressWarnings("serial")
    public class ChatClient extends Frame{
    	Socket s = null;
    	DataInputStream dis = null;
    	DataOutputStream dos = null;
    	boolean connected = false;
    	String name;
    	
    	Thread rt = null;
    	
    	TextField tfTxt = new TextField();
    	TextArea taContent = new TextArea();
    	//Button buttonSend = new Button("发送");
    	
    	public static void main(String[] args){
    		new ChatClient("dd").lauchFrame();
    	}
    	
    	public ChatClient(String name){
    		this.name = name;
    	}
    	
    	public void lauchFrame(){
    		this.setLocation(400, 300);
    		this.setSize(500, 400);
    		this.setTitle(name);
    		this.add(tfTxt, BorderLayout.SOUTH);
    		this.add(taContent, BorderLayout.NORTH);
    		this.tfTxt.addActionListener(new TFListener());
    		//this.add(buttonSend, BorderLayout.AFTER_LINE_ENDS);
    		this.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent arg0){
    				close();
    				System.exit(0);
    			}
    		});
    		
    		this.pack();
    		this.setVisible(true);
    		connect();
    		rt = new Thread(new ReceiveThread());
    		rt.start();
    	}
    	public void connect(){
    		try{
    			s = new Socket("127.0.0.1", 8888);
    			dos = new DataOutputStream(s.getOutputStream());
    			dis = new DataInputStream(s.getInputStream());
    			connected = true;
    		}catch(UnknownHostException ex){
    			ex.printStackTrace();
    		}
    		catch(IOException ex){
    			ex.printStackTrace();
    		}
    	}
    	
    	@SuppressWarnings("deprecation")
    	public void close(){
    		try{
    			if(dis != null)
    				dis.close();
    			if(dos != null)
    				dos.close();
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}finally{
    			Thread.yield();
    			rt.stop();
    			System.out.println("退出了. . . . . .");
    		}
    	}
    	
    	public void sendMsg(String msg){
    		try{
    			dos.writeUTF(msg);
    			dos.flush();
    		}catch(IOException e){
    			e.printStackTrace();
    		}
    	}
    	
    	private class TFListener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			String msg = name + " : " + tfTxt.getText().trim() + "\n";
    			taContent.append(msg);
    			tfTxt.setText("");
    			sendMsg(msg);
    		}
    	}
    	
    	private class ReceiveThread implements Runnable {
    		private void receive(){
    			String msg = "";
    			try{
    				msg = dis.readUTF();
    				taContent.append(msg);
    			}catch(IOException e){
    			}
    		}
    		public void run(){
    			while(connected){
    				receive();
    			}
    		}
    	}
    }
    

      

    服务器端:

    package zju.lh;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.*;
    import java.util.Vector;
    
    public class ChatServer {
    	boolean started = false;
    	ServerSocket ss = null;
    	public static Vector<ClientThread> cts = new Vector<ClientThread>();
    	public static void main(String[] args){
    		new ChatServer().start();
    	}
    	public void start(){
    		try{
    			ss = new ServerSocket(8888);
    			started = true;
    			System.err.println("/*********服务器已经启动在8888端口监听************/");
    		}catch(Exception ex){
    			System.err.print("出错");
    			ex.printStackTrace();
    			System.exit(0);
    		}
    		try{
    			while(started){
    				Socket s = ss.accept();
    				ClientThread ct = new ClientThread(s);
    				new Thread(ct).start();
    				cts.add(ct);
    			}
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}finally{
    			try{
    				if(ss != null)
    					ss.close();
    			}catch(IOException e){
    				e.printStackTrace();
    			}
    		}
    	}
    	private class ClientThread implements Runnable{
    		private Socket s;
    		private DataInputStream dis;
    		private DataOutputStream dos;
    		private boolean connected = false;
    		private Vector<ClientThread> threads = cts;
    		
    		public ClientThread(Socket s){
    			this.s = s;
    			
    			try{
    				this.dis = new DataInputStream(s.getInputStream());
    				this.dos = new DataOutputStream(s.getOutputStream());
    				this.connected = true;
    			}catch(IOException ex){
    				ex.printStackTrace();
    			}
    		}
    		
    		public void transmit(String msg){
    			try{
    				dos.writeUTF(msg);
    				dos.flush();
    			}catch(IOException e){
    				e.printStackTrace();
    			}
    		}
    		@Override
    		public void run(){
    			try{
    				while(connected){
    					String msg = dis.readUTF();
    					for(ClientThread ct : threads){
    						if(ct != this)
    							ct.transmit(msg);
    					}
    				}
    			}catch(Exception ex){
    				
    			}finally{
    				try{
    					if(dos != null)
    						dos.close();
    					if(dis != null)
    						dis.close();
    					if(s != null){
    						s.close();
    						//移除这个客户端的连接
    						cts.remove(this);
    					}
    				}catch(IOException e){
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    git 只添加cpp文件
    URI和URL学习
    scanpy中常用函数
    特征去量纲方法
    排列组合数计算公式
    UML图
    行测图形推理笔记-
    nis服务器实现用户帐号管理同步
    rsh配置双方主机免密
    两周实习的总结
  • 原文地址:https://www.cnblogs.com/hengli/p/2586862.html
Copyright © 2011-2022 走看看