Java 网络编程从入门到精通(30):自定义接受方法
最编程
2024-03-20 08:51:16
...
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();
}
}