服务器返回消息后,就自动停了!什么原因,求大神帮助
//服务器端
package com.example.demo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSoket {
public static void main(String[] args) {
ServerSoket serverSoket = new ServerSoket();
serverSoket.init();
}
private void init() {
// TODO Auto-generated method stub
ServerSocket server = null;
BufferedReader bf = null;
BufferedWriter bw = null;
try {
server = new ServerSocket(9898);
Socket socket = server.accept();
bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line = null;
if((line = bf.readLine())!=null){
System.out.println(line);
bw.write("respond:"+line +"\n");
bw.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//客户端
package com.example.demo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class ClientScoket {
public static void main(String[] args) {
ClientScoket clientScoket = new ClientScoket();
clientScoket.start();
}
private void start() {
// TODO Auto-generated method stub
Socket client =null;
BufferedReader bf = null;
BufferedWriter bw = null;
BufferedReader bfServer = null;
bf = new BufferedReader(new InputStreamReader(System.in));
try {
client = new Socket(InetAddress.getLocalHost(), 9898);
bfServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String line = null;
String res = null;
while(!(line=bf.readLine()).equalsIgnoreCase(("bye"))){
// 这里非常重要,一定要加上这个\n
bw.write(line +"\n");
bw.flush();
res = bfServer.readLine();
System.out.println(res);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}为什么服务器端返回消息之后就关了,然后客户端再发,就报Connection reset by peer: socket write error异常