weixin_42509940
2014-03-28 14:45
浏览 1.2k
已采纳

socket双向通信,为什么服务器只能接收一次客户端发送的消息

如题,双向的socket通信,但是服务端只能接收一次客户端发送的请求消息就终止在accept状态,请问是哪里出了问题了。

客户端程序如下:

[code="java"]package com.snail.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {
public static void main(String[] args) {
Socket socket = null;
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
try {

        socket = new Socket(InetAddress.getLocalHost(), 2123);

        bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        printWriter = new PrintWriter(socket.getOutputStream(), true);


        int i=1;
        while (true) {
            System.out.println("input " + i);
            BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));
            String result = bufIn.readLine();
            System.out.println("client input data " + i++ + ": " + result);
            printWriter.println(result);
            printWriter.flush();

            String serverStr = bufferedReader.readLine();
            System.out.println("Server : " + serverStr);// 在标准输出上打印从Server读入的字符串
            if (result.equals("end")) {
                bufIn.close();
                break;
            }
        }
        printWriter.close();
        bufferedReader.close();
        socket.close();

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(printWriter != null) {
            printWriter.close();
        }
        if(bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

}[/code]

服务端代码如下:

[code="java"]package com.snail.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
BufferedReader bufferedReader = null;
PrintWriter printWriter = null;

    try {
        serverSocket = new ServerSocket(2123);
        int i = 1;
        while (true) {

            System.out.println("accept " + i);
            socket = serverSocket.accept();
            System.out.println("begin " + i++ + ": " + "accept");
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            printWriter = new PrintWriter(socket.getOutputStream(), true);

            // 获取从客户端读入的字符串
            String result = bufferedReader.readLine();

            System.out.println("Client say : " + result);

            if ("bye".equals(result)) {
                System.out.println("Client say : " + result);
                printWriter = new PrintWriter(socket.getOutputStream());
                printWriter.print("hello Client, I am Server! bye-bye.");
                printWriter.flush();
                break;
            }

            // 由Socket对象得到输出流,并构造PrintWriter对象

            printWriter.println("hello Client, I am Server!");
            printWriter.flush();

        }
        printWriter.close();
        bufferedReader.close();
        socket.close();
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (printWriter != null) {
            printWriter.close();
        }
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

}
[/code]

请大神指教下

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 温故而知新666 2014-03-28 14:51
    已采纳

    // 获取从客户端读入的字符串

    String result = bufferedReader.readLine(); 这里会阻塞。
    你服务器端获取Socket以后 需要另外启动一个线程去处理,你现在是单线程的服务器端设计,当然只能接收一次请求了。

    服务器端接收到Socket以后应该启动一个线程

    new Thread(new Runable(){
    //你上面的哪些代码。
    }).start();

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • hayability 2014-03-28 15:33

    使用NIO吧,推荐mina或者netty

    评论
    解决 无用
    打赏 举报
  • fxhu09 2014-03-28 16:38

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    import java.io.PrintWriter;

    import java.net.ServerSocket;

    import java.net.Socket;

    public class SocketServer {

    public static void main(String[] args) {

    ServerSocket serverSocket = null;

    Socket socket = null;

    BufferedReader bufferedReader = null;

    PrintWriter printWriter = null;

        try {  
           serverSocket = new ServerSocket(2123); 
           socket = serverSocket.accept(); 
           bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
           printWriter = new PrintWriter(socket.getOutputStream(), true);  
            int i = 1;  
            while (true) {  
    
                System.out.println("accept " + i);  
    
                System.out.println("begin " + i++ + ": " + "accept");  
    
    
               // 获取从客户端读入的字符串  
                String result = bufferedReader.readLine();  
    
                System.out.println("Client say : " + result);  
    
                if ("bye".equals(result)) {  
                   System.out.println("Client say : " + result);  
                   printWriter = new PrintWriter(socket.getOutputStream());  
                    printWriter.print("hello Client, I am Server! bye-bye.");  
                    printWriter.flush();  
                    break;  
                }  
    
                // 由Socket对象得到输出流,并构造PrintWriter对象  
    
                printWriter.println("hello Client, I am Server!");  
                printWriter.flush();  
                //printWriter.close();  
                //bufferedReader.close();  
             }  
            printWriter.close();  
            bufferedReader.close();  
            socket.close();  
             serverSocket.close();  
         } catch (IOException e) {  
             e.printStackTrace();  
         } finally {  
             if (socket != null) {  
                 try {  
                     socket.close();  
                 } catch (IOException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                 }  
             }  
             if (serverSocket != null) {  
                 try {  
                     serverSocket.close();  
                 } catch (IOException e) {  
                     e.printStackTrace();  
                 }  
             }  
             if (printWriter != null) {  
                 printWriter.close();  
             }  
             if (bufferedReader != null) {  
                 try {  
                     bufferedReader.close();  
                 } catch (IOException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                 }  
             }  
         }  
     }  
    

    }

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    import java.io.PrintWriter;

    import java.net.InetAddress;

    import java.net.Socket;

    import java.net.UnknownHostException;

    public class SocketClient {

    public static void main(String[] args) {

    Socket socket = null;

    PrintWriter printWriter = null;

    BufferedReader bufferedReader = null;

    try {

            socket = new Socket(InetAddress.getLocalHost(), 2123);  
    
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
            printWriter = new PrintWriter(socket.getOutputStream(), true);  
    
    
            int i=1; 
            BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));  
            while (true) {  
                System.out.println("input " + i);  
    
                String result = bufIn.readLine();  
                System.out.println("client input data " + i++ + ": " + result);  
                printWriter.println(result);  
                printWriter.flush();  
    
                String serverStr = bufferedReader.readLine();  
                System.out.println("Server : " + serverStr);// 在标准输出上打印从Server读入的字符串  
                if (result.equals("bye")) {  
                    bufIn.close();  
                    break;  
                }  
    
            }  
            printWriter.close();  
            bufferedReader.close();  
            socket.close();  
    
        } catch (UnknownHostException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if(socket != null) {  
                try {  
                    socket.close();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
            if(printWriter != null) {  
                printWriter.close();  
            }  
            if(bufferedReader != null) {  
                try {  
                    bufferedReader.close();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
    

    }

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题