<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 11">
<meta name="Originator" content="Microsoft Word 11">
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">
<!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Times New Roman";}
/* Page Definitions */
@page
{}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
</style>
<![endif]-->
本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:Java网络编程从入门到精通(29):服务端Socket的选项
使用ServerSocket类的implAccept方法可以使用accept方法返回一个Socket子类对象。但implAccept是protected方法,因此,必须在ServerSocket类的子类中覆盖accept方法,然后在accept方法中使用implAccept方法重新设置Socket对象。implAccept方法的定义如下:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->protectedfinalvoidimplAccept(Sockets)throwsIOException
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Times New Roman";}
/* Page Definitions */
@page
{}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
</style>
<![endif]-->
只要通过implAccept方法设置一个未连接的Socket子类对象,accept方法就会返回一个已经连接的Socket子类对象(accept返回的是Socket对象,要想得到Socket子类对象,必须进行类型转换)。在大多数时候不需要改变accept方法的行为,但有时需要一个有更多特性的Socket类。通过implAccept方法就可以达到这个目的。下面的代码定义了一个从Socket继承的HttpSocket类,这个类除了具有Socket类的一切特性外,还增加了一个方法getRequestHeaders,用于返回HTTP请求的头信息。
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->package server;
importjava.net.*;
importjava.io.*;
classHttpSocketextendsSocket
{
publicStringgetRequestHeaders()throwsException
{
InputStreamReaderisr=newInputStreamReader(getInputStream());
BufferedReaderbr=newBufferedReader(isr);
Strings="",result="";
while(!(s=br.readLine()).equals(""))
result=result+s+"\r\n";
returnresult;
}
}
classHttpServerSocketextendsServerSocket
{
publicHttpServerSocket(intport)throwsIOException
{
super(port);
}
publicSocketaccept()throwsIOException//覆盖accept方法
{
Sockets=newHttpSocket();
implAccept(s);//将accept方法返回的对象类型设为HttpSocket
returns;
}
}
publicclassCustomAccept
{
publicstaticvoidmain(String[]args)throwsException
{
HttpServerSockethttpServerSocket=newHttpServerSocket(1234);
HttpSockethttpSocket=(HttpSocket)httpServerSocket.accept();
System.out.println(httpSocket.getRequestHeaders());//向控制台输出HTTP请求头
httpServerSocket.close();
}
}
测试
执行如下命令:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->java server.CustomAccept
在IE的地址栏中输入如下Url:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->http://localhost:1234
CustomAccept在控制台中的运行结果:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->GET/HTTP/1.1
Accept:*/*
Accept-Language:zh-cn
UA-CPU:x86
Accept-Encoding:gzip,deflate
User-Agent:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.2;SV1;.NETCLR1.1.4322;.NETCLR2.0.50727;InfoPath.1;InfoPath.2)
Host:localhost:1234
Connection:Keep-Alive
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 11">
<meta name="Originator" content="Microsoft Word 11">
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">
<!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Times New Roman";}
/* Page Definitions */
@page
{}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
</style>
<![endif]-->
上面的运行结果就是IE向服务端发出的HTTP请求头的内容。这个运行结果会根据客户机配置的不同而有所差异。
下一篇:Java网络编程从入门到精通(31):非阻塞I/O简介
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!《银河系列原创教程》发布《Java Web开发速学宝典》出版,欢迎定购
分享到:
相关推荐
Java 网络编程 说明: 网络编程,分为两种一种是TCP、另一种是UDP方式。我们分别写一下TCP、UDP端的Server和Client简单演示代码。 TCP: Sever import java.io.DataInputStream; import java.io.IOException; ...
基于Socket的java网络编程, Socket通讯的过程 Server端Listen(监听)某个端口是否有连接请求,Client端向Server 端发出Connect(连接)请求,Server端向Client端发回Accept(接受)消息。一个连接就建立起来了。Server...
1、Linux网络编程01——网络协议入门 2、Linux网络编程02——无连接和面向连接的区别 3、Linux网络编程03——字节序和地址转换 4、Linux网络编程04——套接字 5、Linux网络编程05——C/S与B/S架构的区别 6、Linux...
对于服务器编程中最重要的一步等待并接受客户的连接,那么这一步在编程中如何完成,accept函数就是完成这一步的。它从内核中取出已经建立的客户连接,然后把这个已经建立的连接返回给用户程序,此时用户程序就可以与...
import java.io.*; import java.net.*; import java.util.*; import java.lang.*; public class Server extends ServerSocket { private static ArrayList User_List = new ArrayList(); private static ...
Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇 Linux网络编程之TCP/IP...
1.Linux网络编程-客户端与服务器端异步通信 2.fcntl函数详解 3.请看我的代码!用非阻塞的accept函数要注意什么
java实现socket网络编程 服务端使用ServerSocket绑定IP和端口,使用Accept监听端口是否有客户端发送连接请求,一旦有客户端发送连接请求, 服务端就回送连接信息,正式建立连接。Server端和Client端都可以通过Send,...
Accept方法用于产生"阻塞",直到接受到一个连接,并且返回一个客户端的Socket对象实例。"阻塞"是一个术语,它使程序运行暂时"停留"在这个地方,直到一个会话产生,然后程序继续;通常"阻塞"是由循环产生的。 . ...
Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇 Linux网络编程之TCP/IP...
Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇 Linux网络编程之TCP/IP...
【Python编程题】实战练习题目,falcon例程,API轻量网络服务编程 输入说明:使用falcon生成轻量级网络服务程序 输出说明:能正常访问网络服务 用例:用例curl localhost:8000 -v 结果: * Rebuilt URL to: ...
套接字 : 网络编程方式 流式套接字:tcp 数据报套接字: udp tcp服务端流程 socket() ---> bind() ---> listen() ---> accept()---> recv/send ---> close() 客户端 socket() ---> connect() ---> send/recv --->...
精通Windows Sockets网络开发——基于Visual C++实现 目 录 第1篇网络开发基础篇 第1章准备开发环境 1.1windows sockets开发概述 1.1.1网络程序开发应用 1.1.2网络程序结构——c/s、b/s 1.1.3网络程序通信基础——...
该文档是介绍访问应用很慢,访问Nginx时,报错:accept() failed (24 Too many open files)的解决方案,与大家共享!
Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇 Linux网络编程之TCP/IP...
目的是让初学者不仅对Socket异步非阻塞的概念有个非常透彻的理解,而且也给他们提供一个用Socket开发网络通信应用程序的快速入门方法。操作系统是Windows 98(或NT4.0),开发工具是Visual C++6.0。 MFC提供了一个...
Linux网络编程视频视频教程, 01-复习-Linux网络编程mp40 2-信号量生产者复习mp40 3-协议.mp4 4-7层模型和4层模型及代表协议mp4 AV 5-网络传输数据封装流程avi A 06-以太网帧和ARP请求avi AV 07-IP协议.avi 08-端口...
for the JAVA SE DEVELOPMENT KIT (JDK), VERSION 6 SUN MICROSYSTEMS, INC. ("SUN") IS WILLING TO LICENSE THE SOFTWARE IDENTIFIED BELOW TO YOU ONLY UPON THE CONDITION THAT YOU ACCEPT ALL OF THE TERMS ...
一个Socket实例代表了TCP连接的一个客户端,而一个ServerSocket实例代表了TCP连接的一个服务器端,一般在TCP Socket编程中,客户端有多个,而服务器端只有一个,客户端TCP向服务器端TCP发送连接请求,服务器端的...