我是想做一个间单的用户分3次每次发送一个数,然后将3个数相乘答案发还给用户。用户在发送第三个数的时候答案就会返回。客户端屎没问题的,我的问题是怎样利用readline()再服务器端读取3个数然后放在一起计算?我尝试使用计数器在下列的代码中 但是好像有点问题 可否赐教以下?
package slics;/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2007
 * Company:
 * @author
 * @version 1.0
 */
import java.net.*;
import java.io.*;
public class Calcuserver { public static void main(String args[]){
 int count;
      
      
        
        String a="";
        String b="";
        String c="";
        String value="";
        // 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
        {
           ServerSocket ss = new ServerSocket(1024);
           count=1;
           // Set up the streams which the clients will access
           Socket sock =ss.accept();
           InputStream is = sock.getInputStream();
           BufferedReader bf = new BufferedReader(new InputStreamReader(is));
           OutputStream os = sock.getOutputStream();
           PrintWriter pw = new PrintWriter(os, true);
           while(true)
           {
               //Read value request
               if(count==1){
               a = bf.readLine();
               }
              
                if(count==2){
               b= bf.readLine();
               }
               
               if(count==3){
               c = bf.readLine();
               }
              
               if(a.equals("Clientstop"))
  //Client has disconnected close down server
                  break;
                  ss.accept();
               int temp=Integer.parseInt(a)*Integer.parseInt(b)*Integer.parseInt(c);
               value=String.valueOf(temp);
               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++;
           }
        }catch
        (Exception e){System.out.println("Problem setting up connection "+e);}
    System.out.println("Server closed down");
    }}

解决方案 »

  1.   

    我是想做一个间单的用户分3次每次发送一个数,然后将3个数相乘答案发还给用户。用户在发送第三个数的时候答案就会返回。客户端是没问题的,我的问题是怎样利用readline()在服务器端读取3个数然后放在一起计算?我尝试使用计数器在下列的代码中  但是好像有点问题 因为每次发送数的时候我的客户端都会拿到“java.net.SocketException: Connection reset by peer: JVM_recv in socket input stream read“  可否赐教一下?  
    package  slics;  
     
    /**  
     *  Title:  
     *  Description:  
     *  Copyright:        Copyright  (c)  2007  
     *  Company:  
     *  @author  
     *  @version  1.0  
     */  
    import  java.net.*;  
    import  java.io.*;  
    public  class  Calcuserver  {  
     
     public  static  void  main(String  args[]){  
     int  count;  
                 
                 
                     
                   String  a="";  
                   String  b="";  
                   String  c="";  
                   String  value="";  
                   //  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  
                   {  
                         ServerSocket  ss  =  new  ServerSocket(1024);  
                         count=1;  
                         //  Set  up  the  streams  which  the  clients  will  access  
                         Socket  sock  =ss.accept();  
                         InputStream  is  =  sock.getInputStream();  
                         BufferedReader  bf  =  new  BufferedReader(new  InputStreamReader(is));  
                         OutputStream  os  =  sock.getOutputStream();  
                         PrintWriter  pw  =  new  PrintWriter(os,  true);  
                         while(true)  
                         {  
                                 //Read  value  request  
                                 if(count==1){  
                                 a  =  bf.readLine();  
                                 }  
                                 
                                   if(count==2){  
                                 b=  bf.readLine();  
                                 }  
                                   
                                 if(count==3){  
                                 c  =  bf.readLine();  
                                 }  
                                 
                                 if(a.equals("Clientstop"))  
                               //Client  has  disconnected  close  down  server  
                                       break;  
                                       ss.close();  
                                 int  temp=Integer.parseInt(a)*Integer.parseInt(b)*Integer.parseInt(c);  
                                 value=String.valueOf(temp);  
                                 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++;  
                         }  
                   }catch  
                   (Exception  e){System.out.println("Problem  setting  up  connection  "+e);}  
                                   System.out.println("Server  closed  down");  
           }  
     
    }