当前位置: 技术问答>java相关
这个违例是因何而产生?如何消除?
来源: 互联网 发布时间:2015-03-09
本文导语: 在Applet和Servlet以http的post的方式通讯中,servlet在每次通讯(从Applet获取数据然后返回数据)后都会掷出如下的违例。虽然不影响正常的通讯,但很想知道这个违例的产生原因,和消除方法。 HANDLER THREAD PROBLEM:java.io....
在Applet和Servlet以http的post的方式通讯中,servlet在每次通讯(从Applet获取数据然后返回数据)后都会掷出如下的违例。虽然不影响正常的通讯,但很想知道这个违例的产生原因,和消除方法。
HANDLER THREAD PROBLEM:java.io.IOException:Socket Closed
java.io.IOException:Socket Closed
at java.net.PlainSocketImpl.getInputStream(PlainSocketImpl.java:432)
at java.net.socket$1.run(Socket.java:335)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.Socket.getInputStream(Socket.java:332)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:161)
HANDLER THREAD PROBLEM:java.io.IOException:Socket Closed
java.io.IOException:Socket Closed
at java.net.PlainSocketImpl.getInputStream(PlainSocketImpl.java:432)
at java.net.socket$1.run(Socket.java:335)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.Socket.getInputStream(Socket.java:332)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:161)
|
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PostServer extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse resp)
{
try
{
//接收从客户端发来的数据
DataInputStream in=new DataInputStream(req.getInputStream());
strChoice=in.readUTF().trim();
System.out.println("strChoice="+strChoice);
//向客户端发送数据
resp.setContentType("application/octet-stream");
byteout=new ByteArrayOutputStream();
out=new DataOutputStream(byteout);
if(strChoice.equals("第一步"))
{
out.writeUTF("第一步回复1!");
out.writeUTF("第一步回复2!");
}
else if(strChoice.equals("第二步"))
{
out.writeUTF("第二步回复1!");
out.writeUTF("第二步回复2!");
out.writeUTF("第二步回复3!");
out.writeUTF("第二步回复4!");
}
out.flush();
buf=byteout.toByteArray();
resp.setContentLength(buf.length);
servletout=resp.getOutputStream();
servletout.write(buf);
}
catch(IOException ex)
{
System.out.println("out3 ERROR:"+ex);
}
}
public void destroy(){
try{
in.close();
servletout.close();
byteout.close();
out.close();
}catch(Exception e){System.out.println(e);}
private String strChoice;
private byte[] buf;
private ServletOutputStream servletout;
private ByteArrayOutputStream byteout;
private DataOutputStream out;
}
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PostServer extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse resp)
{
try
{
//接收从客户端发来的数据
DataInputStream in=new DataInputStream(req.getInputStream());
strChoice=in.readUTF().trim();
System.out.println("strChoice="+strChoice);
//向客户端发送数据
resp.setContentType("application/octet-stream");
byteout=new ByteArrayOutputStream();
out=new DataOutputStream(byteout);
if(strChoice.equals("第一步"))
{
out.writeUTF("第一步回复1!");
out.writeUTF("第一步回复2!");
}
else if(strChoice.equals("第二步"))
{
out.writeUTF("第二步回复1!");
out.writeUTF("第二步回复2!");
out.writeUTF("第二步回复3!");
out.writeUTF("第二步回复4!");
}
out.flush();
buf=byteout.toByteArray();
resp.setContentLength(buf.length);
servletout=resp.getOutputStream();
servletout.write(buf);
}
catch(IOException ex)
{
System.out.println("out3 ERROR:"+ex);
}
}
public void destroy(){
try{
in.close();
servletout.close();
byteout.close();
out.close();
}catch(Exception e){System.out.println(e);}
private String strChoice;
private byte[] buf;
private ServletOutputStream servletout;
private ByteArrayOutputStream byteout;
private DataOutputStream out;
}
|
你可以看看你的程序是不是在关闭了socket链接后,又进行了socket通讯。
|
注意:
关闭Socket的位置是否恰当、是否flush?
关闭Socket的位置是否恰当、是否flush?
|
有没有高手出现呀,什么没有,那我就要出手了
|
byteout.close();
out.close();
变一下顺序,out.close()应该在前面
out.close();
变一下顺序,out.close()应该在前面
|
try to close "in" after you call servletout.write(buf).