. TCP/IP的参考模型Java的网络编程方面比其他的WINDOWS桌面程序要强大太多了很多复杂的协议接口都已经封装得很完美功能代码只需要很少就完美实现。OSI七层网络模型 TCP/IP四层概念模型 对应网络协议应用层 Application 应用层 HTTP,TFTP, FTP,NFS,WAIS,SMTP表示层 Presentation Telnet, Rlogin, SNMP, Gopher会话层 Session SMTP, DNS传输层 Transport 传输层 TCP, UDP网络层 Network 网络层 IP,ICMP,ARP, RARP, AKP, UUCP数据链路层DataLink 数据链路层 FDDI, Ethernet, Arpanet, PDN,SLIP,PPP物理层 Physical IEEE 802.1A, IEEE802.2到IEEE.802.11小结如何准确的定位到网络上的一台或多台主机。找到主机后如何通信。确定IP和端口号。 IP 类网络通信协议。 TCP和UDP类万物皆对象。 InetAddress 类IP: 用来唯一定位一台网络上的计算机。127.0.0.1 : 本机地址localhost相当于this.IP地址分类 ipv4/ ipv6. 4字节组成IP和6字节组成IP。 42亿4类地址30亿在北美4亿在亚洲。6类地址是未来的地址。 ifconfig可以用来查看地址。128位无符号整数。A类地址00000001, 00000000, 00000000, 0000000101111110, 11111111,11111111,111111101.0.0.1~ 126.255.255.254B类地址: 10000000, 00000001, 00000000, 0000000110111111, 11111110, 11111111,11111110128.1.0.1~ 191.254.255.254C类地址11000000, 00000000, 00000001, 00000001~11011111, - 11111111, 11111110, 11111110192.0.1.1~223.255.254.254D类地址224.0.0.1~239.255.255.254E类地址: 240.0.0.0~255.255.255.254//参数也可以用localhost或者127.0.0.1InetAddress inetAddress InetAddress.getByName(www.Apple.com);使用方法inetAddress.getHostAddress(); inetAddress.getHostName(); 得到对应IP和域名2. TCP的java实现客户端连接服务器Socket(serverIP, port); //port可以是约定serverIP 用InetAddress.getByName(127.0.0.1); 获得。发送消息. 用ossocket.getOutputStream();得到一个发送流对象。然后再调用对象os的write(这是一个消息哦,getbytes());方法。服务器端建立服务器端口 ServerSocket ss new ServerSock(port);再建立一个通信端口变量socket这里不用实例化。用通信端口指向服务器端口的accept: socketss.accept();等待用户的连接 accept接收用户的消息 定义一个输入流对象 InputStream issocket.getInputStream();用socket的对应方法得到输入流。繁琐的流处理用ByteArrayOutputStream()获得流信息并输出。3. UDP的java实现发送端用DatagramSocket建立套接字; 用DatagramPacket 建立UDP数据包。DatagramPacket pkt new DatagramPacket(msg.getBytes(),0,msg.getBytes().length, inetAddress, port); sck.send(pkt); //send方法发送出去就可以了不用管连接没连接。 sck.close();接收端:同样用DatagramSocket建立套接字; 用DatagramPacket 建立UDP数据包。sct new DatagramSocket(port); //繁琐的缓冲数据处理 byte[] buffnew byte[1024]; DatagramPacket pckt new DatagramPacket(buff, 0, buff.length); sct.receive(pckt); //对应send 方法用receive 形成一个阻塞接收。 //以下是需要的信息 pckt.getAddress().getHostName()或getHostAddress(); new String(pckt.getData(), 0, pckt.getLength()); //这个可以得到数据内容。 sct.close();UDP的特点就是没有谁做服务器的概念只要定义好相同的端口谁都可以发送谁都可以接收。4. URL的类使用统一资源定位符 可以定位网上的的某一个资源。协议//ip地址: 端口号/项目名/资源位置。new URL(地址String);getProtocol()得到协议名。getHost();得到主机。getPort()得到端口。getPath()得到地址。getFile()得到文件。getQuery()得到查询名。利用URL下载文件HttpURLConnection urlCon (HttpURLConnection) url.openConnection(); //返回URL的连接 InputStream strm urlCon.getInputStream(); //从连接里得到流资源 FileOutputStream fos new FileOutputStream(str文件流的文件名); //繁琐的读缓冲区 byte[] buff new byte[1024]; int len while((leninputStream.read(buff))!1){ fos.write(buffer, 0, len); //往信件的文件流中写入连接流。 } fos.close(); strm.close(); urlCon.disconnect();几行代码就可以搞定网络资源的文件了。Apache Tomcat的启动:cd /Users/用户名/tomcat/bin进入到tomcat的bin目录下输入./startup.sh 回车如出现错误“Permission denied”操作失败缺少权限输入命令赋予超级管理员权限sudo chmod 755 *.sh 回车再次执行命令./startup.sh 回车打开浏览器输入网址 http://localhost:8080/ 如果出现一只三角猫表示tomcat安装成功关闭的话./shutdown.sh 回车核心配置文件./conf/server.xml5. 示例实现一个标准的TCP/IP通信下面是服务端需要先启动形成一个通信阻塞。核心方法: ServerSocket; Socket; ByteArrayOutputStream; InputStream; buffer的应用以及各种异常处理。package UseInternet; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServerDemo01 { public static void main(String[] args) { ByteArrayOutputStream baos null; ServerSocket serverSocket null; Socket socket null; InputStream is null; try { serverSocket new ServerSocket(9999); socket serverSocket.accept(); //这里形成一个阻塞,等待接收客户端信息. is socket.getInputStream(); baos new ByteArrayOutputStream(); byte[] buffer2 new byte[1024]; int len2 0; while ((len2is.read(buffer2))!-1){ baos.write(buffer2,0,len2); } System.out.println(baos.toString()); System.out.println(Got msg); } catch (IOException e) { e.printStackTrace(); } finally { if (baos!null){ try { baos.close(); }catch (IOException e){ e.printStackTrace(); } } if (is!null){ try{ is.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket!null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } }; if (serverSocket!null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }下面是客户端代码。package UseInternet; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class TcpClientDemo1 { public static void main(String[] args) { Socket socket null; OutputStream os null; try { //获得服务器的IP和端口号 InetAddress serverIP InetAddress.getByName(127.0.0.1); int port 9999; //创建一个socket连接 socket new Socket(serverIP, port); os socket.getOutputStream(); os.write(你好这是一个网络外发的消息OS.getBytes()); // 关键修复1.刷新输出流确保数据全部发送 2.关闭输出流发送流结束标记 os.flush(); socket.shutdownOutput(); } catch (UnknownHostException e) { e.printStackTrace(); //throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); // 建议不要直接throw RuntimeException方便排查问题 }finally { if (os!null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }解析各种网址的IP信息了解InetAddress.getByName; InetAddress.getLocalHost; getAddress; getCanonicalHostName; getHostAddress; getHostName等常用方法。import java.net.InetAddress; import java.net.UnknownHostException; public class TestInetAddress { public static void main(String[] args) { try{ //查询本机地址 InetAddress inetAddress1 InetAddress.getByName(127.0.0.1); System.out.println(inetAddress1); InetAddress inetAddress2 InetAddress.getByName(localhost); System.out.println(inetAddress2); InetAddress inetAddress3 InetAddress.getLocalHost(); System.out.println(inetAddress3); InetAddress inetAddress4 InetAddress.getByName(www.baidu.com); System.out.println(inetAddress4); InetAddress inetAddress5 InetAddress.getByName(www.Apple.com); System.out.println(inetAddress5); System.out.println(); System.out.println(inetAddress5.getAddress()); //拿IP地址的字节数据(底层二进制,很少用) System.out.println(inetAddress5.getCanonicalHostName()); //拿规范主机名,强制反向DNS解析 System.out.println(inetAddress5.getHostAddress()); //只拿IP地址字符串(最常用) System.out.println(inetAddress5.getHostName()); //拿主机名(优先用已知的,不强制反向解析 }catch (UnknownHostException e){ System.out.println(e); e.printStackTrace(); } } }UDP发送与接收服务端需要了解类和方法DatagramSocket; DatagramPacket; datagramSocket.receive; datagramSocket.send.package UseInternet; import java.net.DatagramPacket; import java.net.DatagramSocket; public class TestUDPserver { public static void main(String[] args) throws Exception { DatagramSocket sct; sct new DatagramSocket(8888); byte[] buffnew byte[1024]; DatagramPacket dgp new DatagramPacket(buff,0,buff.length); sct.receive(dgp); //阻塞接收 System.out.println(dgp.getAddress().getHostName()|||dgp.getAddress().getHostAddress()); System.out.println(new String(dgp.getData(),0,dgp.getLength())); sct.close(); } }客户端严格说没有服务端和客户端的区别都是使用同样的DatagramSocket类。都可以发送和接收。package UseInternet; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class TestUDP { public static void main(String[] args) throws Exception { DatagramSocket skt new DatagramSocket(); String msg这是一个UDP的发送测试; InetAddress idr InetAddress.getByName(localhost); int port 8888; DatagramPacket dgp new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,idr,port); skt.send(dgp); //这里不需要建立连接 skt.close(); } }网络资源下载核心类HttpURLConnection FileOutputStream;package UseInternet; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class UseURLDownLoad { public static void main(String[] args) throws IOException { URL url new URL(https://himg.bdimg.com/sys/portrait/item/pp.1.f0337f07.qLRrh6j5cr5u6SFhRwgS1A?_t1764409787836); HttpURLConnection urlCon (HttpURLConnection) url.openConnection(); InputStream strm urlCon.getInputStream(); FileOutputStream fos new FileOutputStream(阳光和青草.jpg); byte[] buff new byte[1024]; int len; while ((lenstrm.read(buff))!-1){ fos.write(buff,0,len); } fos.close(); strm.close(); urlCon.disconnect(); } }