计算机网络:把分布在不同地理区域的计算机与专门的外部设备用通信线路互联成一个规模大,功能强的网络系统,从而使众多的 计算机可以方便地互相传递信息,共享硬件,软件,数据信息等资源。
网络编程的目的:直接或间接地通过网络协议与其它计算机实现数据交换,进行通讯。
网络编程中由二个主要的问题:如何准确地定位网络上一台或多台主机:定位主机上的特定的应用
找到主机后如何可靠高效地进行数据传输
如何实现网络中的主机互相通信:1>通信双方地址:IP 端口号 2>一定的规则:OSI参考模型(模型过于理想化,未能在银泰 网上进行广泛推广) TCP/IP参考模型(事实上的国际标准)
IPV4:4个字节组成
IPV6:128位
IP地址分类方式2:公网地址(万维网使用)和私有地址(局域网使用)。192.168.开头的就是私有地址,范围为 192.0.0~192.168.255.255,专门为组织机构内部使用。域名:www.baidu.com
实例化InetAddress: getByName(String host)或者get.LocalHost()
常用方法:getHostName()/getHostAddress()
注册端口:1024~49151,分配给用户进程或应用程序(如Tomcat占8080,MySQL占3306,Oracle占1521)
动态/私有端口:49152-65535
端口号和IP地址的组合得出一个网络套接字:SocketTCP/UDP:传输控制协议(TCP)和网络互连协议(IP)而得名,包括多个具有不同功能且为关联的协议
IP:是网络层的主要协议,支持网间互连的数据通信
TCP/IP:从实用的角度出发,形成了高效的四层体系结构,即物理链路层,IP层,传输层和应用层。
TCP协议:使用TCP协议前,需建立TCP连接,形成传输数据通道
传输前使用“三次握手”方式,点对点通信,是可靠的
TCP协议进行通信的二个应用进程:客户端,服务端
在连接中可进行大数据量的传输
传输完毕,需释放已建立的连接,效率低
UDP协议:将数据,源,目的封装成数据包,不需要建立连接
每个数据报的大小控制在62k内
发送不管对方是否准备好,接收方收到也不确认,故是不可靠的
可以广播发送
发送数据结束时无需释放资源,开销小,速度快
例一:客户端发送内容给服务器,服务端将内容打印到控制台上
public void client(){ Socket socket = null; OutputStream os = null; try { //1.创建Socket对象,指明服务器端的ip和端口号 InetAddress inet = InetAddress.getByName("127.0.0.1"); socket = new Socket(inet,8899); //2.获取一个输出流,用于输出数据 os = socket.getOutputStream(); //3.写出数据的操作 os.write("你好,我是客户端".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { //4.关闭资源 os.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } public void serve(){ ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1.创建服务器端的socket,指明自己的端口号 ss = new ServerSocket(8899); //2.调用accept()方法接受来自于客户端的socket socket = ss.accept(); //3.获取输入流 is = socket.getInputStream(); // byte[] bytes = new byte[1024]; 不介意这样写,会有乱码 // int len; // while ((len = is.read(bytes)) !=-1){ // String str = new String(bytes,0,len); // System.out.print(str); // } //4.读取数据 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while ((len =is.read(buffer)) != -1){ baos.write(buffer,0,len); } System.out.println(baos.toString()); } catch (IOException e) { e.printStackTrace(); } finally { try { //关闭资源 baos.close(); is.close(); socket.close(); ss.close(); } catch (IOException e) { e.printStackTrace(); } }例二:客服端发送文件给服务端,服务端将文件保存在本地
public void client() throws IOException{ Socket socket = new Socket(InetAddress.getByName ("127.0.0.1"),9090); OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("C://Users//smallBottle//Desktop//222.png")); byte [] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1){ os.write(buffer,0,len); } fis.close(); os.close(); socket.close(); } @Test public void server() throws IOException { ServerSocket ss = new ServerSocket(9090); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("C://Users//smallBottle//Desktop//333.png")); byte [] bytes = new byte[1024]; int len; while ((len = is.read(bytes)) != -1){ fos.write(bytes ,0,len); } fos.close(); is.close(); socket.close(); ss.close(); } }例三:从客户端发送文件给服务端,服务端保存到本地,并返回成功发送给客户端
public void client() throws IOException{ Socket socket = new Socket(InetAddress.getByName ("127.0.0.1"),9090); OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("C://Users//smallBottle//Desktop//222.png")); byte [] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1){ os.write(buffer,0,len); } //关闭数据传输 socket.shutdownOutput(); //接受来自服务端的数据,并显示到控制台上 InputStream is = socket.getInputStream() ; ByteArrayOutputStream baos = new ByteArrayOutputStream() ; byte [] bufferr = new byte[20]; int lenn ; while ((lenn = is.read(bufferr)) != -1){ baos.write(bufferr,0,len); } System.out.print(baos.toString ()); fis.close(); os.close(); socket.close(); baos.close(); } @Test public void server() throws IOException { ServerSocket ss = new ServerSocket(9090); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("C://Users//smallBottle//Desktop//7.png")); byte [] bytes = new byte[1024]; int len; while ((len = is.read(bytes)) != -1){ fos.write(bytes ,0,len); } System.out.print("图片传输完成"); //服务端给客户端反馈 OutputStream os = socket.getOutputStream(); os.write("你好,文件已收到".getBytes()); fos.close(); is.close(); socket.close(); ss.close(); os.close(); }UDP网络通信:类DatagramSocket和DatagramPacket实现了基于UDP协议网络程序。
UDP数据报通过数据报套接字DatagramPacket发送和接收,系统不保证UDP数据报一定能够安全送到目的地,也不能确定什么时候抵达。
DatagramPacket对象封装了UDP数据报,在数据中包含了发送端的IP地址和端口号以及接受端的IP地址和端口号。
UDP协议每个数据报都给出了完整的地址信息,因此无需建立发送方和接收方,如同发快递包裹一样。
public void sender()throws IOException{ DatagramSocket socket = new DatagramSocket() ; String str= "我是UDP方式发送的"; byte[] data = str.getBytes(); InetAddress inet = InetAddress .getByName("127.0.0.1"); DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090); socket.send(packet); socket.close(); } @Test public void receiver() throws IOException{ DatagramSocket socket = new DatagramSocket(9090) ; byte[] buffer = new byte[100]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); socket.receive(packet); System.out.print(new String(packet.getData(),0,packet.getLength())); socket.close(); } }URL类:统一资源定位符,它表示Internet上某一资源的地址。
它是一种具体的URL,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。
URL的基本结构:<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
URL类的构造器:public URL(String spec):通过一个URL地址的字符串构造一个URL对象
public URL(URL context,String spec)
public URL(String protocol,String host,String file)
public URL(String protocol,String host,int port,String file)
URL类的常用方法:public String getProtocol():获取该URL的协议名
public String getHost():获取该URL的主机名
public String getPort():获取该URL的端口号
public String getPath():获取该URL的文件路径
public String getFile():获取该URL的文件名
public String getQuery():获取该URL的查询名
public class Ip1 { public static void main(String[] args){ URL url = new URL("http://localhost:8080/222.png"); System.out.println(url.getProtocol() ); System.out.println(url.getHost()); } } class IP2{ URL url = new URL("http://localhost:8080/222.png"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); InputStream is = urlConnection.getInputStream(); FileOutputStream fos = new FileOutputStream("dss.png"); byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer,0,len); } is.close(); fos.close(); urlConnection.disconnect(); }