本人系新手,刚通过视频教程学习玩java基础。然后尝试通过线程编写聊天室雏形来巩固下所学。
存在些bug。。运行时正常。当关掉某一聊天窗口,就报错经观察,可能是readUTF()抛异常。一直解决不了。。
求助于坛友诶。希望帮忙琢磨一下。有点乱,不好意思哈。 源代码如下:Client端:import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class ChatClient {
public static void main(String[] args) {
new MyFrame().lanuchFrame();
}
}
class MyFrame extends Frame {
private static TextField tf = null;
private TextArea ta = null;
private Label l = null;
private Socket s = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
private Thread t = null;
private boolean b =false; public void lanuchFrame() {
setTitle("Chat V1.0");
setLocation(400, 100);
Button b1 = new Button("发送");
Button b2 = new Button("关闭");
b2.setActionCommand("关闭");
b1.setActionCommand("发送");
tf = new TextField();
l = new Label();
ta = new TextArea("");
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.setLayout(new GridLayout(2,1));
p1.add(ta);
p1.add(tf);
add(l,BorderLayout.NORTH);
add(p1,BorderLayout.CENTER);
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
p2.add(b2);
p2.add(b1);
add(p2,BorderLayout.SOUTH);
pack();
setVisible(true);
connect();
tf.addActionListener(new B1Monitor());
b1.addActionListener(new B1Monitor());
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while(e.getActionCommand() == "关闭") {
setVisible(false);
disConnect();
System.exit(0);
}
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
disConnect();
System.exit(0);
}
});
}
public void connect() {
try {
s = new Socket("127.0.0.1",9999);
t = new Thread(new Servers(s));
t.start();
dos = new DataOutputStream(s.getOutputStream());
l.setText("IPAdress: " + s.getInetAddress() + " Port# " + String.valueOf(s.getLocalPort()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}public void disConnect() {
try {
b = false;
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
try {
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}private class Servers implements Runnable {
private Socket s = null;
Servers(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
b = true;
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (b) {
try {
String str = dis.readUTF();
if (!str.startsWith(s.getInetAddress() + " #"
+ s.getLocalPort()))
ta.append(str);
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("推出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}private class B1Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
ta.append("=== " + new Date() + " ===" + "\n" + tf.getText() + "\n");
try {
dos.writeUTF(s.getInetAddress() +" #" + s.getLocalPort() + " " + new Date() + "\n" + " " + tf.getText() + "\n");
tf.setText(null);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}}
Server端:
import java.io.*;
import java.net.*;
import java.util.*;public class ChatServer {
private boolean flag = false;
private ServerSocket ss = null;
private List<Clients> arr = null;
public static void main(String[] args) {
new ChatServer().lanuchServer();
}
public void lanuchServer() {
try {
ss = new ServerSocket(9999);
arr = new ArrayList<Clients>();
flag = true;
while (flag) {
Socket s = ss.accept();
Clients c = new Clients(s);
arr.add(c);
new Thread(c).start();
}
} catch (IOException e) {
try {
arr.remove(this);
ss.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
class Clients implements Runnable {
private Socket s = null;
private boolean b = false;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private String str = null;
Clients(Socket s) {
this.s = s;
try {
b = true;
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (b) {
try {
str = dis.readUTF();
for (int i = 0; i < arr.size(); i++) {
Clients c = arr.get(i);
c.dos.writeUTF(str);
c.dos.flush();
}
System.out.println(str);
} catch (IOException e) {
try {
dis.close();
dos.close();
System.out.println(s.getInetAddress() + " #"
+ s.getLocalPort() + " connected qiut!");
arr.remove(this);
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
}
}
存在些bug。。运行时正常。当关掉某一聊天窗口,就报错经观察,可能是readUTF()抛异常。一直解决不了。。
求助于坛友诶。希望帮忙琢磨一下。有点乱,不好意思哈。 源代码如下:Client端:import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class ChatClient {
public static void main(String[] args) {
new MyFrame().lanuchFrame();
}
}
class MyFrame extends Frame {
private static TextField tf = null;
private TextArea ta = null;
private Label l = null;
private Socket s = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
private Thread t = null;
private boolean b =false; public void lanuchFrame() {
setTitle("Chat V1.0");
setLocation(400, 100);
Button b1 = new Button("发送");
Button b2 = new Button("关闭");
b2.setActionCommand("关闭");
b1.setActionCommand("发送");
tf = new TextField();
l = new Label();
ta = new TextArea("");
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.setLayout(new GridLayout(2,1));
p1.add(ta);
p1.add(tf);
add(l,BorderLayout.NORTH);
add(p1,BorderLayout.CENTER);
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
p2.add(b2);
p2.add(b1);
add(p2,BorderLayout.SOUTH);
pack();
setVisible(true);
connect();
tf.addActionListener(new B1Monitor());
b1.addActionListener(new B1Monitor());
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while(e.getActionCommand() == "关闭") {
setVisible(false);
disConnect();
System.exit(0);
}
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
disConnect();
System.exit(0);
}
});
}
public void connect() {
try {
s = new Socket("127.0.0.1",9999);
t = new Thread(new Servers(s));
t.start();
dos = new DataOutputStream(s.getOutputStream());
l.setText("IPAdress: " + s.getInetAddress() + " Port# " + String.valueOf(s.getLocalPort()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}public void disConnect() {
try {
b = false;
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
try {
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}private class Servers implements Runnable {
private Socket s = null;
Servers(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
b = true;
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (b) {
try {
String str = dis.readUTF();
if (!str.startsWith(s.getInetAddress() + " #"
+ s.getLocalPort()))
ta.append(str);
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("推出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}private class B1Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
ta.append("=== " + new Date() + " ===" + "\n" + tf.getText() + "\n");
try {
dos.writeUTF(s.getInetAddress() +" #" + s.getLocalPort() + " " + new Date() + "\n" + " " + tf.getText() + "\n");
tf.setText(null);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}}
Server端:
import java.io.*;
import java.net.*;
import java.util.*;public class ChatServer {
private boolean flag = false;
private ServerSocket ss = null;
private List<Clients> arr = null;
public static void main(String[] args) {
new ChatServer().lanuchServer();
}
public void lanuchServer() {
try {
ss = new ServerSocket(9999);
arr = new ArrayList<Clients>();
flag = true;
while (flag) {
Socket s = ss.accept();
Clients c = new Clients(s);
arr.add(c);
new Thread(c).start();
}
} catch (IOException e) {
try {
arr.remove(this);
ss.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
class Clients implements Runnable {
private Socket s = null;
private boolean b = false;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private String str = null;
Clients(Socket s) {
this.s = s;
try {
b = true;
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (b) {
try {
str = dis.readUTF();
for (int i = 0; i < arr.size(); i++) {
Clients c = arr.get(i);
c.dos.writeUTF(str);
c.dos.flush();
}
System.out.println(str);
} catch (IOException e) {
try {
dis.close();
dos.close();
System.out.println(s.getInetAddress() + " #"
+ s.getLocalPort() + " connected qiut!");
arr.remove(this);
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货