使用ServerSocket
类的implAccept
方法可以使用accept
方法返回一个Socket
子类对象。但implAccept
是protected
方法,因此,必须在ServerSocket
类的子类中覆盖accept
方法,然后在accept
方法中使用implAccept
方法重新设置Socket
对象。implAccept
方法的定义如下:
protected
final
void
implAccept(Socket s)
throws
IOException
只要通过implAccept
方法设置一个未连接的Socket
子类对象,accept
方法就会返回一个已经连接的Socket
子类对象(accept
返回的是Socket
对象,要想得到Socket
子类对象,必须进行类型转换)。在大多数时候不需要改变accept
方法的行为,但有时需要一个有更多特性的Socket
类。通过implAccept
方法就可以达到这个目的。
下面的代码定义了一个从Socket
继承的HttpSocket
类,这个类除了具有Socket
类的一切特性外,还增加了一个方法getRequestHeaders
,用于返回HTTP
请求的头信息。
package
server;
import java.net. * ;
import java.io. * ;
class HttpSocket extends Socket
{
public String getRequestHeaders() throws Exception
{
InputStreamReader isr = new InputStreamReader(getInputStream());
BufferedReader br = new BufferedReader(isr);
String s = "" , result = "" ;
while ( ! (s = br.readLine()).equals( "" ))
result = result + s + " \r\n " ;
return result;
}
}
class HttpServerSocket extends ServerSocket
{
public HttpServerSocket( int port) throws IOException
{
super (port);
}
public Socket accept() throws IOException // 覆盖accept方法
{
Socket s = new HttpSocket();
implAccept(s); // 将accept方法返回的对象类型设为HttpSocket
return s;
}
}
public class CustomAccept
{
public static void main(String[] args) throws Exception
{
HttpServerSocket httpServerSocket = new HttpServerSocket( 1234 );
HttpSocket httpSocket = (HttpSocket) httpServerSocket.accept();
System.out.println(httpSocket.getRequestHeaders()); // 向控制台输出HTTP请求头
httpServerSocket.close();
}
}
import java.net. * ;
import java.io. * ;
class HttpSocket extends Socket
{
public String getRequestHeaders() throws Exception
{
InputStreamReader isr = new InputStreamReader(getInputStream());
BufferedReader br = new BufferedReader(isr);
String s = "" , result = "" ;
while ( ! (s = br.readLine()).equals( "" ))
result = result + s + " \r\n " ;
return result;
}
}
class HttpServerSocket extends ServerSocket
{
public HttpServerSocket( int port) throws IOException
{
super (port);
}
public Socket accept() throws IOException // 覆盖accept方法
{
Socket s = new HttpSocket();
implAccept(s); // 将accept方法返回的对象类型设为HttpSocket
return s;
}
}
public class CustomAccept
{
public static void main(String[] args) throws Exception
{
HttpServerSocket httpServerSocket = new HttpServerSocket( 1234 );
HttpSocket httpSocket = (HttpSocket) httpServerSocket.accept();
System.out.println(httpSocket.getRequestHeaders()); // 向控制台输出HTTP请求头
httpServerSocket.close();
}
}
测试
执行如下命令:
执行如下命令:
java server.CustomAccept
在IE的地址栏中输入如下Url:
http:
//
localhost:1234
CustomAccept在控制台中的运行结果:
GET / HTTP/
1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip , deflate
User-Agent: Mozilla/ 4.0 (compatible ; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; InfoPath.2)
Host: localhost: 1234
Connection: Keep-Alive
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip , deflate
User-Agent: Mozilla/ 4.0 (compatible ; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; InfoPath.2)
Host: localhost: 1234
Connection: Keep-Alive
本文转自 androidguy 51CTO博客,原文链接:
http://blog.51cto.com/androidguy/214344
,如需转载请自行联系原作者