4.27 자바 채 팅 방 간단 한 작성

5669 단어 연마 하 다.
package com.softeem.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 *        
 * @author Mr.zhou
 *
 */
public class ChatServer {
	//       ,         
	private List clients = new ArrayList();
	
	/**
	 *       
	 */
	private void init(){
		//   
		Socket socket = null;
		//            Alt + /
		ServerSocket ss = null;
		try {
			//     
			ss = new ServerSocket(8088);
			System.out.println("    ");
		} catch (IOException e) {
			//    
			System.out.println("      ");
			e.printStackTrace();
		}
		try {
			User user = null;
			//            
			while(true){
				socket = ss.accept();				
				System.out.println("      ");
				user = new User(socket);
				//            
				clients.add(user);
				//    
				new Thread(user).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		//          
		
	}
	//        
	public static void main(String[] args) {
		new ChatServer().init();
	}
	
	
	
	//               ,             ,            
	private class User implements Runnable{
		private Socket socket = null;
		private InputStream is = null;
		private DataInputStream dis = null;
		private OutputStream os = null;
		private DataOutputStream dos = null;
		//                     
		private boolean flag = true;
		
		public User(Socket socket){
			this.socket = socket;
			try {
				is = socket.getInputStream();
				dis = new DataInputStream(is);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//           
		private void forwordToAllClient(String message){
			for (User user : clients) {
				if(user != this){
					//  
					try {
						dos = new DataOutputStream(user.socket.getOutputStream());
						dos.writeUTF(message);
						dos.flush();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		//             run   
		@Override
		public void run() {
			String message;
			//          
			while(flag){
				//         
				try {
					message = dis.readUTF();
					System.out.println("    :"+message);
					forwordToAllClient(message);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}


}
package com.softeem.chat;

import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;

/**
 *        
 * @author Mr.zhou
 *
 */
public class Client extends JFrame {
	//      
	private TextArea textAreaContent = new TextArea();
	//       
	private TextField textFieldContent = new TextField();
	//   
	private Socket socket = null;
	//     
	private InputStream is = null;
	//     
	private DataInputStream dis = null;
	//     
	private OutputStream os = null;
	//     
	private DataOutputStream dos = null;
	
	//      
	private void init(){
		//      
		this.setSize(400, 300);
		//      
		this.setTitle("Softeem   ");
		//        
		this.setLocationRelativeTo(null);
		
		//    
		this.add(textAreaContent);//        
		this.add(textFieldContent,BorderLayout.SOUTH);//              ( )
		textAreaContent.setFocusable(false);//          
		//         
		pack();
		//    
		conenct();
		
		//                 
		textFieldContent.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//                
				onClickEnter();
			}
		});
		
		//    
		this.setVisible(true);
		//    
		new Thread(new ReciveMessage()).start();
	}
	//        
	public static void main(String[] args) {
		new Client().init();
	}
	
	//             
	private void onClickEnter(){
		//           
		String message = textFieldContent.getText().trim();
		//    
		if(!"".equals(message) && message != null){	
			//               
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
									.format(new Date());
			//             
			textAreaContent.append(time + "
"+message+"
"); // textFieldContent.setText(""); // sendToServer(message); // System.out.println(message); } } // public void sendToServer(String message){ try { // dos.writeUTF(message); // dos.flush(); } catch (IOException e) { e.printStackTrace(); } } // private void conenct(){ try { // socket = new Socket("localhost", 8088); // os = socket.getOutputStream(); // dos = new DataOutputStream(os); // is = socket.getInputStream(); // dis = new DataInputStream(is); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // public class ReciveMessage implements Runnable{ @Override public void run() { boolean flag = true; while(flag){ try { // String message = dis.readUTF(); System.out.println(message); // textAreaContent.setText(message + "
"); } catch (IOException e) { e.printStackTrace(); } } } } }

좋은 웹페이지 즐겨찾기