*
*创建客户端,将第一次输入的用户名传输到服务器,作为用户的聊天名称
*/
public class Client {
@SuppressWarnings("resource")
public static void main(String[] args) {
Client client = new Client();
System.out.println("请输入用户名:");
String name = new Scanner(System.in).next();
client.logIn(name);
}
//接收从服务器端返回的信息
public void showInfo(){
ServerSocket ss;
try {
//与服务器端建立通信连接
ss = new ServerSocket();
Socket socket = ss.accept();
//此程序的目的是要完成简易聊天窗口,所以通过字符流进行转换输出
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str;
while((str = br.readLine())!=null){
System.out.println(str);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*创建本地服务器,用于接收和转发从客户端输入的信息
*
*/
public class Server {
//创建静态同步的hashmap,保存每个客户端的ip地址和传入的数据
static Map<String, String> map = Collections.synchronizedMap(new HashMap<String,String>());
@SuppressWarnings("resource")
public static void main(String[] args) {
//创建服务器端的端口号
ServerSocket ss = null;
while(true){
try {
//监听8080端口
ss = new ServerSocket(8080);
//与客户端建立连接
Socket socket = ss.accept();
//分配线程,并开启线程
ServerThread s = new ServerThread(socket);
s.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*新建线程类,完成服务器的接收和转发功能
*
*/
public class ServerThread extends Thread{
Socket socket;
public ServerThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
//获取当前连接的客户端
String ip = socket.getInetAddress().getHostAddress();
try {
//读取用户端传入的数据
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str;
//判断是否是第一次登录
Boolean flag = Server.map.containsKey(ip) ;
while((str = br.readLine()) != null){
//如果是第一次登录,则直接将数据保存到hashmap中,作为聊天的名称
if(!flag){
Server.map.put(ip, str);
}
String nowDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
//记录信息
String info = "";
//如果不是第一次登录,则将该用户的ip地址和登录时间,已经发送的消息保存
if(flag){
info = Server.map.get(ip) + " " + nowDate + "\r\n" + str;
}else{
//表示第一次登录,进入系统
info = "系统消息:" + nowDate + "\r\n" + str + "加入聊天室";
}
//遍历hashmap中所有的ip地址,发送info消息
Set<String> ipSet = Server.map.keySet();
System.out.println("<"+ ip +">" + info);
//逐个发送给消息
Iterator<String> it = ipSet.iterator();
while(it.hasNext()){
try{
Socket socket = new Socket(it.next(), 9090);
OutputStream os = socket.getOutputStream();
//通过缓冲流和字符流将信息从服务器端返回
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(info);
bw.close();
os.close();
socket.close();
}catch(Exception e){
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 消息发送客户端,向服务器端发送消息
* @author LL
*
*/
public class Sender {
@SuppressWarnings("null")
public static void main(String[] args) {
//因为Client类是为了完成第一次的用户注册,后续的消息发送都在sender中实现
Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
socket = new Socket("localhost",8080);
br.close();
bw.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}