public void run()
{
try
{
while(true) //代码只执行一次,就退出
{
String message = in.readUTF();
if(message != null)
{
for(DataOutputStream o : map)
{
o.writeUTF(message);
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
{
try
{
while(true) //代码只执行一次,就退出
{
String message = in.readUTF();
if(message != null)
{
for(DataOutputStream o : map)
{
o.writeUTF(message);
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
只是你第二次in.readUTF();时是处于阻塞状态,
也就是在继续等待你的输入源。
有了输入源,才会继续执行下面的for循环,遍历输出流。
一直启动服务端,然后客户端发数据过来。
或者你可以在服务端设断点。
public void run() {
try {
while(true) //代码只执行一次,就退出
{
// 加上调试信息
System.out.println("准备读取信息");
String message = in.readUTF();
System.out.println("读取到信息");
if(message != null) {
for(DataOutputStream o : map) {
System.out.println("输出信息");
o.writeUTF(message);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
我发送的代码如下:
public void actionPerformed(ActionEvent e)
{
try
{
if(buttonList.peek() != null)
{
JButton delButton = buttonList.remove();
delButton.setIcon(new ImageIcon("img/noone.gif"));
}
JButton button = (JButton)e.getSource();
button.setIcon(RoomPanel.getIcon());
buttonList.push(button);
int i = list.indexOf(button);
out.writeUTF(button.getIcon().toString() + "|" + String.valueOf(i) + "|" +name);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
是不是在这里因为其他原因阻塞了?
for(DataOutputStream o : map)
{
o.writeUTF(message);
}把这段屏蔽掉 然后你
system.out.println(message);
看看会有什么效果
try-catch在循环外面