首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

急socket 的有关问题

2012-01-05 
急啊!!socket的问题那位高手可以帮我看一下我的code什么问题,为什么在count2的时候客户端就会死机?谢谢p

急啊!!socket 的问题
那位高手可以帮我看一下我的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 ");
        }

}

[解决办法]
奇怪,你的程序在我这里并没有死机现象,能不能把你的客户端程序也贴出来?
[解决办法]
服务器端又不是多线程,运行一遍后就over了,客户端可不死机了
[解决办法]
帮顶~闪人~
[解决办法]
/**
@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;
}


------解决方案--------------------


我没有运行,因为我的机没有装com.borland包,我想是因为
服务端用readLien()方法,这个方法是接收以 "\n "为尾的数据,
而客户端只是用getText()方法,没有发送 "\n "字符,所以会死机,
你试试,不知正不正确
[解决办法]
帮忙顶
[解决办法]
学习一下
[解决办法]
同意yqh2009(),建议楼主试试

热点排行