那位高手可以帮我看一下我的code什么问题,为什么在count==2的时候客户端就会死机?谢谢
package slics;/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2007
 * Company:
 * @author
 * @version 1.0
 */
import java.net.*;
import java.io.*;
import java.math.*;
public class Calcuserver { public static void main(String args[]){
 int count;        // Set up name database, normally this would be done in a much
        // more sophisticated way using a relational database        System.out.println("Starting the Slics server...");
        try
        {  System.out.println("creating server socket");
           ServerSocket ss = new ServerSocket(1024);
           System.out.println("finish creating");
           count=1;
           // Set up the streams which the clients will access           while(true)
           {
            System.out.println("1");
            Socket sock =ss.accept();
            System.out.println("2");
            InputStream is = sock.getInputStream();
            System.out.println("3");
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            System.out.println("4");
            OutputStream os = sock.getOutputStream();
            System.out.println("5");
            PrintWriter pw = new PrintWriter(os, true);
             String a="";
             String b="";
             String c="";
             String value="";
             double A=0;
             double M=0;
             double L=0;
             double I=0;
             double i=0;
             double round=0;
               System.out.println("Start reading ");
               //Read value request               if(count==1){
               System.out.println("reading a");
               a = bf.readLine();
               //if(!a.equals("0"))
               L=Double.parseDouble(a);
              }
              System.out.println("finish a start read b");
                if(count==2){
                System.out.println("reading b");
               b= bf.readLine();
               //if(!b.equals("0"))
               I=Double.parseDouble(b)/100;
               i=I/12;
               }
              System.out.println("finish b start read c");
               if(count==3){
               System.out.println("reading c");
               c = bf.readLine();
               //if(!c.equals("0"))
               M=Double.parseDouble(c);
               A=L*i/(1-java.lang.Math.pow((1+i),-M));
               //round A to 2 decimal places
               round=0.01 * (int) (A *100 + 0.5);
               }               if(a.equals("0")){
               System.out.println("Clientsstop1");
               ss.accept();
               }
               //
               System.out.println("caluating value");
               value="$"+String.valueOf(round);
               System.out.println(value);
               //if(value == null)
                  // Invalid name provided
                  //value = "value must not be null";
               //send back host name or error message to client
               if(count==3){               pw.println(value);
               count=0;
               }else{pw.println("");}               System.out.println("Serviced connection "+count);
               count++;
               //ss.accept();
           }
        }catch
        (Exception e){System.out.println("Problem setting up connection "+e);}
    System.out.println("Server closed down");
    }}

解决方案 »

  1.   

    好的。我是说客户端会死机,当我发送第二个数的时候
    package slicsclient;/**
     * Title:
     * Description:
     * Copyright:    Copyright (c) 2007
     * Company:
     * @author
     * @version 1.0
     */import java.awt.*;
    import com.borland.jbcl.layout.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class Calcuclient extends JFrame {//
    // Instance variables required for net connection
    static Socket ss = null;
    static InputStream is = null;
    static BufferedReader bf = null;
    static OutputStream os = null;
    static PrintWriter pw = null;
    //******************** Ignore until next line of asterisks *********************
      XYLayout xYLayout1 = new XYLayout();
      JButton goButton = new JButton();
      JTextField InputValue = new JTextField();
      JTextField addressField = new JTextField();
      JLabel Value = new JLabel();
      JLabel addressLabel = new JLabel();
      JButton quitButton = new JButton();  public Calcuclient() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      private void jbInit() throws Exception {
        goButton.setText("Go");
        goButton.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            goButton_actionPerformed(e);
          }
        });
        this.getContentPane().setLayout(xYLayout1);
        Value.setText("Value");
        addressLabel.setText("Monthly Installment amount");
        quitButton.setText("Quit");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            quitButton_actionPerformed(e);
          }
        });
        xYLayout1.setWidth(299);
        xYLayout1.setHeight(332);
        this.getContentPane().add(Value, new XYConstraints(17, 31, -1, -1));
        this.getContentPane().add(InputValue, new XYConstraints(15, 52, 257, -1));
        this.getContentPane().add(goButton, new XYConstraints(37, 275, 62, 24));
        this.getContentPane().add(quitButton, new XYConstraints(172, 271, 74, 24));
        this.getContentPane().add(addressField, new XYConstraints(13, 121, 260, 146));
        this.getContentPane().add(addressLabel, new XYConstraints(18, 91, -1, -1));
      }  public static void main(String[] args)
      {
        try
        {
           ss = new Socket(InetAddress.getLocalHost(),1024);
           // Set up the streams which the clients will access
           is = ss.getInputStream();
           bf = new BufferedReader(new InputStreamReader(is));
           os = ss.getOutputStream();
           pw = new PrintWriter(os,true);
         }
         catch(Exception e)
         {System.out.println("Trouble with initial connection"+e);}
         System.out.println("***Student code being executed**");
         Calcuclient nc = new Calcuclient();
         nc.setSize(300,350);
         nc.setVisible(true);
      }void quitButton_actionPerformed(ActionEvent e) {
    try
    {
       //Tell the server to close down
     pw.println("0");
                     System.out.println("Clientsstop");
     ////ss.close();
     //is.close();
     //os.close();
     //bf.close();
     //pw.close();
    }catch(Exception ex){};
         System.exit(0);
    }//******************** End of ignore section ***********************************
      void goButton_actionPerformed(ActionEvent e)
      {
         try
         {
            // Extract the string from the 'nameField' text field in the frame and send to server
            pw.println(InputValue.getText());
            System.out.println(""+InputValue.getText());
            // Get reply from server your code should go below, it should extract
            // the string from the buffered reader 'bf' and place it in the string variable
            // 'reply', only one line of java code required here
            String reply = bf.readLine();
            // Display the string 'reply' in the field 'addressField' within the frame;
            // you will need to declare 'reply' as part of your solution above
            // Insert the code below to display the address in the address field
            addressField.setText(reply);     }
         catch (Exception ex)
           {System.out.println("Problem communicating with slics server"+ex);}
      }}
      

  2.   

    服务器端又不是多线程,运行一遍后就over了,客户端可不死机了
      

  3.   

    package slics;/**
     * Title:
     * Description:
     * Copyright:    Copyright (c) 2007
     * Company:
     * @author
     * @version 1.0
     */
    import java.net.*;
    import java.io.*;
    import java.math.*;
    public class Calcuserver { public static void main(String args[]){
     int count;        // Set up name database, normally this would be done in a much
            // more sophisticated way using a relational database        System.out.println("Starting the Slics server...");
            try
            {  System.out.println("creating server socket");
               ServerSocket ss = new ServerSocket(1024);
               System.out.println("finish creating");
               count=1;
               // Set up the streams which the clients will access            //accept 放在这里。
                Socket sock =ss.accept();
               while(true)
               {
                System.out.println("1");
    //把这句话移到循环外            Socket sock =ss.accept();
                System.out.println("2");
                .....
               }
            }catch
            (Exception e){System.out.println("Problem setting up connection "+e);}
        System.out.println("Server closed down");
        }}
      

  4.   

    /**
       @author Cay Horstmann
       @version 1.20 2004-08-03
    */import java.io.*;
    import java.net.*;
    import java.util.*;/**
       This program implements a multithreaded server that listens to port 8189 and echoes back 
       all client input.
    */
    public class ThreadedEchoServer
    {  
       public static void main(String[] args )
       {  
          try
          {  
             int i = 1;
             ServerSocket s = new ServerSocket(8189);         while (true)
             {  
                Socket incoming = s.accept();
                System.out.println("Spawning " + i);
                Runnable r = new ThreadedEchoHandler(incoming, i);
                Thread t = new Thread(r);
                t.start();
                i++;
             }
          }
          catch (IOException e)
          {  
             e.printStackTrace();
          }
       }
    }/**
       This class handles the client input for one server socket connection. 
    */
    class ThreadedEchoHandler implements Runnable

       /**
          Constructs a handler.
          @param i the incoming socket
          @param c the counter for the handlers (used in prompts)
       */
       public ThreadedEchoHandler(Socket i, int c)
       { 
          incoming = i; counter = c; 
       }   public void run()
       {  
          try
          {  
             try
             {
                InputStream inStream = incoming.getInputStream();
                OutputStream outStream = incoming.getOutputStream();
                
                Scanner in = new Scanner(inStream);         
                PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
                
                out.println( "Hello! Enter BYE to exit." );
                
                // echo client input
                boolean done = false;
                while (!done && in.hasNextLine())
                {  
                   String line = in.nextLine();            
                   out.println("Echo: " + line);            
                   if (line.trim().equals("BYE"))
                      done = true;
                }
             }
             finally
             {
                incoming.close();
             }
          }
          catch (IOException e)
          {  
             e.printStackTrace();
          }
       }   private Socket incoming;
       private int counter;
    }
      

  5.   

    我没有运行,因为我的机没有装com.borland包,我想是因为
    服务端用readLien()方法,这个方法是接收以"\n"为尾的数据,
    而客户端只是用getText()方法,没有发送"\n"字符,所以会死机,
    你试试,不知正不正确