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个回答

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

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

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

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

fxhu09
fxhu09 搞笑。这个居然成了答案。
6 年多之前 回复

使用NIO吧,推荐mina或者netty

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();  
            }  
        }  
    }  
}  

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问