client.java
资源名称:QQ.rar [点击查看]
上传用户:hongdaled
上传日期:2007-10-03
资源大小:680k
文件大小:4k
源码类别:
ICQ/即时通讯
开发平台:
Java
- package server;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Properties;
- import java.util.TreeSet;
- class client extends Thread {
- int id;
- ObjectOutputStream dos;
- // 去往客户的输出流
- ObjectInputStream din;
- // 来自客户的输入流
- Socket s;
- private Object key;
- public client(int id, ObjectOutputStream dos, ObjectInputStream din,
- Socket s) {
- this.s = s;
- this.id = id;
- this.dos = dos;
- this.din = din;
- }
- public void run() {
- // 循环读取客户数据转发给其他客户
- while (true) {
- try {
- Object oo = din.readObject();
- int m = serverthread.clientnum;
- // 读客户数据,无数据时线程将挂起
- if (oo instanceof ArrayList) {
- ArrayList user = (ArrayList) oo;
- if (user.size() > 3) {
- qq_user qq = new qq_user();
- qq.name = user.get(0).toString();
- qq.passward = user.get(1).toString();
- qq.sex = Integer.parseInt(user.get(2).toString());
- qq.info = user.get(3).toString();
- System.out.println(qq);
- HashMap set = readtext.read();
- System.out.println(set);
- set.put(qq.name, qq);
- System.out.println(qq.name);
- readtext.write(set);
- } else {
- // String message=user.get(0);
- // System.out.println(message);
- client c;
- for (int j = 0; j < serverthread.l.size(); j++) {
- c = (client) serverthread.l.get(j);
- c.dos.writeObject(user);
- // 转发给其他客户
- }
- }
- }
- if (oo instanceof String) {
- String log = (String) oo;
- String[] arr = log.split(":");
- HashMap set = readtext.read();
- System.out.println(log);
- Object user = set.get(arr[0]);
- qq_user qb = (qq_user) user;
- if (user != null && qb.passward.equals(arr[1].toString())) {
- client b = (client) serverthread.l.get(m - 1);
- String str = b.s.getInetAddress().toString();
- System.out.println(" ggggggggggggggggggg");
- System.out.println(str);
- String stt = str + ":" + arr[2];
- dos.writeObject("login");
- serverstart.map.put(arr[0], stt);
- ArrayList l = new ArrayList();
- Object[] arr1 = serverstart.map.keySet().toArray();
- String[] ss1 = new String[arr1.length];
- for (int j = 0; j < arr1.length; j++) {
- ss1[j] = serverstart.map.get(arr1[j].toString())
- .toString();
- l.add(arr1[j] + ":" + ss1[j]);
- }
- client c;
- for (int j = 0; j < serverthread.l.size(); j++) {
- c = (client) serverthread.l.get(j);
- c.dos.writeObject(l);
- }
- } else {
- System.out.println("登入失败");
- dos.writeObject("shibai");
- }
- }
- if (oo instanceof TreeSet) {
- TreeSet hs = (TreeSet) oo;
- Iterator itr = hs.iterator();
- String ss;
- ss = (String) itr.next();
- if (ss.charAt(0) == '*') {
- serverstart.map.remove(ss.substring(1, ss.length()));
- } else if (ss.charAt(0) == '?') {
- serverstart.map.remove(ss.substring(1, ss.length()));
- // s.close();
- } else {
- String s0 = (String) itr.next();
- String s1 = (String) itr.next();
- serverstart.map.put(ss.substring(1, ss.length()), s0
- .substring(1, s0.length())
- +":"+ s1.substring(1, s1.length()));
- }
- ArrayList l = new ArrayList();
- Object[] arr1 = serverstart.map.keySet().toArray();
- String[] ss1 = new String[arr1.length];
- for (int j = 0; j < arr1.length; j++) {
- ss1[j] = serverstart.map.get(arr1[j].toString())
- .toString();
- l.add(arr1[j] + ":" + ss1[j]);
- }
- client c;
- for (int j = 0; j < serverthread.l.size(); j++) {
- c = (client) serverthread.l.get(j);
- c.dos.writeObject(l);
- }
- }
- } catch (IOException e) {
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }