EncoreLaw
EncoreLaw
2016-08-24 11:26

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

10

客户端代码

 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条回答