网络编程
文章目录
- 网络编程
- 1.1、概述
- 目的:
- 想达成的效果:
- 1.2、网络通信的要素
- 通信双方地址:
- 规则:网络通信的协议
- 1.4、端口
- 1.5、通信协议
- TCP UDP对比
- 1.6 TCP
- 1.7 UDP
- 发送消息
- 1.8 URL
- 通过url下载文件
- 相互发送消息
1.1、概述
目的:
- 传播交流信息、数据交换、通信
想达成的效果:
- 如何准确定位网络上的一台主机:ip:port 定位到这台机器的某个资源
- 找到了这个主机,如何传输数据
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
1.2、网络通信的要素
如何实现网络的通信?
通信双方地址:
- ip
- 端口
规则:网络通信的协议
TCP/IP参考模型:
小结:
- 网络编程中两个主要问题
- 如何准确定位到网络上的主机
- 找到之后如何进行通信
- 网络编程中的要素
- IP和端口号
- 网络通信协议 TCP,UDP
- 万物皆对象
1.3、IP
ip地址:InetAdress
- 唯一定位一台网络上计算机
- 127.0.0.1 本机 localhost
- IP地址的分类
- ipv4 / ipv6
- IPV4:127.0.0.1,4个字节组成 ,0-255
- IPV6: 128位。8个无符号整数
- 公网(互联网)-私网(局域网)
- ABCD类地址
- 域名:记忆IP问题!
public static void main(String[] args) throws UnknownHostException {
InetAddress byName = InetAddress.getByName("127.0.0.1");
System.out.println(byName);
InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress.getCanonicalHostName());//获取规范名
System.out.println(inetAddress.getAddress());//地址,是一个数组
System.out.println(inetAddress.getHostName());//获取域名
}
1.4、端口
端口表示计算机上的一个程序
- 不同的进程有不同的端口,用来区分软件
- 端口规定(0-65535)
- TCP、UDP: 同协议端口不能冲突
- 端口分类
- 公有端口 0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telnet:23
- 程序注册端口:1024~49151,分配用户或者程序
- Tomcat:8080
- Mysql:3306
- Oracle:1521
- 动态、私有:49152~65535
- cmd查看端口:
netstat -ano | findstr "3306"
-
tasklist|findstr "pid"
查看指定程序
InetSocketAddress address1 = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress address2 = new InetSocketAddress("localhost", 8080);
System.out.println(address1);
System.out.println(address2);
System.out.println(address1.getAddress());
System.out.println(address1.getHostName());//地址
System.out.println(address1.getPort());//端口
1.5、通信协议
网络通信协议:速率,传输码率,代码结构,传输控制。。。
TCP/IP协议,是一组协议
- TCP: 用户传输协议
- UPD:用户数据报协议
出名的协议:
- TCP
- IP:网络互连协议
TCP UDP对比
TCP:打电话
- 连接,稳定
- 三次握手,四次挥手
- 客户端、服务端
- 传输完成,释放连接,效率低
UDP:发短信
- 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
1.6 TCP
客户端
- 空囊性服务器Socket
- 发送信息
服务器
- 建立服务的端口
- 等待用户的连接 accept
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket =null;
OutputStream outputStream =null;
//1 要知道服务器地址
try {
InetAddress ip = InetAddress.getByName("127.0.0.1");
//2 端口号
int port = 9999;
//3 创建一个socket连接
socket = new Socket(ip,port);
//4 发送消息
outputStream = socket.getOutputStream();
outputStream.write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//服务器
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket =null;
InputStream inputStream =null;
ByteArrayOutputStream bos = null;
//1 要有一个地址
try {
serverSocket = new ServerSocket(9999);
//2 等待客户端连接过来
socket = serverSocket.accept();
//3 读取客户端的消息
inputStream = socket.getInputStream();
//管道流
bos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len;
while ((len = inputStream.read(buff))!=-1){
bos.write(buff,0,len);
}
System.out.println(buff.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if (bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream!=null){
try {
inputStream.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();
}
}
}
}
}
1.7 UDP
发短信: 只管发送
发送消息
public class UdpClientDemo1 {
//发送端
public static void main(String[] args) throws Exception {
//建一个Socket
DatagramSocket socket = new DatagramSocket();
//建一个包
String msg = "东方飒森奥";
//发送给谁
InetAddress address = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes("UTF-8"),0,msg.getBytes().length,address,port);
//发送包
socket.send(datagramPacket);
socket.close();
}
}
public class UdpserverDemo1 {
//接收端
public static void main(String[] args) throws IOException {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包 DataPacket
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
byte[] data = packet.getData();
System.out.println(new String(data,0, packet.getLength(),"UTF-8"));
socket.close();
}
}
1.8 URL
通过url下载文件
public static void main(String[] args) throws Exception {
//设置连接
URL url = new URL("https://www.xxx.com/xxx.gif");
//连接url
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//下载
InputStream inputStream = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("xxx.gif"));
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
connection.disconnect();
}
相互发送消息
//发送
public class RunSender implements Runnable{
DatagramSocket socket = null;
private int fromPort;
private String toHostName;
private int toPort;
public RunSender(int fromPort,int toPort,String toHostName) {
this.fromPort = fromPort;
this.toPort = toPort;
this.toHostName = toHostName;
try {
socket = new DatagramSocket (fromPort);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String data = null;
data = reader.readLine();
byte[] datas = data.getBytes("UTF-8");
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(toHostName, toPort));
socket.send(packet);
if (data.equals("bye")){
socket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//接收
public class RunReceive implements Runnable{
private int rcPort;
DatagramSocket socket = null;
public RunReceive(int rcPort) {
this.rcPort = rcPort;
try {
socket = new DatagramSocket(rcPort);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
socket.receive(packet);
byte[] data= packet.getData();
String datas = new String(data, 0, packet.getLength(), "UTF-8");
System.out.println(socket.getLocalPort()+":"+datas);
if (datas.equals("bye")){
socket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new Thread(new RunSender(7777,8888,"localhost")).start();
new Thread(new RunReceive(9999)).start();
}
public static void main(String[] args) {
new Thread(new RunSender(6666,9999,"localhost")).start();
new Thread(new RunReceive(8888)).start();
}