首页>代码>基于java swing的聊天室GUI demo实例>/chat/src/com/test/ClientThread.java
package com.test;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.JTextArea;

public class ClientThread implements Runnable{
	private Vector<String> uList;
	private Socket socket;
	private JTextArea txtMsg, txtSend;
	private String strmsg;
	private StringTokenizer tokenizer;
	private Parameterx parameterx;
	@SuppressWarnings("rawtypes")
	private JList lUser;
	@SuppressWarnings("rawtypes")
	public ClientThread(Socket socket, JTextArea area, Vector<String> list, JTextArea txtSend, JList lUser){
		this.socket = socket;
		this.txtMsg = area;
		this.uList = list;
		this.txtSend = txtSend;
		this.parameterx = new Parameterx();
		this.lUser = lUser;
		uList = new Vector<String>();
	}
	@SuppressWarnings("unchecked")
	public void run(){
		try{
			while(true){
				DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
				strmsg = dataInputStream.readUTF();
				tokenizer = new StringTokenizer(strmsg, "|");
				String head = tokenizer.nextToken();
				
				if(head.equalsIgnoreCase("message")){
					String data = tokenizer.nextToken();
					String userName = tokenizer.nextToken();
					String msg = tokenizer.nextToken();
					data += (userName + msg);
					txtMsg.append(data + "\r\n");
					txtSend.setText("");
					
					String fName = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date()); 
					@SuppressWarnings("static-access")
					File file = new File(parameterx.path + "\\" + fName + ".txt");
					BufferedWriter bWriter = new BufferedWriter(new FileWriter(file, true));
					bWriter.write(data);
					bWriter.newLine();
					bWriter.flush();
					bWriter.close();		
				}
				else if(head.equalsIgnoreCase("renew")){
					head = tokenizer.nextToken();
					int length = Integer.parseInt(head);
					uList = new Vector<String>();
					for(int ad = 0; ad < length; ad++){
						head = tokenizer.nextToken();
						uList.add(head);
					}
					lUser.setListData(uList);					
				}						
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
}
最近下载更多
taoshen95  LV16 2024年1月19日
漫步的海星  LV4 2023年8月21日
2017143155  LV12 2023年6月24日
wanglinddad  LV55 2022年6月18日
微信网友_6003487859068928  LV5 2022年6月16日
zhendong  LV7 2022年3月6日
dfz12345  LV4 2021年12月8日
胡萝北呀  LV3 2021年11月28日
ComeDebug  LV6 2021年9月6日
忧麦紫  LV18 2021年6月26日
最近浏览更多
13133117021  LV5 2024年12月26日
srl2881552  LV10 2024年11月25日
微信网友_7004855557083136  LV1 2024年5月22日
taoshen95  LV16 2024年1月19日
陈小灏  LV18 2023年12月27日
求学的熊猫  LV11 2023年12月25日
wuxin427 2023年12月20日
暂无贡献等级
fuyouou  LV5 2023年6月29日
2017143155  LV12 2023年6月24日
17683946472  LV9 2023年6月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友