//ChatServerjava文件
import javaio*;
import *;
import javautil*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer()start();
}
public void start() {
try {
ss = new ServerSocket();
started = true;
} catch (BindException e) {
Systemoutprintln(端口使用中);
Systemoutprintln(请关掉相关程序并重新运行服务器!);
Systemexit();
} catch (IOException e) {
eprintStackTrace();
}
try {
while(started) {
Socket s = ssaccept();
Client c = new Client(s);
Systemoutprintln(a client connected!);
new Thread(c)start();
clientsadd(c);
//disclose();
}
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
ssclose();
} catch (IOException e) {
// TODO Autogenerated catch block
eprintStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
thiss = s;
try {
dis = new DataInputStream(sgetInputStream());
dos = new DataOutputStream(sgetOutputStream());
bConnected = true;
} catch (IOException e) {
eprintStackTrace();
}
}
public void send(String str) {
try {
doswriteUTF(str);
} catch (IOException e) {
clientsremove(this);
Systemoutprintln(对方退出了!我从List里面去掉了!);
//eprintStackTrace();
}
}
public void run() {
try {
while(bConnected) {
String str = disreadUTF();
Systemoutprintln(str);
for(int i=; i<clientssize(); i++) {
Client c = clientsget(i);
csend(str);
//Systemoutprintln( a string send !);
}
/*
for(Iterator<Client> it = erator(); ithasNext(); ) {
Client c = itnext();
csend(str);
}
*/
/*
Iterator<Client> it = erator();
while(ithasNext()) {
Client c = itnext();
csend(str);
}
*/
}
} catch (EOFException e) {
Systemoutprintln(Client closed!);
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
if(dis != null) disclose();
if(dos != null) dosclose();
if(s != null) {
sclose();
//s = null;
}
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
}
//ChatClientjava文件
import javaawt*;
import javaawtevent*;
import javaio*;
import *;
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient()launchFrame();
}
public void launchFrame() {
setLocation( );
thissetSize( );
add(tfTxt BorderLayoutSOUTH);
add(taContent BorderLayoutNORTH);
pack();
thisaddWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg) {
disconnect();
Systemexit();
}
});
tfTxtaddActionListener(new TFListener());
setVisible(true);
connect();
tRecvstart();
}
public void connect() {
try {
s = new Socket( );
dos = new DataOutputStream(sgetOutputStream());
dis = new DataInputStream(sgetInputStream());
Systemoutprintln(connected!);
bConnected = true;
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
public void disconnect() {
try {
dosclose();
disclose();
sclose();
} catch (IOException e) {
eprintStackTrace();
}
/*
try {
bConnected = false;
tRecvjoin();
} catch(InterruptedException e) {
eprintStackTrace();
} finally {
try {
dosclose();
disclose();
sclose();
} catch (IOException e) {
eprintStackTrace();
}
}
*/
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxtgetText()trim();
//taContentsetText(str);
tfTxtsetText();
try {
//Systemoutprintln(s);
doswriteUTF(str);
dosflush();
//dosclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while(bConnected) {
String str = disreadUTF();
//Systemoutprintln(str);
taContentsetText(taContentgetText() + str + \n);
}
} catch (SocketException e) {
Systemoutprintln(退出了bye!);
} catch (EOFException e) {
Systemoutprintln(推出了bye bye!);
} catch (IOException e) {
eprintStackTrace();
}
}
}
}