2024年3月7日发(作者:)

import Layout;

import ;

import ea;

import eld;

import Event;

import Listener;

import Adapter;

import Event;

import putStream;

import tputStream;

import eption;

import ption;

import ;

import Exception;

import nHostException;

public class ChatClient extends Frame {

TextField tfTxt = new TextField();

TextArea taContent = new TextArea();

Socket s = null;

1

DataOutputStream dos = null;

DataInputStream dis = null;

private boolean bConnected = false;

Thread tRece = new Thread(new ReceThread());

public static void main(String[] args) {

new ChatClient().launchFrame();

}

public void launchFrame() {

ation(300, 200);

e(400, 400);

add(tfTxt, );

add(taContent, );

pack();

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

if(s != null) disConnect();

(0);

}

});

2

ionListener(new TFListener());

setVisible(true);

connect();

();

}

public void connect() {

try {

s = new Socket("127.0.0.1", 8888);

dos = new DataOutputStream(putStream());

dis = new DataInputStream(utStream());

n("connected!");

bConnected = true;

} catch (UnknownHostException e) {

tackTrace();

} catch (IOException e) {

n("服务器忙,请稍后再试!");

}

}

public void disConnect() {

3

try {

();

();

();

} catch (IOException e) {

tackTrace();

}

/*try {

bConnected = false;

();

} catch (InterruptedException e) {

tackTrace();

} finally {

try {

();

();

();

} catch (IOException e) {

tackTrace();

}

}*/

}

4

private class TFListener implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

String str = t().trim();

t("");

try {

TF(str);

();

} catch (IOException e) {

tackTrace();

}

}

}

private class ReceThread implements Runnable {

public void run() {

try {

while(bConnected) {

String str = F();

t(t() + str + 'n');

}

5

} catch(SocketException e) {

(0);;

} catch(EOFException e) {

n("退出了,bye-bye!");

}

catch(IOException e) {

tackTrace();

}

}

}

}

import putStream;

import tputStream;

import eption;

import ption;

import ception;

6

import Socket;

import ;

import Exception;

import ist;

import ;

public class ChatServer {

boolean started = false;

ServerSocket ss = null;

List clients = new ArrayList();

public static void main(String[] args) {

new ChatServer().start();

}

public void start() {

try {

ss = new ServerSocket(8888);

started = true;

} catch(BindException e) {

n("对不起,端口被占用!");

7

(0);

} catch (IOException e) {

tackTrace();

}

try {

while (started) {

Socket s = ();

Client c = new Client(s);

n("A client connected!");

new Thread(c).start();

(c);

}

} catch(IOException e) {

tackTrace();

} finally {

try {

();

} catch (IOException e) {

tackTrace();

}

}

8

}

class Client implements Runnable {

private Socket s;

private DataInputStream dis = null;

private boolean bConnected = false;

private DataOutputStream dos = null;

public Client(Socket s) {

this.s = s;

try {

dis = new DataInputStream(utStream());

dos = new DataOutputStream(putStream());

bConnected = true;

} catch (IOException e) {

tackTrace();

}

}

public void send(String str) {

try {

TF(str);

9

} catch (IOException e) {

(this);

n("对方退出了,我从list里面去除了!");

}

}

public void run() {

try {

while (bConnected) {

String str = F();

n(str);

for(int i=0; i<(); i++) {

Client c = (i);

(str);

}

/*for(Iterator it=or(); t();) {

Client c = ();

(str);

}*/

/*Iterator it = or();

10

while(t()) {

Client c = ();

(str);

}*/

}

} catch(EOFException e) {

n("client closed!");

} catch (IOException e) {

tackTrace();

} finally {

try {

if(dis != null) ();

if(dos != null) ();

if(s != null) ();

} catch (IOException e) {

tackTrace();

}

}

}

11

}

}

12