您的位置:首页 > 编程语言 > Java开发

Java AWT实现的简单的多人在线聊天室

2010-08-12 23:23 169 查看
客户端:

package Client;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient {
public static void main(String[] args) {
new Myframe();
}

}

class Myframe extends Frame {
TextField text1;
TextArea text2;
Socket socket;
ServerSocket serversocket;
boolean Con=false;
String str=null;
DataInputStream ini=null;
DataOutputStream outo=null;
Myframe() {
super("聊天室客户端");
setVisible(true);
setBounds(200,200,300,500);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(true);
if(socket!=null) {
try {
socket.close();
} catch(IOException ae) {
ae.printStackTrace();
}
}
if(serversocket!=null) {
try {
serversocket.close();
} catch(IOException ae) {
ae.printStackTrace();
}
}
try {
ini.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
outo.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
}
);
text2=new TextArea(25,10);
text2.setBackground(Color.magenta);
add(text2,"North");
text1=new TextField(20);
text1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str=text1.getText();
text2.append("/n"+"Client:"+str);
text1.setText("");
if(Con) {
try {
outo.writeUTF(str);
outo.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}

}
}
);
add(text1,"South");
Connect();
Mythread mm=new Mythread();
Thread tt=new Thread(mm);
tt.start();
}
public void Connect() {
try {
text2.setText("正在尝试连接服务器....");
socket=new Socket("127.0.0.1",8888);
text2.append("/n"+"已经自动连接上");
ini=new DataInputStream(socket.getInputStream());
outo=new DataOutputStream(socket.getOutputStream());
Con=true;
} catch (Exception ae) {
ae.printStackTrace();
}
}
class Mythread implements Runnable {
public void run() {
while(Con) {
String strstr=null;
try{
strstr=ini.readUTF();
} catch(IOException ae) {
text2.append("/n"+"已和服务器断开连接");
text2.append("/n"+"3秒后程序自动关闭");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(-1);
}
if(!(strstr.equals("A Client said:"+str))) {
text2.append("/n"+"Server:"+strstr);
}
}
}
}
}


服务器端:

package Server;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class ChatServer {

public static void main(String[] args) {
new Myframe();
}
}
class Myframe extends Frame {
TextField text1;
TextArea text2;
Socket socket;
boolean Con=false;
ServerSocket serversocket;
ArrayList<Client> clients=new ArrayList<Client>();
Myframe() {
super("聊天室服务器端");
setVisible(true);
setBounds(200,200,300,500);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(true);
if(socket!=null) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(serversocket!=null) {
try {
serversocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
for(int i=0;i<clients.size();i++) {
Client ccc=clients.get(i);
ccc.disopen();
}
System.exit(0);
}
}
);
text2=new TextArea(25,10);
text2.setBackground(Color.magenta);
add(text2,"North");
text1=new TextField(20);
text1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=text1.getText();
text2.append("/n"+"Server:"+str);
Mythread mm=new Mythread();
Thread tt=new Thread(mm);
tt.start();
}
}
);
add(text1,"South");
start();
}
class Mythread implements Runnable {
public void run() {
String str3=text1.getText();
for(int i=0;i<clients.size();i++) {
Client cc=clients.get(i);
cc.send(str3);
}
text1.setText("");
}
}
public void start() {
try {
serversocket=new ServerSocket(8888);
text2.setText("服务器正在等待客户连接中 。。。");
Con=true;
} catch(IOException ae) {
ae.printStackTrace();
text2.setText("服务器启动失败");
//System.exit(0);
}
while(Con) {
try {
socket=serversocket.accept();
Client client=new Client();
Thread t=new Thread(client);
t.start();
clients.add(client);
text2.append("/n"+"已连接上"+clients.size()+"个客户");
} catch(Exception ae) {
System.out.println("服务器已被关闭");
break;
}
}
}
class Client implements Runnable {
DataInputStream in3=null;
DataOutputStream out3=null;
Client() {
try {
in3=new DataInputStream(socket.getInputStream());
out3=new DataOutputStream(socket.getOutputStream());
} catch(Exception ae) {
ae.printStackTrace();
}
}
public void send(String str1) {
try {
out3.writeUTF(str1);
out3.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disopen() {
try {
in3.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out3.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(true) {
String str2=null;
try {
str2=in3.readUTF();
text2.append("/n"+"Client:"+str2);
} catch (IOException ae) {
try {
clients.remove(this);
text2.append("/n"+"一客户已退出聊天室");
text2.append("/n"+"已连接上"+clients.size()+"个客户");
in3.close();
out3.close();
} catch(Exception e) {
e.printStackTrace();
}
break;
}
for(int i=0;i<clients.size();i++) {
Client c=clients.get(i);
c.send("A Client said:"+str2);
}
}
}
}
}


写这个简单的Java版多人聊天室纯为了巩固Java SE基础(暂不考虑界面美化和个性化功能等细节),因为这样一个小应用程序综合应用了多线程、网络(TCP)、AWT、IO等众多重量级知识点,所谓“麻雀虽小,五脏俱全”~~~O(∩_∩)O~

Jar文件及源码下载地址:http://download.csdn.net/source/2616662

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息