sjyioi 2012-12-28 01:29
浏览 278
已采纳

java我编写的一个聊天程序出现了一些问题不知道怎么解决

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();
     }
  }
}

}
//以上是客户端
我起了服务器端,然后再在我的电脑起了两个客户端然后连个客户端都输入的是127.0.0.1这个ip地址但是服务器端能接收到信息但是不返回给另外一个客户端。求说下这个问题怎么解决,我找了好半天都没有找到啊。我现在发现的问题是客户端不能收到,而且在客户端发一个信息的话在服务器端回打两边但是就是不返回给另一个客户端。但是我不知道怎么解决这个问题。

  • 写回答

1条回答 默认 最新

  • microPAO 2012-12-28 10:47
    关注

    没有全部调试,客户端代码存在写问题:
    [code="java"]
    class Reclient implements Runnable {

        @Override
        public void run() {
            while (true) {
                if(bConnected){
                    try {
                        System.out.println("等待接受数据...");
                        String str = dis.readUTF();
                        System.out.println("receive: " + str);
                        ta.setText(ta.getText() + str + '\n');
                    } catch (SocketException e) {
                        System.out.println("对方退出了!");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
            }
        }
    }
    

    [/code]

    按照你原先的代码,Reclient线程判断bConnected后会直接退出。
    另外
    [code="java"]
    DataOutputStream dos = null;
    DataInputStream dis = null;
    [/code]
    下面没有初始化,会抛出空指针异常。
    应该还有写别的问题,你自己仔细看看吧。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!