package com.nn;
import java.net.*;
import java.util.*;
import java.io.*;
public class QQServer {
public static void main(String args[]) {
try {
//创建一个HashMap,用于保存所有的用户名和对应的Socket
HashMap hm = new HashMap() ;
// 服务器到8000端口监听(1)
ServerSocket ss = new ServerSocket(8000);
while (true) {
System.out.println("Server startup in 1000ms......");
Socket s = ss.accept();
MyService t = new MyService();
t.setSocket(s);
t.setHashMap(hm);
t.start();
}
} catch (Exception e) {
}
}
}
class MyService extends Thread {
private Socket s;
private HashMap hm ;
public void setSocket(Socket s) {
this.s = s;
}
public void setHashMap(HashMap hm){
this.hm = hm ;
}
public void run() {
try {
// 接收用户名和密码(2)
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String uandp = br.readLine();
// 拆分(4)
String user = uandp.split("%")[0];
String pass = uandp.split("%")[1];
// 验证(5)
File f = new File("g:/test/user.txt");
FileReader fr = new FileReader(f);
BufferedReader fbr = new BufferedReader(fr);
boolean yorn = false;
while (fbr.ready()) {
String uandps = fbr.readLine();
String uu = uandps.split("===")[0];
String pp = uandps.split("===")[1];
if (user.equals(uu) && pass.equals(pp)) {
yorn = true;
break;
}
}
// 发送确认信息到客户端(7)
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw, true);
if (yorn) {
pw.println("ok");
//将自己的名字发给HashMap中其他的人
for(Object temp : hm.values()){
Socket ts = (Socket)temp ;
OutputStream tos = ts.getOutputStream() ;
OutputStreamWriter tosw = new OutputStreamWriter(tos) ;
PrintWriter tpw = new PrintWriter(tosw , true) ;
tpw.println("user%"+user);
}
//HashMap里所有的用户名发给自己
for(Object temp : hm.keySet()){
pw.println("user%"+(String)temp);
}
//将用户名和对应的Socket存入HashMap中
hm.put(user, s) ;
//System.out.println(hm);
// 不停的接收客户端发送过来的信息(9)
while (true) {
String message = br.readLine();
String to = message.split("%")[0];
String mess = message.split("%")[1];
Object temp = hm.get(to);
Socket ts = (Socket)temp;
OutputStream tos = ts.getOutputStream();
OutputStreamWriter tosw = new OutputStreamWriter(tos);
PrintWriter tpw = new PrintWriter(tosw , true);
tpw.println("mess%"+mess);
//System.out.println(message);
}
} else {
pw.println("err");
}
} catch (Exception e) {
}
}
}
import java.net.*;
import java.util.*;
import java.io.*;
public class QQServer {
public static void main(String args[]) {
try {
//创建一个HashMap,用于保存所有的用户名和对应的Socket
HashMap hm = new HashMap() ;
// 服务器到8000端口监听(1)
ServerSocket ss = new ServerSocket(8000);
while (true) {
System.out.println("Server startup in 1000ms......");
Socket s = ss.accept();
MyService t = new MyService();
t.setSocket(s);
t.setHashMap(hm);
t.start();
}
} catch (Exception e) {
}
}
}
class MyService extends Thread {
private Socket s;
private HashMap hm ;
public void setSocket(Socket s) {
this.s = s;
}
public void setHashMap(HashMap hm){
this.hm = hm ;
}
public void run() {
try {
// 接收用户名和密码(2)
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String uandp = br.readLine();
// 拆分(4)
String user = uandp.split("%")[0];
String pass = uandp.split("%")[1];
// 验证(5)
File f = new File("g:/test/user.txt");
FileReader fr = new FileReader(f);
BufferedReader fbr = new BufferedReader(fr);
boolean yorn = false;
while (fbr.ready()) {
String uandps = fbr.readLine();
String uu = uandps.split("===")[0];
String pp = uandps.split("===")[1];
if (user.equals(uu) && pass.equals(pp)) {
yorn = true;
break;
}
}
// 发送确认信息到客户端(7)
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw, true);
if (yorn) {
pw.println("ok");
//将自己的名字发给HashMap中其他的人
for(Object temp : hm.values()){
Socket ts = (Socket)temp ;
OutputStream tos = ts.getOutputStream() ;
OutputStreamWriter tosw = new OutputStreamWriter(tos) ;
PrintWriter tpw = new PrintWriter(tosw , true) ;
tpw.println("user%"+user);
}
//HashMap里所有的用户名发给自己
for(Object temp : hm.keySet()){
pw.println("user%"+(String)temp);
}
//将用户名和对应的Socket存入HashMap中
hm.put(user, s) ;
//System.out.println(hm);
// 不停的接收客户端发送过来的信息(9)
while (true) {
String message = br.readLine();
String to = message.split("%")[0];
String mess = message.split("%")[1];
Object temp = hm.get(to);
Socket ts = (Socket)temp;
OutputStream tos = ts.getOutputStream();
OutputStreamWriter tosw = new OutputStreamWriter(tos);
PrintWriter tpw = new PrintWriter(tosw , true);
tpw.println("mess%"+mess);
//System.out.println(message);
}
} else {
pw.println("err");
}
} catch (Exception e) {
}
}
}