Day 8.5 狂神说Java基础笔记(网络编程03-07)

03-IP

IP地址:InetAddress

  • 唯一定位一台网络上的计算机
  • 127.0.0.1:本机localhost
  • ip地址分类
  • ipv4/ipv6:
  • ipv4 4个字节,每个字节0~255,共42亿,30亿在北美
  • ipv6 128位,8个无符号整数。2001:2001:2001:2001:2001:2001:2001:2001
  • 公网/私网
  • ABCDE类地址
  • 192.168.xx.xx,专门给组织内部使用
04-端口

端口表示计算机上一个程序的进程

  • 0~65535
  • TCP:UDP 65535*2,单个协议下,端口不能冲突
  • 端口分类
    • 公有端口,尽量不要占用:0~1023
      • https:443
      • http:80
      • ftp:21
      • telent:23
    • 程序注册端口:1024-49151,分配用户或者程序
      • Tomcat:8080
      • MySQL:3306
      • Oracle:1521
        -动态私有端口:49152-65535
package network;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
 public static void main(String[] args) {
  try {
   //InetAddress inetAddress1=InetAddress.getByName("127.0.0.1");
   //InetAddress inetAddress1=InetAddress.getByName("localhost");
   InetAddress inetAddress1=InetAddress.getLocalHost();
   InetSocketAddress socketAddress=new InetSocketAddress("127.0.0.1",8080);
   System.out.println(socketAddress.getHostName());
   System.out.println(inetAddress1);
   //查询网站地址
   InetAddress inetAddress2=InetAddress.getByName("www.baidu.com");
   System.out.println(inetAddress2);
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
netstat -ano#查看所有端口
netstat -ano|findstr "5900"#参看指定的端口
tasklist|finderstr “8696” #查看指定端口的进程
05-通信协议

TCP:用户传输协议
UDP协议:用户数据报协议
IP协议:网络互连协议
计算机四级考试

06-TCP实现聊天
TcpClient.class
package chatroom;
//客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
 public static void main(String[] args) {
  Socket socket =null;
  OutputStream os=null;
  try {
   //1.要知道服务器的地址
   InetAddress serverIP =InetAddress.getByName("127.0.0.1");
   //2.端口号
   int port=9999;
   //创建一个socket连接
   socket =new Socket(serverIP,port);
   //发送消息
   os=socket.getOutputStream();
   os.write("hello".getBytes());
  }catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   if (os!=null) {
    try {
     os.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (socket!=null) {
    try {
     socket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 } 
}
TCPServer.class
package chatroom;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServer {
 public static void main(String[] args) {
  ServerSocket serverSocket = null;
  Socket socket = null;
  InputStream is = null;
  ByteArrayOutputStream baos = null;
  try {
   //1.有一个地址:localhost:9999
   serverSocket=new ServerSocket(9999);   
   while (true) {
    //等待客户端连接
    socket = serverSocket.accept();
    //3.读取客户端的消息
    is = socket.getInputStream();
    //管道流:避免乱码
    baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    while ((len = is.read(buffer)) != -1) {
     baos.write(buffer, 0, len);
    }
    System.out.println(baos.toString());
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   //关闭资源
   if (baos!=null) {
    try {
     baos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (is!=null) {
    try {
     is.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (socket!=null) {
    try {
     socket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (serverSocket!=null) {
    try {
     serverSocket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }   
  }
 }
}
07-文件上传
TcpClient.class
package chatroom;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
//客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
 public static void main(String[] args) {
  Socket socket =null;
  OutputStream os=null;
  FileInputStream fis = null;
  InputStream is =null;
  ByteArrayOutputStream baos=null;
  try {
   //1.要知道服务器的地址
   InetAddress serverIP =InetAddress.getByName("127.0.0.1");
   //2.端口号
   int port=9999;
   //创建一个socket连接
   socket =new Socket(serverIP,port);
   os=socket.getOutputStream();
   //读取文件流
   fis = new FileInputStream(new File("headright.jpg"));
   //写出文件不好,写法占用资源
   byte[] buffer =new byte[1024];
   int len;
   while ((len=fis.read(buffer))!=-1) {
    os.write(buffer, 0,len);    
   }
   //?通知已经传输完了
   socket.shutdownOutput();
   //确定接受,断开连接
   is=socket.getInputStream();
   baos=new ByteArrayOutputStream();
   byte[] buffer2 = new byte[1024];
      int len2;
      while ((len2 = is.read(buffer2)) != -1) {
       baos.write(buffer2, 0, len2);
      }
      System.out.println(baos.toString());
  }catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    baos.close();
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   try {
    is.close();
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   if (fis!=null) {
    try {
     fis.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (os!=null) {
    try {
     os.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (socket!=null) {
    try {
     socket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 } 
}
TCPServer.class
package chatroom;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServer {
 public static void main(String[] args) {
  ServerSocket serverSocket = null;
  Socket socket = null;
  InputStream is = null;
  FileOutputStream fos = null;
  OutputStream os = null;
  try {
   //1.有一个地址:localhost:9999
   serverSocket=new ServerSocket(9999);
   //阻塞式监听,会一直等待客户端连接
   socket=serverSocket.accept();
   //获取输入流
   is = socket.getInputStream();
   //文件输出
   fos=new FileOutputStream(new File("receive.jpg"));
   byte[] buffer =new byte[1024];
   int len;
   while ((len=is.read(buffer))!=-1) {
    fos.write(buffer, 0,len);    
   }
   //通知客户端,接收完毕了
   os = socket.getOutputStream();
   os.write("接收完毕,可以断开".getBytes());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   //关闭资源
   try {
    os.close();
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   if (fos!=null) {
    try {
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (is!=null) {
    try {
     is.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (socket!=null) {
    try {
     socket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (serverSocket!=null) {
    try {
     serverSocket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }   
  }
 }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值