clientreceivethread.java

来自「本例中聊天服务器默认于本地8000端口建立服务; 本例中聊天客户端默认连接59」· Java 代码 · 共 94 行

JAVA
94
字号
/*
 * Created on 2004-12-15
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author mq
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import javax.swing.*;
import java.io.*;
import java.net.*;

public class ClientReceiveThread extends Thread {
	private JComboBox combobox;
	private JTextArea textarea;
	
	Socket socket;
	ObjectOutputStream output;
	ObjectInputStream  input;

	public ClientReceiveThread(
		Socket socket,
		ObjectOutputStream output,
		ObjectInputStream  input,
		JComboBox combobox,
		JTextArea textarea
	)
	{
		this.socket = socket;
		this.output = output;
		this.input = input;
		this.combobox = combobox;
		this.textarea = textarea;
	}
	
	public void run()
	{
		while(!socket.isClosed())
		{
			try
			{	
				String type = (String)input.readObject();
				//JOptionPane.showMessageDialog(null,type);
				
				if(type.equalsIgnoreCase("系统信息"))
				{
					String sysmsg = (String)input.readObject();
					textarea.append("系统信息:"+sysmsg);
				}
				else if(type.equalsIgnoreCase("服务关闭"))
				{
					output.close();
					input.close();
					socket.close();
					
					textarea.append("服务器已关闭!\n");
					
					break;
				}
				else if(type.equalsIgnoreCase("聊天信息"))
				{
					String message = (String)input.readObject();
					textarea.append(message);
				}
				else if(type.equalsIgnoreCase("用户列表"))
				{
					String userlist = (String)input.readObject();
					String usernames[] = userlist.split("\n");
					combobox.removeAllItems();
					
					int i =0;
					combobox.addItem("all");
					while(i < usernames.length)
					{
						combobox.addItem(usernames[i]);
						i ++;
					}
					combobox.setSelectedIndex(0);
					//JOptionPane.showMessageDialog(null,userlist);
				}
			}
			catch (Exception e )
			{
				//
			}
		}
	}
}

⌨️ 快捷键说明

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