- 浏览: 626203 次
- 性别:
- 来自: 广州
文章分类
最新评论
本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:Java网络编程从入门到精通(16):客户端套接字(Socket)的超时
在Java类中,getter和setter方法占了很大的比重。由于Java中没有定义属性的关键字;因此,getter和setter方法用于获得和设置Java类的属性值;如getName和setName方法用于设置name属性的值。如果某个属性只有getter方法,那这个属性是只读的;如果只有setter方法,那么这个属性是只写的。在Socket类中也有很多这样的属性来获得和Socket相关的信息,以及对Socket对象的状态进行设置。
一、用于获得信息的getter方法
我们可以从Socket对象中获得3种信息。
1. 服务器信息
对于客户端来说,服务器的信息只有两个:IP和端口。Socket类为我们提供了3个方法来得到这两个信息。
(1) public InetAddress getInetAddress()
这个方法返回一个InetAddress对象。通过这个对象,可以得到服务器的IP、域名等信息。
System.out.println(socket.getInetAddress().getHostAddress());
System.out.println(socket.getInetAddress().getHostName());
<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"><!--[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";}
</style>
<![endif]-->
(2) public int getPort()
这个方法可以以整数形式获得服务器的端口号。
System.out.println(socket.getInetAddress().getPort());
<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";} </style> <![endif]-->
(3) public SocketAddress getRemoteSocketAddress()
这个方法是将getInetAddress和getPort方法结合在了一起;利用这个方法可以同时得到服务器的IP和端口号。但这个方法返回了一个SocketAddress对象,这个对象只能作为connect方法的参数用于连接服务器;而要想获得服务器的IP和端口号,必须得将SocketAddress转换为它的子类InetSocketAddress。
System.out.println(((InetSocketAddress)socket.getRemoteSocketAddress()).getHostName());
System.out.println(((InetSocketAddress)socket.getRemoteSocketAddress()).getPort());
注意:以上3个方法都可以在调用Socket对象关闭后调用。它们所获得的信息在Socket对象关闭后仍然有效。如果直接使用IP连接服务器时,getHostName和getHostAddress的返回值是一样的;都是服务器的IP。
<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%5C05%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;}
/* List Definitions */
@list l0
{}
@list l0:level1
{
margin-left:39.0pt;
text-indent:-18.0pt;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";}
</style>
<![endif]-->
2. 本机信息
与服务器信息一样,本机信息也有两个:本地IP和绑定的本地端口号。这些信息也可以通过3个方法来获得。
(1) public InetAddress getLocalAddress()
这个方法返回了本机的InetAddress对象。通过这个方法可以得到本机的IP和机器名。当本机绑定了多个IP时,Socket对象使用哪一个IP连接服务器,就返回哪个IP。如果本机使用ADSL上网,并且通过Socket对象连接到Internet上的某一个IP或域名上(如www.ptpress.com.cn),则getLocalAddress将返回“ADSL连接”所临时绑定的IP;因此,我们可以通过getLocalAddress得到ADSL的临时IP。
socket.connect(newInetSocketAddress("www.ptpress.com.cn",80));
System.out.println(socket.getLocalAddress().getHostAddress());
System.out.println(socket.getLocalAddress().getHostName());
<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%5C07%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> <!--{cps..23}-</style>(2) public int getLocalPort()
通过这个方法可以得到Socket对象所绑定的本机的一个端口号;如果未绑定端口号,则返回一个从1024到65,535之间的随机数。因此,使用这个方法可能每次得到的端口号不一样。
//如果使用下面的bind方法进行端口绑定的话,getLocalPort方法将返回100
//socket.bind(newInetSocketAddress("127.0.0.1",100));
socket.connect(newInetSocketAddress("www.ptpress.com.cn"80));
System.out.println(socket.getLocalPort())
(3) public SocketAddress getLocalSocketAddress()
这个方法和getRemoteSocketAddress方法类似,也是同时得到了本地IP和Socket对象所绑定的端口号。如果要得到本地IP和端口号,必须将这个方法的返回值转换为InetSocketAddress对象。
System.out.println(((InetSocketAddress)socket.getLocalSocketAddress()).getHostName());
System.out.println(((InetSocketAddress)socket.getLocalSocketAddress()).getPort());
<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"><!--[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;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:39.0pt; text-indent:-18.0pt;} @list l1 {} @list l1:level1 {mso-level-text:""(%1")"; margin-left:42.0pt; text-indent:-21.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->3. 用于传输数据的输入、输出流
输入、输出流在前面的章节已经被多次用到。在这里让我们来简单回顾一下。
(1) public InputStream getInputStream() throws IOException
用于获得从服务器读取数据的输入流。它所得以的流是最原始的源。为了操作更方便,我们经常使用InputStreamReader和BufferedReader来读取从服务器传过来的字符串数据。<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
InputStreaminputStream=socket.getInputStream();
InputStreamReaderinputStreamReader=newInputStreamReader(inputStream);
BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);
System.out.println(bufferedReader.readLine());
<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%5C16%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;} /* List Definitions */ @list l0 {} @list l0:level1 {mso-level-text:""(%1")"; margin-left:42.0pt; text-indent:-21.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
(2) public OutputStream getOutputStream() throws IOException
用于获得向服务器发送数据的输出流。输出流可以通过OutputStreamWriter和BufferedWriter向服务器写入字符串数据。
OutputStreamoutputStream=socket.getOutputStream();
OutputStreamWriteroutputStreamWriter=newOutputStreamWriter(outputStream);
BufferedWriterbufferedWriter=newBufferedWriter(outputStreamWriter);
bufferedWriter.write("你好");
bufferedWriter.flush();
注意:在使用OutputStream的write方法输出数据后,必须使用flush方法刷新输出缓冲区,以便将输出缓冲区中的数据发送出去。如果要输出字符串,使用OutputStreamWriter和BufferedWriter都可以;它们的write方法都可以直接使用字符串作为参数来输出数据。而这一点与相应的InputStreamReader和BufferedReader不同;它们中只有BufferedReader有readLine方法,因此,必须使用BufferedReader才能直接读取字符串数据。
下一篇:Java网络编程从入门到精通(18):Socket类的getter和setter方法(2)
<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%5C20%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;}
/* List Definitions */
@list l0
{}
@list l0:level1
{
margin-left:18.0pt;
text-indent:-18.0pt;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.0pt;
font-family:"Times New Roman";}
</style>
<![endif]-->
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购
相关推荐
java 的eclipse或idea等 定义变量时 直接引入lombok.jar包 在类外部 引入@AllArgsConstructor @NoArgsConstructor @Data 简便方法引入getter/setter/tostring等方法
java普通类编译成json但只是当前类的有getter、setter方法的版本
eclipse 自动为getter和setter添加中文注释 详细参见 http://blog.csdn.net/kongguoan/article/details/38293137
自动生成带注释的getter和setter方法(注释)插件
科德尔 具有严格的 getter 和 setter 的 MongoDB 的承诺模型类。 有关使用情况,请检查test文件夹。去做文档添加“类型”检查支持原子更新DynamoDB 适配器执照该程序是免费软件:您可以根据自由软件基金会发布的 GNU...
主要介绍了Vue getter setter,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
c++的成员变量的get和set生成器,简单易用。
java代码-【3】定义一个学生类,使用LinkedList对学生类进行管理,执行...(2)方法为:构造方法,getter和setter方法,toString方法 2.定义三个Student类的对象,添加到LinkedList中 3.显示LinkedList中元素的内容
已经拥有一年经验的你真的了解get set方法吗?也许不!以前我以为都有上架项目,这都是太基础的东西,但今天我重新回头看了以前做的笔记,真的是书读百遍,其义自见。
最近因为公司的新项目决定使用Vue.js来做,但在使用的过程中发现了一个有趣的事情,因为发现的这个事情展开了一些对于getter和setter的思考,具体是什么下面通过这篇文章来一起看看吧,有需要的朋友们可以参考学习。
显然这是一个无关IE(高级IE除外)的话题,尽管如此,有兴趣的同学还是一起来认识一下ECMAScript5标准中getter和setter的实现。在一个对象中,操作其中的属性或方法,通常运用最多的就是读(引用)和写了,譬如说o....
主要介绍了IntelliJ IDEA快速创建getter和setter方法,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
gettersAndSetters 使用Getter和Setter的简单方法-Java 这些项目是展示使用getter和setter的简单方法
问题之一是,Java 可能会受到批评,因为它与其他语言不同,它没有真正的 Object 属性,只有诸如方法名称之类的编程习惯用法。 因此,名为 getValue() 的方法可能正在访问属性或调用 Web 服务。 此属性访问实用程序...
FastCoding是一个对Xcode所扩展的插件,主要是根据java的开发工具 eclipse 快速生成属性的getter ,setter 方法有感而发.出于兴趣开发本插件,目前主要功能是根据.h,.m文件声明的属性,自动生成get,set,lazy实例化方法
如何使用它使用 getter/setter 方法创建对象 // Create an object with getter/setter methodvar obj = { name : new getterSetter ( 'John' )} ;// Access the property in read or write modeconsole . log ( obj ...
Intellij 直接将jar包当做插件进行本地安装,重启软件 在实体类中,写好成员的注释,然后Alt+Insert 选择 generateGSDoc即可
通常我们用idea默认生成的getter和setter方法是不带注释的,当然,我们同样可以设置idea像MyEclipse一样生成带有Javadoc的模板,具体设置方法,大家参考下本
PHP类属性的getter和setter的快速生成器。 这基于的扩展名,该扩展名已有3年没有更新了。 我删除了自定义模板功能和所有配置选项。 这是一个简单的扩展,可以完成简单的工作。 我已经更新了扩展以与PHP类型提示...