日期:2014-05-20  浏览次数:20937 次

在线等。。Applet在IE中无法监听http端口,急啊~
请高人们帮我看看,指点迷津,我写的一个服务端的Applet监听了一个http端口http://127.0.0.1:8085,用AppletView运行一切正常,能够收到客户端发来的http请求。但是把Applet嵌入到html后,再用浏览器打开,看日志端口已经在监听了,但是客户端发请求过去,客户端会报Error: java.net.SocketException: Software caused connection abort: recv failed这个错误。服务端没有任何反应没收到请求。

我的客户端和服务端都是在本地运行的。

服务端代码:
import java.applet.Applet;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

class httpServerDaemon
{
void startHttpServer()
{
try
{
InetSocketAddress addr = new InetSocketAddress ( 8085 );
HttpServer server = HttpServer.create ( addr, 0 );

server.createContext ( "/", new httpHandler () );
server.setExecutor ( Executors.newCachedThreadPool () );
server.start ();

System.out.println ( "Succ:\t Http Processor is listening on 8085" );
}
catch ( Exception ex )
{
System.out.println ( ex.toString () );
}
}
}

class httpHandler implements HttpHandler
{
public String getParam(String params, String key)
{
String tokens[] = params.split ( "&" );

for ( int i = 0; i < tokens.length; ++i )
{
String pair[] = tokens[i].split ( "=" );

if ( pair.length == 2 && pair[0].equals ( key ) )
return pair[1];
}

return "";
}

public void handle(HttpExchange exchange)
{
try
{
System.out.println ( "handle....." );
String requestMethod = exchange.getRequestMethod ();
String response = String.format ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><XMLRequestVarBean><myState>Ready</myState><serverName>MonitorServer</serverName><serverVersion>v1.0.0.0</serverVersion></XMLRequestVarBean>" );

if ( requestMethod.equalsIgnoreCase ( "POST" ) )
{
String line = "";
String request = "";
BufferedReader br = new BufferedReader ( new InputStreamReader ( exchange.getRequestBody (), "UTF-8" ) );

while ( ( line = br.readLine () ) != null )
{
request += line;
}

ClientRequest req = new ClientRequest ();

if ( !req.ParseRequest ( request ) )
{
response = "EventError|";
response += "Unknown Request";
}
else if ( req.m_RequestID.equals ( "RequestNotReady" ) )
{
response = "EventNotReady|" + req.m_AgentID + "|ok";
}
else if ( req.m_RequestID.equals ( "RequestAllowNotReady" ) )
{
response = "EventAllowNotReady|" + req.m_AgentID + "|ok";
}
else if ( req.m_RequestID.equals ( "RequestDenyNotReady" ) )
{
response = "EventDenyNotReady|" + req.m_AgentID + "|ok";
}

System.out.println ( "request = " + request );
System.out.println ( "response = " + response );
}

byte body[] = response.getBytes ( "UTF-8" );
Headers responseHeaders = exchange.getResponseHeaders ();
responseHeaders.set ( "Content-Type", "text/html" );
exchange.sendResponseHeaders ( 200, body.length );

OutputStream responseBody = exchange.getResponseBody ();
responseBody.write ( body );