class ReadWorkThread extends Thread {
public void run() {
try {
while (!exitFlag) {
String str = in.readUTF();
if (str != null) {
ui.receiveTF.setString(str);
}
}
} catch (IOException e) {
if (!exitFlag)
ui.state.setText("读取数据失败");
}
}
}
class WriteWorkThread extends Thread {
DataOutputStream out = null;
public WriteWorkThread(DataOutputStream out){
this.out = out;
}
public void run() {
try {
while (!exitFlag) {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (exitFlag)// 可能因为关闭操作被打断
break;
if (sendText != null)
out.writeUTF(sendText);
out.flush();
}
}
out.close();
} catch (IOException e) {
if (!exitFlag)
ui.state.setText("写数据失败");
}
}
}
这段程序读线程在第一次读取的时候没有任何问题,但是第二次就会发生IOException
"读取数据失败",不知道为什么?
public void run() {
try {
while (!exitFlag) {
String str = in.readUTF();
if (str != null) {
ui.receiveTF.setString(str);
}
}
} catch (IOException e) {
if (!exitFlag)
ui.state.setText("读取数据失败");
}
}
}
class WriteWorkThread extends Thread {
DataOutputStream out = null;
public WriteWorkThread(DataOutputStream out){
this.out = out;
}
public void run() {
try {
while (!exitFlag) {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (exitFlag)// 可能因为关闭操作被打断
break;
if (sendText != null)
out.writeUTF(sendText);
out.flush();
}
}
out.close();
} catch (IOException e) {
if (!exitFlag)
ui.state.setText("写数据失败");
}
}
}
这段程序读线程在第一次读取的时候没有任何问题,但是第二次就会发生IOException
"读取数据失败",不知道为什么?
try {
wait();
} catch (InterruptedException e) {
你的代码不全吧,这里在等待,没有看到你notify();或notifyAll()方法
java.io.IOException:存储空间不足,无法处理此命令
这是什么错误?
然后,各自写连对方端口的客户端,监听端口的方法只接收输入流,
连接端口的客户端只发送输出流,把逻辑搞清楚,
思路就这些,参考下