消息未从客户端发送到服务器 - Java

时间:2015-04-22 10:18:34

标签: java client server lan

我有一个Server-Client程序,我使用JLabel向客户端发送一个小小的消息。当从服务器收到该消息时,特定客户端必须立即发送响应。但它没有发送任何消息。有人可以看看我的代码并告诉我我的错误在哪里吗?

// SERVER

void connect_clients()
    {
        try {
            ServerSocket listener = new ServerSocket(7700);
            jButton1.setText("Server Running!");
            jButton1.setEnabled(false);
                while (true) {

                    socket = listener.accept();
                    socketList.add(socket);
                    //socketList.add(listener.accept());
                     BufferedReader ed = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String tmp = ed.readLine();
             System.out.print("I Recieved :"+tmp);

                }

            }
        catch(IOException ex)
        {
            JOptionPane.showMessageDialog(null,ex);
        }
    }

// CLIENT

 void connect_server() throws IOException
    {
        try {
            // TODO code application logic here
            String serverAddress = JOptionPane.showInputDialog(
                    "Enter IP Address of a machine that is\n" +
                            "running the date service on port 9090:");
            s = new Socket(serverAddress, 7700);

            while(true){
            BufferedReader input =
            new BufferedReader(new InputStreamReader(s.getInputStream()));
            String answer = input.readLine();
            System.out.println(answer);
                if(answer != null)
                {
                    PrintStream pr = new PrintStream(s.getOutputStream());
                    InputStreamReader rd = new InputStreamReader(System.in);
                    BufferedReader ed = new BufferedReader(rd);
                    String temp = ed.readLine();
                    pr.println(temp);

                    JOptionPane.showMessageDialog(null,"Answer is not null");  //THIS WORKS
               }

            }

          }
       catch (ConnectException e) {

            JOptionPane.showMessageDialog(null, e);
        }
         catch (SocketException e) {

            JOptionPane.showMessageDialog(null, e);
        }
    }

1 个答案:

答案 0 :(得分:1)

您在实施中遗漏的一些要点:

  • 流和套接字永远不会关闭
  • 在客户端我没有看到无限循环的意义
  • 客户端应通过输出流发送消息来初始化通信(不要先尝试先读取)

举一个简单的例子,步骤应该是:

  1. 启动服务器以进行侦听,并在建立连接后阅读消息(您已做)
  2. 客户端应通过输出流发送消息并关闭流和插座
  3. 服务器应关闭已建立连接的流和套接字
  4. 示例:

    //Server
    socket = listener.accept();
    BufferedReader ed = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    BufferedWriter pr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream());
    String tmp = ed.readLine();
    System.out.print("I Recieved :"+tmp);
    String msg = "Message received";
    pr.write(msg,0,msg.length());
    pr.newLine();
    ed.close();
    pr.close();
    socket.close();
    
    //Client
    BufferedWriter pr = new BufferedWriter(new OutputStreamWriter(s.getOutputStream());
    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String sendMessage = "Send Message";
    pr.write(msg,0,msg.length());
    pr.newLine();
    String answer = input.readLine();
    System.out.println(answer);
    JOptionPane.showMessageDialog(null,"Answer is not null"); 
    input.close();
    pr.close();
    s.close();
    

    <强>更新

    连续读取输入流:

    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String line;
    while((line=input.readLine())!=null){ 
      //do something with line
    }