具体情况是这样在同一台电脑上起了两个客户端都输入的ip地址是:127.0.0.1但是发送消息给服务器端时服务器端不转发我怎么也找不到这时为什么求解释啊
import java.io.*;
import java.net.*;
import java.util.*;public class SecurityChatServer {
boolean started = false;
ServerSocket ss = null;

List<Client> clients = new ArrayList<Client>();

public static void main(String[] args) {
new SecurityChatServer().start();
}

public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}

try {

while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
//dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;

public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String str) {
try {
dos.writeUTF(str);
//System.out.println(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!我从List里面去掉了!");
//e.printStackTrace();
}
}

public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
//System.out.println(str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
System.out.println(str);
//System.out.println(" a string send !");
}
}
} catch(SocketException e){
System.out.println("客户端退出了!");
}catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null)  {
s.close();
//s = null;
}

} catch (IOException e1) {
e1.printStackTrace();
}


}
}

}
}
//以上是服务器端
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class SecurityChatClient extends Frame {
private TextArea ta = new TextArea(10,50);
private TextField tf = new TextField();
private TextField tf2 = new TextField();
private Button b = new Button("输入ip");
private Panel p1 = new Panel();
Socket s = null;
InetAddress address = null;
DataOutputStream dos = null;
DataInputStream dis = null;
boolean bConnected = false;

public static void main(String[] args){
new SecurityChatClient().launchFrame();
}

public void launchFrame(){
p1.setLayout(new BorderLayout());
p1.add(tf,BorderLayout.WEST);
p1.add(tf2,BorderLayout.CENTER);
p1.add(b,BorderLayout.EAST);
this.add(ta,BorderLayout.NORTH);
this.add(p1,BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b.addActionListener(new ButtonEvent1());
new Thread(new Reclient()).start();
tf.addActionListener(new TFListener());
}

public void connect(){
try {
s = new Socket(address,8888);
bConnected = true;
System.out.println("connected!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

class ButtonEvent1 implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
String str = tf2.getText();
try {
 address = InetAddress.getByName(str);
System.out.println(address);
 connect();
tf2.setText("");
} catch (UnknownHostException e1) {
e1.printStackTrace();
}

}

}

class Reclient implements Runnable{ @Override
public void run() {
while(bConnected == true){
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();
}
}
}

}

class TFListener implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
try {
String str = tf.getText().trim();
tf.setText("");
ta.setText(str);
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
dos.flush();
    } catch (IOException e1) {
 e1.printStackTrace();
 }
  }
}}
以上是客户端

解决方案 »

  1.   

    你都知道问题了,那就去那个问题找啊,不转发肯定就是转发的线程出现了问题,调试一下是不是客户端连接时,根本没有把他加到容器里,测试一下不就可以了,看看client.size()是不是为0,没保存客户端连接当然转发不了
      

  2.   

    客户端有问题,代码改下供参考:class Reclient implements Runnable
    {
    @Override
    public void run() 
    {
    while(true)//原代码线程马上结束了。因为在输入ip前,bConnected=false.
    {
    if(bConnected == true)
    {
    try 
    {
    dis=new DataInputStream(s.getInputStream());//初始化dis.
    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();
    }
    }
    }
    }
    }