`
seara
  • 浏览: 624202 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java网络编程从入门到精通(26):在服务端接收和发送数据

阅读更多
<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 */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;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网络编程从入门到精通(25):创建ServerSocket对象

在建立完ServerSocket对象后,通过accept方法返回的Socket对象,服务端就可以和客户端进行数据交互。

Socket类和ServerSocket类都有两个得到输入输出流的方法:getInputStreamgetOutputStream。对于Socket类而言,使用getInputStream方法得到的InputStream是从服务端获取数据,而getOutputStream方法得到的OutputStream是向服务端发送数据。而ServerSocketgetInputStreamgetOutputStream方法也类似。InputStream从客户端读取数据,OutputStream向客户端发送数据。下面的代码是一个接收HTTP请求,并返回HTTP请求头信息的程序,它演示了ServerSocket类如何读取和发送来自客户端的数据。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package server;

importjava.net.*;
importjava.io.*;

publicclassHttpEchoServerextendsThread
{
privateSocketsocket;
publicvoidrun()
{
try
{
InputStreamReaderisr
=newInputStreamReader(socket
.getInputStream());
BufferedReaderbr
=newBufferedReader(isr);
OutputStreamWriterosw
=newOutputStreamWriter(socket
.getOutputStream());
osw.write(
"HTTP/1.1200OK\r\n\r\n");
Strings
="";
while(!(s=br.readLine()).equals(""))
osw.write(
"<html><body>"+s+"<br></body></html>");
osw.flush();
socket.close();
}
catch(Exceptione)
{
}
}
publicHttpEchoServer(Socketsocket)
{
this.socket=socket;
}
publicstaticvoidmain(String[]args)throwsException
{
ServerSocketserverSocket
=newServerSocket(8888);
System.out.println(
"服务器已经启动,端口:8888");
while(true)
{
Socketsocket
=serverSocket.accept();
newHttpEchoServer(socket).start();
}
}
}

<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 */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;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]-->

编译并运行HttpEchoServer后,在IE的地址栏中输入URLhttp://localhost:8888。输出结果如图1所示。


图1
<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 */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;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]-->

上面的代码并未验证HTTP请求类型,因此,GETPOSTHEADHTTP请求都可以得到回应。在接收客户端请求后,只向客户端输出了一行HTTP响应头信息(包括响应码和HTTP版本号),对于HTTP响应头来说,这一行是必须有的,其他的头字段都是可选的。上面的代码每读一行请求头信息,就向客户端写一行响应信息。最后使用了flush方法将输出缓冲区中的内容发送的客户端。这是必须的,只要使用OutputStream,在最后就必须要调用flush方法(Socket类中使用OutputStream也是一样)

下一篇:
Java网络编程从入门到精通(27):关闭服务端连接



国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》发布

《Java Web开发速学宝典》出版,欢迎定购

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics