一个简单的聊天程序,客户端接收不到服务器返回的信息,检查很多遍不知道哪里错了,望大神耐心指教 10C

客户端代码

 import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;


public class chat {

    JFrame frame;
    JTextArea outputText;
    JTextField inputText;
    JButton sendButton;
    JPanel mainPanel;
    JPanel inputPanel;
    BufferedReader reader;
    Socket socket;
    PrintWriter writer;
    public static void main(String[] args) {

        chat chat=new chat();
        chat.setUpNetWorking();
        chat.buildGUI();

    }

    public void setUpNetWorking() {
        try {
            socket=new Socket("127.0.0.1",5000);
            InputStreamReader streamReader=new InputStreamReader(socket.getInputStream());
            reader=new BufferedReader(streamReader);

            writer=new PrintWriter(socket.getOutputStream());
        }catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void buildGUI(){

        outputText=new JTextArea("",8,20);
        outputText.setLineWrap(true);
        outputText.setEditable(false);
        JScrollPane scroller=new JScrollPane(outputText);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        inputText=new JTextField(15);
        inputText.addActionListener(new SendListener());
        sendButton=new JButton("send");
        inputPanel=new JPanel();
        inputPanel.setLayout(new BorderLayout());
        inputPanel.add(inputText,BorderLayout.CENTER);
        inputPanel.add(sendButton,BorderLayout.EAST);

        frame=new JFrame("Chat Windows");
        frame.getContentPane().add(scroller,BorderLayout.CENTER);
        frame.getContentPane().add(inputPanel,BorderLayout.SOUTH);
        frame.setVisible(true);
        frame.pack();
        inputText.requestFocus();
    }
    public class SendListener implements ActionListener{



        @Override
        public void actionPerformed(ActionEvent e) {

            outputText.setText(outputText.getText()+"me :"+inputText.getText()+"\n");
            writer.println(inputText.getText());
            writer.flush();
            System.out.println("client send it "+inputText.getText());
            inputText.setText("");
            inputText.requestFocus();
        }

        public class ReceiveRunnable implements Runnable {

            @Override
            public void run() {
                while(true){
                String message=null;
                try {
                    while((message=reader.readLine())!=null){
                        System.out.println("cilent Revive it "+message);

                    }
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
            }

        }
    }

}

服务器代码

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;


public class ChatServer {
    ArrayList<PrintWriter> inputStramList=new ArrayList<PrintWriter>();

    public static void main(String[] args) {
        ChatServer server=new ChatServer();
        server.go();

    }
    public void go(){
         try {
            ServerSocket serverSocket=new ServerSocket(5000);

            while(true){
                Socket clientSocket=serverSocket.accept();
                System.out.println("server connected it");
                PrintWriter writer=new PrintWriter(clientSocket.getOutputStream());
                inputStramList.add(writer);

                Thread t=new Thread(new ReceiveRunnable(clientSocket));
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public class ReceiveRunnable implements Runnable {
        BufferedReader reader;

        public ReceiveRunnable(Socket socket){
            try {
                reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));

            } catch (IOException e) {

            }
        }
        public void run() {
            String message;
            try {
                while((message=reader.readLine())!=null){
                    telleverbody(message);
                    System.out.println("server send it "+message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        private void telleverbody(String message) {
            Iterator<PrintWriter> it=inputStramList.iterator();
            while(it.hasNext()){
                PrintWriter writer=(PrintWriter) it.next();
                writer.println(message);
                writer.flush();
            }

        }
    }
}

5个回答

serversocket每次accept都会阻塞线程,等待一个新的客户链接,一旦有用户连接上这个端口,就会返回一个socket

EncoreLaw
EncoreLaw 听不太懂耶,可不可以说详细一点呀!
3 年多之前 回复

服务端代码有问题,while((message=reader.readLine())!=null) 首次连上肯定为空,然后这个线程就死掉了,相当于服务端自动断开连接了。

EncoreLaw
EncoreLaw 他判断为null那就不执行while里面的语句,为什么会死掉啊,如果要改需要怎么改
3 年多之前 回复
EncoreLaw
EncoreLaw 为什么会死掉啊
3 年多之前 回复

        inputText=new JTextField(15);
        sendButton=new JButton("send");
        sendButton.addActionListener(new SendListener());

我也是醉了,,

villare
villare 回复EncoreLaw: 你的不是这样写的眼
3 年多之前 回复
EncoreLaw
EncoreLaw 这样不可以吗,不是可以监听回车这个动作吗
3 年多之前 回复
villare
villare 居然把监听绑定到了输入框了。。。
3 年多之前 回复

如果想服务器和客户端不等待对方就能自由发数据,需要多线程

EncoreLaw
EncoreLaw 可不可以稍微讲解一下:D
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问