任务: p2p网站即时通信
先做出多线程的简单通信
再连接到 项目上
然后以P2P替换
服务器端能够把一个客户的信息 传递给另一个用户
单线程 实时发送 图形化界面
服务器可同时接受多个客户端的请求
每一个人上线的时候 会启动一个服务器
(接收显示)如果有人往这个服务器里面发消息了 就把聊天窗口显示出来
这个窗口里面显示的 就是与这个人的聊天内容 包括收到和发出的信息
(发送显示)如果点击在线人员列表 也弹出一个同样的聊天对话框
实现接收或者发送消息时 弹出聊天对话框:
用户建立了与服务器的连接 就会有一个聊天面板 点击隐藏可以让其消失Frame.setVisible(false)
在接收或者发送的管道里如果有数据出现 让聊天面板出现
while(true){
....
String read = reader.readLine();
if(read.contains("zhimakaimen")){
this.setVisible(true);
continue;
....
}
私聊:
在服务器上 有一个用户map列表 每一个用户发消息时候 都在消息里面加上发送的对象
服务器根据名字取到对应的用户 转发信息
给每一个对话 建立一个对话框:
在客户端 建立一个map 里面装着 所有的与每一个人聊天的对话框
用户收发信息的时候 如果map里面没有与此人聊天的对话框 就新建一个 并放进ma
能给一个客户端生成多个对话框了:
给把客户端代码分开 重新组合
以通信代码为主 在需要的时候弹出对话框
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.*;
public class Cl implements Runnable {
Client5 frame = null;
String title = "";
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
private HashMap map = new HashMap();
private void checkMap(String read) {
read = read.substring(read.indexOf("> :")+3);
System.err.println(read.trim());
if (!(map.containsKey(read.trim()))) {
frame = new Client5("");
frame.setVisible(true);
map.put(read.trim(),frame);
}
}
public PrintWriter getWriter() {
return writer;
}
public void setWriter(PrintWriter writer) {
this.writer = writer;
}
public void run() {
while (true) {
try {
String read = reader.readLine();
checkMap(read);
frame.getMsgView().append(read + ""n");
} catch (IOException ie) {
}
}
}
private void connect() {
try {
System.err.println("尝试与服务器连接."n");
socket = new Socket("127.0.0.1", 7777);
System.err.println("聊天准备完毕."n");
reader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
new Thread(this).start();
} catch (Exception e) {
System.err.println("连接失败..");
}
}
public static void main(String[] args) {
Cl client = new Cl();
client.connect();
}
class Client5 extends Frame {
private TextField nameBox = new TextField("<WeiJian>");
private TextArea msgView = new TextArea();
private TextField sendBox = new TextField();
public Client5() {}
public Client5(String title) {
super(title);
msgView.setEditable(false);
add(nameBox, "North");
add(msgView, "Center");
add(sendBox, "South");
sendBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
writer.println(
nameBox.getText() + " : " + sendBox.getText());
sendBox.setText("");
} catch (Exception ie) {
}
}
});
pack();
}
public TextArea getMsgView() {
return msgView;
}
public void setMsgView(TextArea msgView) {
this.msgView = msgView;
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class Server5{
private ServerSocket server;
private BManager bMan=new BManager();
public Server5(){}
void startServer(){
try{
server=new ServerSocket(7777);
System.out.println("服务器创建完毕");
while(true){
Socket socket=server.accept();
new Chat_Thread(socket).start();
bMan.add(socket);
bMan.sendClientInfo();
}
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args){
Server5 server=new Server5();
server.startServer();
}
class Chat_Thread extends Thread{
Socket socket;
private BufferedReader reader;
private PrintWriter writer;
Chat_Thread(Socket socket){
this.socket=socket;
}
public void run(){
try{
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(), true);
String msg;
while((msg=reader.readLine())!=null){
System.out.println(msg);
bMan.sendToAll(msg);
}
}catch(Exception e){
}finally{
try{
bMan.remove(socket);
if(reader!=null) reader.close();
if(writer!=null) writer.close();
if(socket!=null) socket.close();
reader=null; writer=null; socket=null;
System.out.println("客户机离开.");
bMan.sendClientInfo();
}catch(Exception e){}
}
}
}
class BManager extends Vector{
BManager(){}
void add(Socket sock){
super.add(sock);
}
void remove(Socket sock){
super.remove(sock);
}
synchronized void sendToAll(String msg){
PrintWriter writer=null;
Socket sock;
for(int i=0; i<size(); i++){
sock=(Socket)elementAt(i);
try{
writer=new PrintWriter(sock.getOutputStream(), true);
}catch(IOException ie){}
if(writer!=null)writer.println(msg);
}
}
synchronized void sendClientInfo(){
String info="当前聊天人数: "+size();
System.out.println(info);
sendToAll(info);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class Client5 extends Frame implements Runnable {
private TextField nameBox = new TextField("<shenxian>");
private TextArea msgView = new TextArea();
private TextField sendBox = new TextField();
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
public Client5(String title) {
super(title);
msgView.setEditable(false);
add(nameBox, "North");
add(msgView, "Center");
add(sendBox, "South");
sendBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
writer.println(nameBox.getText() + " : "
+ sendBox.getText());
sendBox.setText("");
} catch (Exception ie) {
}
}
});
pack();
}
public void run() {
while (true) {
try {
msgView.append(reader.readLine() + ""n");
} catch (IOException ie) {
}
}
}
private void connect() {
try {
msgView.append("尝试与服务器连接."n");
socket = new Socket("127.0.0.1", 7777);
msgView.append("聊天准备完毕."n");
reader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
new Thread(this).start();
} catch (Exception e) {
msgView.append("连接失败..");
}
}
public static void main(String[] args) {
Client5 client = new Client5("聊天");
client.setVisible(true);
client.connect();
}
}