实现功能:客户端发送个信息,服务器回个信息
问题1:客户端发送第一次信息,服务器可以正确回复,但第二次的时候程序       就会没有响应,弄了半天也没法解决……郁闷了,求高手解答。
问题2:客户端关闭的时候服务器端也会自动关闭,这个怎么解决?代码:client端:
package client;import java.io.*;
import java.net.*;public class Client {

String message = null; /**
 * @param args
 */
public String getMessage(String name, String password) {
// TODO Auto-generated method stub
try{
Socket connectToServer = new Socket("localhost",727);
DataInputStream isFromServer = new DataInputStream(connectToServer.getInputStream());
DataOutputStream osToServer = new DataOutputStream(connectToServer.getOutputStream());
osToServer.writeUTF(name);
osToServer.flush();
message = isFromServer.readUTF();
}catch(IOException e){System.out.println(e);}
return message;
}server端:
package server;import java.io.*;
import java.net.*;public class Server { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
ServerSocket serverSocket = new ServerSocket(727);
Socket connectToClient = serverSocket.accept();
DataInputStream isFromClient = new DataInputStream(connectToClient.getInputStream());
DataOutputStream osToClient = new DataOutputStream(connectToClient.getOutputStream());
while(true){
String name = isFromClient.readUTF();
if(name.equals("123")){
osToClient.writeUTF("success");
osToClient.flush();
}
else{
osToClient.writeUTF("fail");
osToClient.flush();
}
}
}catch(IOException e){System.out.println(e);}
}

}
还有在client的流输入输出那里加上while(true)的话第一次发送信息就会直接进入没响应状态……问题出在哪里啊?谢谢了!!!!

解决方案 »

  1.   

    import java.net.*;
    import java.io.*;public class Client
    {
    private Socket you;
    public Client()
    {
    try
    {
       you = new Socket("127.0.0.1",9001);
      System.out.println("连接服务器端口:9001.......");
      if(you != null)
      {
       System.out.println("连接成功,等待输入圆半径:");
       new Thread(new SendData(you)).start();
      }
    }catch(Exception e){}

    }

    public static void main(String args[])
    {
    new Client();
    }
    }class SendData implements Runnable
    {
    private String str;
    private double lens;
    private DataInputStream din;
    private DataOutputStream dout;
    private BufferedReader bin;
    private Socket you;
    public SendData(Socket you)
    {
    this.you = you;
    }

    public void run()
    {
    try
    {
      din = new DataInputStream(you.getInputStream());
      dout = new DataOutputStream(you.getOutputStream());
                      bin = new BufferedReader(new InputStreamReader(System.in));
      while(true)
      {   
         try{
                          if((str = bin.readLine()).equals("exit"))
           break;
                           lens = Double.parseDouble(str);
                           }catch(Exception e){System.out.println("请输入数字!");}
       dout.writeUTF("OK");
       dout.writeDouble(lens);
       System.out.println("圆面积:" + din.readDouble());
      }
                      bin.close();
      dout.writeUTF("exit");
      dout.close();
      din.close();                 
      you.close();
      }catch(Exception e){}

    }
    }
      

  2.   

    import java.net.*;
    import java.io.*;
    public class Server
    {
    private ServerSocket server;
    private Socket you; 
    public Server()
    {
    try
      {
       server = new ServerSocket(9001);
                    System.out.println("服务器运行中,监听端口:9001......");
       while(true)
       {
       you = server.accept();
         if(you != null)
                      System.out.println("有客户连接,启动服务线程......");
                      new Thread(new receive(you)).start();
                      // System.out.println("处理完成,继续监听......");
       }
      }catch(Exception e){System.out.println("服务器监听出错!");}
    }

    public static void main(String args[])
    {
    new Server();
    }
    }class receive implements Runnable
    {
    private DataInputStream din;
    private DataOutputStream dout;
    private Socket you;
    private String str;
    private double lens;
    private double area;
    public receive(Socket you)
    {
    this.you = you;
    }
    public void run()
    {
    try
    {
            din = new DataInputStream(you.getInputStream());
            dout = new DataOutputStream(you.getOutputStream());
            while(true)
            {
             if((str = din.readUTF()).equals("exit"))
             break;
             lens = din.readDouble();
             area = 3.14 *(lens*lens);
             dout.writeDouble(area);
            }
            dout.close();
            din.close();
            you.close();
            System.out.println("服务线程关闭,服务器继续监听......");
      }catch(Exception e){}
    }
    }
    基于线程的客户端输入半径,服务器端返回面积 的例子,参考下
      

  3.   

    while(true){
    String name = isFromClient.readUTF();
    if(name.equals("123")){
    osToClient.writeUTF("success");
    osToClient.flush();
    }
    else{
    osToClient.writeUTF("fail");
    osToClient.flush();
    }
    看不懂这个死循环是什么?
    服务器没有循环监听,他只接收一次连接就进入你的死循环了!
      

  4.   

    是啊,没有循环监听,就只能接收第一个客户端的socket,而你客户端也不是循环。
      

  5.   

    你们对 io 流操作的时候怎么不做循环啊?
    boolean =true;
    while(boolean)
    {
       io= new inputstream(client.getinputstream());
       while((string=io.inputstream.readln())!=null)
       {
            if string.equals("...")
            do .........
            else
            {
                boolean=false;
               break;        
             } 
        }
        io.close();
        sleep(2000);
    }