java awt聊天室_Java AWT实现的简单的多人在线聊天室

客户端:

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 clients=new ArrayList();

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

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

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

Client c=clients.get(i);

c.send("A Client said:"+str2);

}

}

}

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值