网络参考模型:
网络通讯要素:
l IP地址:
网络中设备的标识。
不易记忆,可以用主机名。
本机回环地址:127.0.0.1 主机名:localhost
l 端口号:
用于标识进程的逻辑地址,不同进程的标识。
有效端口:0~65536 ,其中0~1024为系统使用或保留端口。
l 传输协议:
通信的规则:TCP、UDP
Java.net.InetAddress 类:
此类表示互联网协议 (IP) 地址。
代码示例:
public class IpAddress { public static void main(String[] args) { InetAddress home = null; try { //ia = InetAddress.getByName("www.baidu.com");//在给定主机名的情况下确定主机的 IP地址。 //String s = ia.getCanonicalHostName();//获取此 IP地址的完全限定域名。 home = InetAddress.getLocalHost(); System.out.println(home.getHostAddress()+" "+home.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); } } }
|
TCP和UDP:
UDP:
1. 将数据及源和目的封装到数据包中,不需要建立连接。
2. 每个数据包的大小限制64K。
3. 不可靠连接。
4. 速度快。
TCP:
1. 建立连接形成传输数据的通道。
2. 在连接中进行大量数据传输。
3. 通过三次握手完成连接,可靠。
4. 传输速度慢,效率低。
Socket:
是为网络服务提供的一种机制,通讯两端都有Socket,网络通讯就死Socket通讯,通过IO操作。
UDP:
UDP发送数据:
public class IpAddress { public static void main(String[] args) { try { //通过DatagramSocket创建 UDB的 Socket 服务。 DatagramSocket ds = new DatagramSocket(); //确定数据,使用 public final class DatagramPacket类封装成对象。 byte b [] ="苏联空军分了手机分离设计".getBytes(); InetAddress ia = InetAddress.getByName("127.0.0.1"); DatagramPacket dp = new DatagramPacket(b,b.length,ia,80); //通过Socket服务,将数据发送 ds.send(dp); //关闭Socket ds.close(); } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
UDP接受数据:
public class UDBReceive { public static void main(String[] args) { try { //建立Socket服务,监听在8888端口 DatagramSocket ds = new DatagramSocket(8888); //建立数据包,通过8888端口进行读取 byte buf [] =newbyte [1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); //阻塞式接受数据,并存放在dp中 ds.receive(dp); System.out.println(new String(buf)); System.out.println("ip地址:"+dp.getAddress()); //关闭服务 ds.close(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
TCP :
服务端对象:ServerSocket
public class TestTcp { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1",8866); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(s.getOutputStream()))); OutputStreambw = s.getOutputStream(); String str = br.readLine(); bw.write(str); s.close(); } catch (IOException e) { e.printStackTrace(); } } } |
public class ServerDemo { public static void main(String[] args) { ServerSocket ss = null; try { ss = new ServerSocket(8866); while(true) { Socket s = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); String str = br.readLine(); System.out.println(s.getInetAddress()); System.out.println("有一个连接"+" "+str ); } } catch (IOException e) { e.printStackTrace(); }finally { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } |
上传文件:
客户端:
public class UpClient { public static void main(String[] args) { Socket s = null; try { s = new Socket("127.0.0.1",9998); FileInputStream fis = new FileInputStream("G:\\PDF教程\\CoreJava笔记.pdf"); BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream()); byte b [] =newbyte[1024]; intlen; while((len = fis.read(b))!=-1) { bos.write(b,0,b.length); } bos.flush(); s.shutdownOutput(); fis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); }finally{ try { s.close(); } catch (IOException e) { e.printStackTrace(); } } } |
服务端:
public class UpServer { public static void main(String[] args) { ServerSocket ss = null; try { ss = new ServerSocket(9998); while(true) { Socket s = ss.accept(); System.out.println(s.getInetAddress().getHostAddress()+"已连接"); BufferedInputStream bis = new BufferedInputStream(s.getInputStream()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:\\AKD-73617405806.pdf")); byte[] b =newbyte[1024]; intlen = 0; while((len=bis.read(b))!=-1) { bos.write(b,0,b.length); } bos.flush(); } } catch (IOException e) { e.printStackTrace(); }finally { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } }
} |
Java.net.URL:
类 URL
代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。
URI的范围要比URL范围大。
方法摘要:
返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。 openConnection方法:
每次调用此 URL 的协议处理程序的 openConnection 方法都打开一个新的连接。
如果 URL 的协议(例如,HTTP 或 JAR)存在属于以下包或其子包之一的公共、专用 URLConnection 子类:java.lang、java.io、java.util、java.net,返回的连接将为该子类的类型。例如,对于 HTTP,将返回 HttpURLConnection,对于 JAR,将返回 JarURLConnection。
public class UrlTest { public static void main(String[] args) throws Exception { URL url = new URL("http://www.baidu.com"); URLConnection uc = url.openConnection(); System.out.println(uc); InputStream in = uc.getInputStream(); byte b[] = new byte[1024]; in.read(b); System.out.print(new String(b,0,b.length)); } } |