关于java的问题!!!

MessengerServer.java

package pro4;
import java.net.*;
import java.io.*;
import java.util.*;
public class MessengerServer {
	public void startServer(){
		try{
			ServerSocket serverSocket=new ServerSocket(8000,100);
			System.out.println("服务器已启动");
			while(true){
				Socket clientSocket=serverSocket.accept();
				System.out.println("已接受到客户来自:"+clientSocket.getInetAddress());
				
				ChatHandler handler=new ChatHandler(clientSocket);
				handler.start();
			}
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	public static void main(String[] args) {
		MessengerServer application=new MessengerServer();
		application.startServer();

	}

}


ChatHandler.java
package pro4;
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatHandler implements Runnable{
	protected Socket socket;
	protected ObjectInputStream dataIn;
	protected ObjectOutputStream dataOut;
	protected Thread listener;
	protected static Vector handlers=new Vector();
	
	private boolean keepListening=true;
	public ChatHandler(Socket socket){
		this.socket=socket;
	}
	public synchronized void start(){
		if(listener==null){
			try{
				dataIn=new ObjectInputStream(socket.getInputStream());
				dataOut=new ObjectOutputStream(socket.getOutputStream());
				listener=new Thread(this);
				listener.start();
			}catch(IOException ioException){
				ioException.printStackTrace();
			}
		}
	}
	public synchronized void stop(){
		if(listener!=null){
			try{
				if(listener!=Thread.currentThread())
					listener.interrupt();
				listener=null;
				dataOut.close();
				socket.close();
			}catch(IOException ignored){
				
			}
		}
	}
	public void run(){
		String message="";
		try{
			handlers.addElement(this);
			while(keepListening){
				message=(String)dataIn.readObject();
				if(message.equals("DISCONNECT")){
					dataOut.writeObject(message);
					dataOut.flush();
					stopListening();
				}else
					broadcast(message);
			}
		}catch(ClassNotFoundException classNotFoundException){
			
		}catch(EOFException ignored){
			
		}catch(IOException ex){
			if(listener==Thread.currentThread())
				ex.printStackTrace();
		}finally{
			handlers.removeElement(this);
		}
		try{
			dataIn.close();
		}catch(IOException ioException){
			ioException.printStackTrace();
		}
		stop();
	}
	protected void broadcast(String message){
		synchronized (handlers){
			Enumeration enumer=handlers.elements();
			while(enumer.hasMoreElements()){
				ChatHandler handler=(ChatHandler)enumer.nextElement();
				try{
					handler.dataOut.writeObject(message);
					handler.dataOut.flush();
				}catch(IOException ex){
					handler.stop();
				}				
			}
		}
	}
	public void stopListening(){
		keepListening=false;
	}
}


MessengerClient.java
package pro4;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
public class MessengerClient  extends JFrame implements Runnable{
	private JTextArea messageArea;
	private JTextArea inputArea;
	
	private JButton connectButton;
	private JButton disconnectButton;
	private JButton sendButton;
	private JLabel statusBar;
	
	private String userName;
	private String chatServer;
	private ObjectOutputStream output;
	private ObjectInputStream input;
	
	private Socket client;
	private boolean connected;
	private Thread processConnection;
	
	public MessengerClient(String host){
		super("聊天客户端");
		chatServer=host;
		connectButton=new JButton("登录");
		ActionListener connectListener=new ConnectListener();
		connectButton.addActionListener(connectListener);
		disconnectButton=new JButton("退出");
		disconnectButton.setEnabled(false);
		ActionListener disconnectListener=new DisconnectListener();
		disconnectButton.addActionListener(disconnectListener);
		
		messageArea=new JTextArea();
		messageArea.setEditable(false);
		messageArea.setWrapStyleWord(true);
		messageArea.setLineWrap(true);
		
		JPanel messagePanel=new JPanel();
		messagePanel.setLayout(new BorderLayout(10,10));
		messagePanel.add(new JScrollPane(messageArea),BorderLayout.CENTER);
		
		inputArea=new JTextArea(4,20);
		inputArea.setWrapStyleWord(true);
		inputArea.setLineWrap(true);
		inputArea.setEditable(false);
		
		sendButton=new JButton("发送");
		sendButton.setEnabled(false);
		sendButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				sendMessage(userName+">"+inputArea.getText());
				inputArea.setText("");
			}
		});
		JPanel buttonPanel=new 	JPanel();
		buttonPanel.setLayout(new GridLayout(3,1));
		buttonPanel.add(sendButton);
		buttonPanel.add(connectButton);
		buttonPanel.add(disconnectButton);
		
		Box box=new Box(BoxLayout.X_AXIS);
		box.add(new JScrollPane(inputArea));
		box.add(buttonPanel);
		messagePanel.add(box, BorderLayout.SOUTH);
		
		statusBar=new JLabel("离线");
		statusBar.setBorder(new BevelBorder(BevelBorder.LOWERED));
		
		Container container=getContentPane();
		container.add(messagePanel, BorderLayout.CENTER);
		container.add(statusBar,BorderLayout.SOUTH);
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent event){
				if(connected){
					sendMessage("DISCONNECT");
					do{
						
					}while(connected);
				}
				System.exit(0);
			}
		});
		
	}
	private void sendMessage(String message){
		try{
			output.writeObject(message);
			output.flush();
		}catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	public void run(){
		String message="";
		do{
			try{
				message=(String)input.readObject();
			}catch(ClassNotFoundException classNotFoundException){
				displayMessage("\n收到异常对象类型");
			}catch(EOFException eofException){
				System.err.println("连接终止");
			}catch(IOException ioException){
				ioException.printStackTrace();
			}
			if(message.equals("DISCONNECT")){
				try{
					input.close();
					output.close();
					
					client.close();
				}catch(IOException ioException){
					ioException.printStackTrace();
				}
				connected=false;
			}
			else displayMessage(message+"\n");
		}while(connected);
	}
	private class ConnectListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			try{
				client=new Socket(chatServer,8000);
				output=new ObjectOutputStream(client.getOutputStream());
				output.flush();
				input=new ObjectInputStream(client.getInputStream());
			}catch(IOException ioException){
				ioException.printStackTrace();
			}
			userName=JOptionPane.showInputDialog("请输入你的用户名");
			messageArea.setText("");
			connectButton.setEnabled(false);
			disconnectButton.setEnabled(true);
			sendButton.setEnabled(true);
			inputArea.setEditable(true);
			inputArea.requestFocus();
			statusBar.setText("在线:"+userName);
			connected=true;
			
			processConnection=new Thread(MessengerClient.this);
			processConnection.start();
		}
	}
	private class DisconnectListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			sendMessage("DISCONNECT");
			sendButton.setEnabled(false);
			disconnectButton.setEnabled(false);
			
			inputArea.setEditable(false);
			connectButton.setEnabled(true);
			
			statusBar.setText("离线");
		}
	}
	private void displayMessage(final String messageToDisplay){
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				messageArea.append(messageToDisplay);
				messageArea.setCaretPosition(messageArea.getText().length());
			}
		});
	}
	public static void main(String[] args) {
		MessengerClient application;
		if(args.length==0)
			application=new MessengerClient("127.0.0.1");
		else
			application=new MessengerClient(args[0]);
		
		application.setSize(300,400);
		application.setResizable(false);
		application.setVisible(true);

	}

}

请问怎么在这个基础上实现细聊,就是能看到在线的人,然后点击进行私聊


ZXJ03
浏览 1550回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java