一 网络基本知识 1.计算机网络OSI分层:物,数,网,传,会,表,应 七层 TCP/IP协议 分为四层 1-2 网络接口层 3 网络层 4 传输层 5-7 应用层
2.网络通讯三要素: (1) ip地址:主机的唯一标识 占4个字节 例如本机127.0.0.1 对应获取的类InetAddress (2) 端口号: 0-65535 (3) 传输协议: 是计算机网络进行数据交换建立的规则 TCP (Tranfer Control Protocol) 面向连接 UDP(User Datagram Protocol) 面向无连接
3 实例1: TCP 客户端-服务端。 客户端: public class ClientDemo { public static void main(String[] args) throws Exception, Exception { //1.创建连接 Socket scoket=new Socket(“127.0.0.1”, 8088); //2.从键盘接收 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //3读取发送过来的数据 BufferedReader reader=new BufferedReader(new InputStreamReader(scoket.getInputStream())); //4.写出去 PrintWriter pw=new PrintWriter(scoket.getOutputStream(),true); String len=null; while((len=br.readLine())!=null){ if(len.equals(“By”)){ break; } pw.println(len); System.out.println(reader.readLine()); } System.out.println(“已断开”); pw.close(); reader.close(); scoket.close(); br.close(); } } 服务端: public class ServerDemo { public static void main(String[] args) throws Exception { //1.创建连接 ServerSocket ssoket=new ServerSocket(8088); //serversocket接收成socket Socket socket = ssoket.accept(); System.out.println(InetAddress.getLocalHost().getHostAddress()+”连接成功”); BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter pw=new PrintWriter(socket.getOutputStream(),true); String len=null; while((len=br.readLine())!=null){ if(“By”.equals(len)){ break; } System.out.println(len);//接收客户端消息打印到控制台 pw.println(len.toUpperCase());//变成大写返回给客户端 } pw.close(); br.close(); socket.close(); ssoket.close(); } } 运行时 先运行服务端,在开客户端
实例二: UDP 发送端到接收端 发送端: public class Client1Demo { public static void main(String[] args) throws SocketException, Exception { //1.创建数据连接 DatagramSocket ds=new DatagramSocket(); //2.发送的类容 String str=”nihao”; byte[] buf=str.getBytes(); //3.发送的类容打包 四参数 数组 长度 主机号 端口号 DatagramPacket dp=new DatagramPacket(buf,buf.length, InetAddress.getByName(“localhost”), 9898); //4.发送 ds.send(dp); //1.数组接收 byte[] buf1=new byte[20]; //数据包接收返回的内容 DatagramPacket dp1=new DatagramPacket(buf1, buf1.length); //接收 ds.receive(dp1); System.out.println(new String(buf1,0,dp1.getLength())); ds.close(); } }
接收端: public class Server1 { public static void main(String[] args) throws Exception { //接收 DatagramSocket ds=new DatagramSocket(9898); byte[] buf1=new byte[20]; DatagramPacket dp1=new DatagramPacket(buf1, buf1.length); ds.receive(dp1); System.out.println(new String(buf1,0,dp1.getLength()));
//返回 String str=”wobuhao”; byte[] buf=str.getBytes(); //接收方发送内容 可根据接收时的的数据包拿到对应发送方的端口号 主机号 DatagramPacket dp=new DatagramPacket(buf,buf.length,dp1.getAddress(),dp1.getPort()); ds.send(dp); ds.close(); } } 先开接收端,再开发送端 只有TCP才涉及IO流的操作,UDP不涉及它有数据包封装
4.URL编程 URL:统一资源定位符 —->协议名:\机器名+端口号+文件名+内部引用 http://www.tomcat.com:80/1.html#BOTTOM
实例:从网上下载照片 public class URLDemo { public static void main(String[] args) { try { DownloadUtil.dowmLoad(“http://preview.quanjing.com/mf101/mf700-08697971.jpg“, “1.jpg”, “e:\a”); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class DownloadUtil{ public static void dowmLoad(String urlstr,String filename,String fileroad) throws IOException{ URL url=new URL(urlstr); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream();//抽象类不能实例化, File file=new File(filename); if(!file.exists()){ file.mkdirs(); } byte[] buff=new byte[1024]; int len=0; OutputStream os=new FileOutputStream(file.getAbsolutePath()+”\”+filename); while((len=is.read(buff))!=-1){ System.out.println(len); os.write(buff,0,len); } os.close(); is.close(); } }