socket通信只有第一次发送的数据能被服务器接收
在用servlet建立serversocket与客户端的socket通信的过程中,客户端发送的数据只有第一次能被服务器接收到并打印,第二次及以后就会提示java.net....
在用servlet建立serversocket与客户端的socket通信的过程中,客户端发送的数据只有第一次能被服务器接收到并打印,第二次及以后就会提示java.net.SocketException: Software caused connection abort: recv failed的异常信息,请问大神是哪里出了问题?在线等,服务器端代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
class SocketServer extends Thread{
private Socket socket;
public SocketServer(Socket socket){
this.socket = socket;
}
public void run(){
try{
BufferedReader bfr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream());
String line = bfr.readLine();
System.out.println(line);
System.out.println("run here!");
pw.println(line);
pw.flush();
bfr.close();
pw.close();
socket.close();
}
catch (IOException ioex){
ioex.printStackTrace();
}
}
}
public class Listener extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
//private String message;
public void init() throws ServletException{
new Thread(){
public void run(){
try{
ServerSocket server = new ServerSocket(5203);
System.out.print("服务器启动成功1");
Socket socket = null;
while(true){
socket = server.accept();
SocketServer ss = new SocketServer(socket);
ss.start();
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}.start();
}
}
客户端代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketClient {
public static void main(String []args)throws IOException{
try{
Socket socket = new Socket("localhost",5203);
System.out.println("client start successful");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//系统输入
PrintWriter pr = new PrintWriter(socket.getOutputStream());//输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//输入流
String readline;
readline = br.readLine();
while(!readline.equals("end")){
pr.println(readline);
pr.flush();
System.out.println("Client:" + readline);
System.out.println("Server:" + in.readLine());
readline = br.readLine();
}
pr.close();
in.close();
socket.close();
}catch(Exception e){
System.out.println("can not listen to:" + e);
}
}
} 展开
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
class SocketServer extends Thread{
private Socket socket;
public SocketServer(Socket socket){
this.socket = socket;
}
public void run(){
try{
BufferedReader bfr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream());
String line = bfr.readLine();
System.out.println(line);
System.out.println("run here!");
pw.println(line);
pw.flush();
bfr.close();
pw.close();
socket.close();
}
catch (IOException ioex){
ioex.printStackTrace();
}
}
}
public class Listener extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
//private String message;
public void init() throws ServletException{
new Thread(){
public void run(){
try{
ServerSocket server = new ServerSocket(5203);
System.out.print("服务器启动成功1");
Socket socket = null;
while(true){
socket = server.accept();
SocketServer ss = new SocketServer(socket);
ss.start();
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}.start();
}
}
客户端代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketClient {
public static void main(String []args)throws IOException{
try{
Socket socket = new Socket("localhost",5203);
System.out.println("client start successful");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//系统输入
PrintWriter pr = new PrintWriter(socket.getOutputStream());//输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//输入流
String readline;
readline = br.readLine();
while(!readline.equals("end")){
pr.println(readline);
pr.flush();
System.out.println("Client:" + readline);
System.out.println("Server:" + in.readLine());
readline = br.readLine();
}
pr.close();
in.close();
socket.close();
}catch(Exception e){
System.out.println("can not listen to:" + e);
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询