62,621
社区成员
发帖
与我相关
我的任务
分享import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Client extends Frame {
DataOutputStream dos;
DataInputStream dis;
Socket s = null;
TextField tf = new TextField();
TextArea ta = new TextArea();
private boolean ifConnect = false;
Thread recvThread = new Thread(new RecvThread());
public static void main(String[] args) {
new Client().runFrame();
}
public void runFrame() {
this.setLocation(400,300);
this.setSize(400,300);
add(ta,BorderLayout.NORTH);
add(tf,BorderLayout.SOUTH);
this.pack();
this.addWindowListener(new WindowsMonitor());
tf.addActionListener(new TextFieldLietener());
setVisible(true);
connect();
recvThread.start();
}
private void connect() {
try {
s = new Socket("127.0.0.1",8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
ifConnect = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private class WindowsMonitor extends WindowAdapter {
public void windowClosing(WindowEvent e) {
try {
ifConnect = false;
dos.close();
dis.close();
s.close();
} catch(IOException e1) {
e1.printStackTrace();
}
/*try {
ifConnect = false;
recvThread.join();
dos.close();
dis.close();
s.close();
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}*/
System.exit(0);
}
}
private class TextFieldLietener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String str = tf.getText();
tf.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
while(ifConnect) {
try{
String str = dis.readUTF();
// System.out.println(str);
ta.setText(ta.getText() + str + '\n');
} catch(SocketException e) {
System.out.println("退出了!");
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
ServerSocket ss = null;
boolean started = false;
List<ClientListen> clients = new ArrayList<ClientListen>();
public static void main(String[] args) {
new Server().starting();
}
public void starting() {
try {
ss = new ServerSocket(8888);
started = true;
} catch(BindException e ) {
System.out.println("端口使用中...");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
//System.out.println("a client connected!");
ClientListen c = new ClientListen(s);
new Thread(c).start();
clients.add(c);
// dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientListen implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean ifstart = false;
public ClientListen(Socket s ) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
ifstart = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!");
//e.printStackTrace();
}
}
public void run() {
try{
while(ifstart) {
String str = dis.readUTF();
//System.out.println(str);
for(int i = 0;i < clients.size();i++) {
ClientListen c = clients.get(i);
c.send(str);
}
}
} catch(SocketException e) {
clients.remove(this);
System.out.println("一个客户端已经退出!");
}
catch (EOFException e) {
System.out.println("客户端退出!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}