├── .gitignore ├── LICENSE ├── README.md ├── aidl_structure.png ├── handler-howtowork.png ├── tcpconnect.png ├── tcpdisconnect.png ├── tcp快重传.png └── tcp慢开始.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-InterView 2 | android 面试汇总 3 | ##如果后台的Activity由于某原因被系统回收了,如何在被系统回收之前保存当前状态? 4 | 在onSaveInstanceState()中保存其状态。activity被置于后台时,在执行onStop之前,通过onSaveInstacneState 5 | 保存当前activity状态。如果被系统杀死,再次被调起时,,在onResume之前,onStart之后,通过 6 | onRestoreInstanceState()来恢复其状态。如果没有被系统杀死,则不执行onRestoreInstanceState。 7 | 如果用户点击back键,则onSaveInstanceState()不会被调用。onSaveInstanceState()在以下几种情况会被调用: 8 | 1,按home键时;2,横竖屏切换时;3,按电源键时;4,从当前activity进入另一个activity时。 9 | 系统默认只会保存ui状态--view hierarchy,比如editext里面的text,或者listview的scroll位置, 10 | 不会保存别的信息,如果需要保存则复写此方法。 11 | ##2,请解释下在单线程模型中Message、Handler、Message Queue、Looper之间的关系。 12 | Message:包含消息id,存放携带消息数据的消息对象,MessageQueue 13 | 统一管理,handler发送和处理; 14 | MessageQue 消息队列,管理message的队列,存放handler发过来的消息,链表串联, 15 | 经由looper抽取 16 | Looper:消息泵,不断抽取messageQueue中的消息,一个messageque对应 17 | 一个looper。实现消息队列创建和消息循环功能。静态方法prepare和loop。 18 | handler: 用来发送message和处理message。在使用handler之前必须使用looper创建了 19 | 队列,否则抛出异常。需要实现handleMessage来处理message。Thread 线程 负责调 20 | 度整个消息循环,即消息循环的执行场所。 21 | 有个问题,为什么非要出来个looper,而不是把looper的功能放到handler里面实现呢? 22 | 我觉得是面向对象的原则,由于looper里面的功能基本不怎么变化,也不需要用户了解, 23 | 因此将其封装,仅仅将handler和message对用户开放去调用。 24 | 25 | ![mahua]( https://github.com/paceboy/Android-InterView/blob/master/handler-howtowork.png) 26 | 27 | 在非主线程中直接new Handler() 会报如下的错误: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 28 | 原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。 29 | 30 | ##TCP的3次握手和4次挥手? 31 | 3次握手连接,首先连接端发送一个连接请求,syn为1,接收端回复一个确认表示同意连接, 32 | 连接端发送确认,表示收到接收端的连接确认,至此连接成功,可以全双工的发送数据。 33 | 34 | 4次断开连接,首先连接端发送fin为1的请求,表示自己不再发送数据,接收端收到后发送 35 | 确认,并再次发送一个自己的fin请求,表示自己也不再发送数据,连接端发送确认,至此 36 | 断开连接成功。 37 | ![mahua](https://github.com/paceboy/Android-InterView/blob/master/tcpconnect.png) 38 | ![mahua](https://github.com/paceboy/Android-InterView/blob/master/tcpdisconnect.png) 39 | ##tcp三次握手存在的风险? 40 | 攻击方伪造IP地址发送大量的SYN包,使服务端发送的SYN+ACK包得不到确认,服务器端将 41 | 为了维护一个非常大的半连Syn攻击就是 攻击客户端 在短时间内伪造大量不存在的IP地址, 42 | 向服务器不断地发送syn包,服务器回复确认包,并等待客户的确认,由于源地址是不存在的 43 | ,服务器需要不断的重发直 至超时,这些伪造的SYN包将长时间占用未连接队列,正常的SYN 44 | 请求被丢弃,目标系统运行缓慢,严重者引起网络堵塞甚至系统瘫痪。 45 | Syn攻击是一个典型的DDOS攻击。检测SYN攻击非常的方便,当你在服务器上看到大量的半连 46 | 接状态时,特别是源IP地址是随机的,基本上可以断定这是一次SYN攻击.在Linux下可以如 47 | 下命令检测是否被Syn攻击 48 | netstat -n -p TCP | grep SYN_RECV 49 | 一般较新的TCP/IP协议栈都对这一过程进行修正来防范Syn攻击,修改tcp协议实现。主要方 50 | 法有SynAttackProtect保护机制、SYN cookies技术、增加最大半连接和缩短超时时间等. 51 | 但是不能完全防范syn攻击。接列表而消耗非常多的资源,服务器失去响应,即服务器端受 52 | 到了SYN Flood攻击。 53 | ##为什么关闭连接要四次挥手? 54 | TCP是全双工通道,一方发起关闭请求只关闭了一个方向上的通道,所以建立连接的双方都 55 | 要发起关闭请求并确认来关闭各自方向上的数据通道。 56 | ##为什么建立连接要三次握手? 57 | 目的: 防止已经失效的连接请求到达服务端,创建无效的连接,浪费资源。 58 | 说明: 当客户端发出的第一个连接请求在网络上的某个节点被滞留了(网络会存在许多不 59 | 可靠的因素),过一段时间后突然又到达了服务端,服务端误以为这是一个新的建立连接 60 | 的请求,于是就会向客户端发出确认包并建立连接。 61 | 实际上客户端当前并没有发出创建连接的请求,就会丢弃服务端的确认包。而服务端却创 62 | 建了连接并等待客户端发送数据,浪费了相关的资源。 63 | ### tcp拥塞控制机制? 64 | 计算机网络中的带宽、交换节点中缓存和处理机等,都是网络资源。在某段时间,若对网络 65 | 中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏。这种情况就叫 66 | 做阻塞。拥塞控制就是防止过多的数据注入网络中,这样可以使网络中的路由器或链路不致 67 | 过载。拥塞控制是一个全局性的过程,和流量控制不同,流量控制指点对点通信量的控制。 68 | (1) 拥塞窗口(cwnd):拥塞控制的关键参数,它描述源端在拥塞控制情况下一次最多能发送 69 | 的数据包的数量。 70 | (2) 通告窗口(awin):接收端给源端预设的发送窗口大小,它只在TCP连接建立的初始阶段 71 | 发挥作用。 72 | (3) 发送窗口(win):源端每次实际发送数据的窗口大小。 73 | (4) 慢启动阈值(ssthresh):拥塞控制中慢启动阶段和拥塞避免阶段的分界点。初始值通 74 | 常设为65535byte。 75 | (5) 回路响应时间(RTT):一个TCP数据包从源端发送到接收端,源端收到接收端确认的时间 76 | 间隔。 77 | (6) 超时重传计数器(RTO):描述数据包从发送到失效的时间间隔,是判断数据包丢失与否 78 | 及网络是否拥塞的重要参数。通常设为2RTT或5RTT。 79 | (7) 快速重传阈值(tcprexmtthresh)::能触发快速重传的源端收到重复确认包ACK的个数。 80 | 当此个数超过tcprexmtthresh时,网络就进入快速重传阶段。tcprexmtthresh缺省值为3。 81 | ####1,慢开始与拥塞避免 82 | 发送方维持一个叫做拥塞窗口cwnd(congestion window)的状态变量。拥塞窗口的大小取决 83 | 于网络的拥塞程度,并且动态地在变化。发送方让自己的发送窗口等于拥塞窗口个,另外 84 | 考虑到接收方的接受能力,发送窗口可能小于拥塞窗口。 85 | 慢开始算法的思路就是,不要一开始就发送大量的数据,先探测一下网络的拥塞程度, 86 | 就是由小到大的逐渐增加拥塞窗口的大小。如图所示 87 | ![](https://github.com/paceboy/Android-InterView/blob/master/tcp%E6%85%A2%E5%BC%80%E5%A7%8B.png) 88 | 89 | 当建立连接后,设置cwnd为1(这里用报文段的个数的拥塞窗口大小举例说明慢开始算法 90 | ,实时拥塞窗口大小是以字节为单位的),发送1个数据包,接收方接收到后,发送确认 91 | 信息;cwnd变为1×2,发送2个数据包,接收方接收到后,发送2个确认信息;cwnd变为2×2 92 | ,发送4个数据包,接收方收到后,发送4个确认信息,cwnd变为4×2... 93 | 为了防止cwnd增长过大引起网络拥塞,还需设置一个慢开始门限ssthresh状态变量。 94 | ssthresh的用法如下: 95 | 当cwndssthresh时,改用拥塞避免算法。 97 | 当cwnd=ssthresh时,慢开始与拥塞避免算法任意。 98 | 拥塞避免算法让拥塞窗口缓慢增长,即每经过一个往返时间RTT就把发送方的拥塞窗口 99 | cwnd加1,而不是加倍。这样拥塞窗口按线性规律缓慢增长。 100 | 无论是在慢开始阶段还是在拥塞避免阶段,只要发送方判断网络出现拥塞(其根据就是 101 | 没有收到确认,虽然没有收到确认可能是其他原因的分组丢失,但是因为无法判定,所 102 | 以都当做拥塞来处理),就把慢开始门限设置为出现拥塞时的发送窗口大小的一半。然 103 | 后把拥塞窗口设置为1,执行慢开始算法。 104 | ####2,快重传和快恢复 105 | 快重传要求接收方在收到一个失序的报文段后就立即发出重复确认,而不要等到自己发 106 | 送数据是捎带确认。快重传算法规定,发送发只要一连收到3个重复确认就应当立即重传 107 | 对方尚未收到的报文段,而不必继续等待设置的重传计时器时间到期。 108 | 快重传配合使用的还有快恢复算法,有以下两个要点: 109 | ①当发送方连续收到三个重复确认时,就执行“乘法减小”算法,把ssthresh门限减半。但 110 | 是接下去并不执行慢开始算法。 111 | ②考虑到如果网络出现拥塞的话就不会收到好几个重复的确认,所以发送方现在认为网络 112 | 可能没有出现拥塞。所以此时不执行慢开始算法,而是将cwnd设置为ssthresh的大小,然 113 | 后执行拥塞避免算法。如下图: 114 | ![](https://github.com/paceboy/Android-InterView/blob/master/tcp%E5%BF%AB%E9%87%8D%E4%BC%A0.png) 115 | 参考:http://blog.csdn.net/sicofield/article/details/9708383 116 | http://blog.chinaunix.net/uid-27122224-id-3276910.html 117 | http://www.cnblogs.com/zhuzheic/archive/2013/04/08/3008364.html 118 | 119 | ###Android ipc的方式aidl的机制?与传统的linux ipc区别? 120 | 先说区别,aidl是解决android进程通信问题,是一种rpc的调用方式。由Dianne Hackbor提出。传统的linux通信有pipe管道、信号和跟踪。但其都局限于父进程与子进程之间,或者兄弟进程之间,后来增加了命名管道,使得不再局限于亲戚之间进程,后来AT&T Unix又增加报文队列,共享内存和信号量,BSD Linux增加了socket进程通信机制。为什么android自己设计一个新的ipc方式,原因如下: 121 | 另一方面是传输性能。socket作为一款通用接口,其传输效率低,开销大,主要用在跨网络的进程间通信和本机上进程间的低速通信。消息队列和管道采用存储-转发方式,即数据先从发送方缓存区拷贝到内核开辟的缓存区中,然后再从内核缓存区拷贝到接收方缓存区,至少有两次拷贝过程。共享内存虽然无需拷贝,但控制复杂,难以使用。 122 | 各种IPC方式数据拷贝次数如下表: 123 | 124 | IPC 数据copy次数 125 | 共内存 0 126 | Binder 1 127 | Socket/Pipe/消息队列 2 128 | 129 | 还有一点是出于安全性考虑。Android作为一个开放式,拥有众多开发者的的平台,应用程序的来源广泛,确保智能终端的安全是非常重要的。终端用户不希望从网上下载的程序在不知情的情况下偷窥隐私数据,连接无线网络,长期操作底层设备导致电池很快耗尽等等。传统IPC没有任何安全措施,完全依赖上层协议来确保。首先传统IPC的接收方无法获得对方进程可靠的UID/PID(用户ID/进程ID),从而无法鉴别对方身份。Android为每个安装好的应用程序分配了自己的UID,故进程的UID是鉴别进程身份的重要标志。使用传统IPC只能由用户在数据包里填入UID/PID,但这样不可靠,容易被恶意程序利用。可靠的身份标记只有由IPC机制本身在内核中添加。其次传统IPC访问接入点是开放的,无法建立私有通道。比如命名管道的名称,system V的键值,socket的ip地址或文件名都是开放的,只要知道这些接入点的程序都可以和对端建立连接,不管怎样都无法阻止恶意程序通过猜测接收方地址获得连接。 130 | 131 | Binder提供了远程过程调用RPC功能,英文意思是粘结剂的意思。在android的binder机制中,由一系列组件组成,分别是client,server,service manager和binder驱动程序,其中client,server和service manager运行在用户空间,binder驱动程序运行在内核空间,binder就是将这四个组件粘合在一起的粘结剂,其中核心组件便是binder驱动程序,service manger提供了辅助管理功能,client和server正式在binder驱动和service manger提供的基础设施上,进行client-server之间的通信。Service Manager和Binder驱动已经在Android平台中实现好,开发者只要按照规范实现自己的Client和Server组件就可以了。如图所示 132 | ![](https://github.com/paceboy/Android-InterView/blob/master/aidl_structure.png) 133 | 134 | 调用方式的实现参考:http://blog.csdn.net/songjinshi/article/details/22918405 135 | 参考:http://www.cnblogs.com/albert1017/p/3849585.html 136 | ###AMS WMS作用? 137 | 1,ams :进程管理,内存管理,统一调各应用程序的activity 138 | 2,wms:管理所有窗口。创建、删除窗口,设置焦点窗口。保持窗口层次关系,以便surfaceflinger能据此绘制屏幕。将窗口信息传递给inputmanager对象,以便inputdispatcher能够把输入消息拍发给和屏幕上显示一致的窗口。 139 | -------------------------------------------------------------------------------- /aidl_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paceboy/Android-InterView/74cd9d9f88eda1a4dae2c5d448332e4b32a67fb8/aidl_structure.png -------------------------------------------------------------------------------- /handler-howtowork.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paceboy/Android-InterView/74cd9d9f88eda1a4dae2c5d448332e4b32a67fb8/handler-howtowork.png -------------------------------------------------------------------------------- /tcpconnect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paceboy/Android-InterView/74cd9d9f88eda1a4dae2c5d448332e4b32a67fb8/tcpconnect.png -------------------------------------------------------------------------------- /tcpdisconnect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paceboy/Android-InterView/74cd9d9f88eda1a4dae2c5d448332e4b32a67fb8/tcpdisconnect.png -------------------------------------------------------------------------------- /tcp快重传.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paceboy/Android-InterView/74cd9d9f88eda1a4dae2c5d448332e4b32a67fb8/tcp快重传.png -------------------------------------------------------------------------------- /tcp慢开始.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paceboy/Android-InterView/74cd9d9f88eda1a4dae2c5d448332e4b32a67fb8/tcp慢开始.png --------------------------------------------------------------------------------