复习了一天Java。晚上写了一个HeartChat 0.1,实现多个客户端相互聊天的机制。代码如下:
class="java" name="code">
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.net.*;
public class HeartClient extends Frame {
/*
* 成员方法出场...
*/
private TextField tfText;
private TextArea taContent;
private Socket s;
private DataOutputStream dos;
private DataInputStream dis;
/**
* 注意,入口... ^^
* @param args
*/
public static void main(String[] args) {
new HeartClient().launchFrame();
}
/**
* Loading GU
*/
public void launchFrame(){
tfText = new TextField();
taContent = new TextArea();
this.setSize(300,300);
this.setLocation(300,300);
this.tfText.addActionListener(new TFListener());
this.add(tfText,BorderLayout.SOUTH);
this.add(taContent,BorderLayout.NORTH);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
this.pack();
this.connect();
this.setVisible(true);
}
/**
* 我在努力地连接服务器中...
*/
public void connect() {
try {
s = new Socket("127.0.0.1",1720);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
new Thread(new SendThread()).start();
// dos.writeUTF("Hello,i find u!");
} catch (UnknownHostException e) {
System.out.println("UnknownHostException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}finally{
//关闭啥尼...
}
}
/**
* 额不会傻等着tfText(TextField tfText的监听器类)
*/
class TFListener implements ActionListener{
private String str;
@Override
public void actionPerformed(ActionEvent e) {
str = tfText.getText().trim();
tfText.setText("");
try {
dos.writeUTF(str);
} catch (IOException e1) {
System.out.println("IOException");
e1.printStackTrace();
}
}
}
/**
* 客户端接收消息的线程呦...
*
*/
class SendThread implements Runnable{
private String str;
private boolean iConnect = false;
public void run(){
iConnect = true;
recMsg();
}
/**
* 消息,看招,哪里跑...(客户端接收消息的实现)
* @throws IOException
*/
public void recMsg() {
try {
while(iConnect){
str = dis.readUTF();
taContent.setText(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class HeartServer {
/*
* 成员变量闪亮登场
*/
List<ClientThread> clients = new ArrayList<ClientThread>();
/**
* 这系入口啊,向这里看齐...
* @param args
*/
public static void main(String[] args) {
new HeartServer().start();
}
/**
* 启动服务器中...
*
*/
public void start(){
try {
boolean iConnect = false;
ServerSocket ss = new ServerSocket(1720);
iConnect = true;
while(iConnect){
System.out.println("绑定服务器端口成功!");
Socket s = ss.accept();
ClientThread currentClient = new ClientThread(s);//创建线程引用
System.out.println("发现客户端!");
clients.add(currentClient);//把当前客户端加入集合
new Thread(currentClient).start();
System.out.println("客户端进程已经启动!");
}
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}
}
/**
* 给每个客户端留个家(客户端的进程)
*
*/
class ClientThread implements Runnable {
/*
* 成员变量又来啦...
*/
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private String str;
private boolean iConnect = false;
/**
* 小构一下
*/
ClientThread(Socket s){
this.s = s;
iConnect = true;
}
public void run(){
System.out.println("run方法启动了!");
try {
while(iConnect){
System.out.println("RUN方法中的while循环启动,正在等待客户端的发送消息...");
dis = new DataInputStream(s.getInputStream());
str = dis.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++){
System.out.println("转发消息中..."+i);
ClientThread c = clients.get(i);
c.sendMsg(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 转发消息,我做主...
* 将送至服务器的消息发送给每个连接到的客户端
*/
public void sendMsg(String str){
try {
System.out.println("创建输出管道!");
dos = new DataOutputStream(this.s.getOutputStream());
System.out.println("正在向客户端写消息!");
dos.writeUTF(str);
System.out.println("正在向客户端写消息成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- HeartChat_0.1.zip (2.4 KB)
- 下载次数: 98