概述
- 网络编程:TCP/UDP,C/S架构。网页编程:JavaWeb,B/S架构
- 智能汽车技术已经非常成熟。存在伦理问题
网络通信要素
- ip+端口号:192.168.1.1:5900
- 在C:\windows\system32内才可以ping baidu.com。或者把C:\windows\system32增加到系统变量中
IP
package com.adair.lesson1;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本地主机
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);// /127.0.0.1
InetAddress inetAddress3 = InetAddress.getByName("localhost");
System.out.println(inetAddress3);// localhost/127.0.0.1
InetAddress inetAddress4 = InetAddress.getLocalHost();
System.out.println(inetAddress4);// DESKTOP-Adair/192.168.1.2
//查询百度ip
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2); // www.baidu.com/39.156.66.14
System.out.println(inetAddress2.getAddress()); //[B@74a14482
System.out.println(inetAddress2.getCanonicalHostName());// 39.156.66.14
System.out.println(inetAddress2.getHostAddress());// 39.156.66.14
System.out.println(inetAddress2.getHostName());// www.baidu.com
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口port
- 表示一台电脑上的一个程序的进程,不同的进程有不同的端口号。0~65535
- TCP/UDP :2*65535。TCP:80 &&UDP:80,同一类型端口不可相同
- 端口分类
- 服务器端:公有端口0~1023。HTTP:80 HTTPS:443 FTP:21
- 服务器端:程序注册端口1024~49151。Tomcat8080 MySQL3306 Oracle1521
- 客户端:端口号49152~65535:暂时的动态私有,不可用
netstat -ano //查看所有端口
netstat -ano|findstr "10809" //查看指定端口,先查询再带入
tasklist|findstr "8232" //查看指定端口的进程
- C:\Windows\System32\drivers\etc下的hosts可以查看本机的配置
package com.adair.lesson1;
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress); // /127.0.0.1:8080
System.out.println(socketAddress2); // localhost/127.0.0.1:8080
System.out.println(socketAddress.getAddress());// /127.0.0.1
System.out.println(socketAddress.getHostName());// 127.0.0.1
System.out.println(socketAddress.getPort());//8080
}
}
通信协议
TCP
客户端
服务器
- 建立服务的端口ServerSocket
- 等待用户的连接accept
- 接受用户的消息
package com.adair.lesson2;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
ByteOutputStream baos = null;
InputStream is = null;
try {
//需要有个地址
serverSocket = new ServerSocket(9999);
while(true){
//等待客户端连接
socket = serverSocket.accept();
//读取客户端消息
is = socket.getInputStream();
//管道流
baos = new ByteOutputStream();
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) {
e.printStackTrace();
}finally {
if(baos != null){
baos.close();
}
if(is != null){
try {
is.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();
}
}
}
}
}
--------------------------------------
package com.adair.lesson2;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClientDemo01 {
public static void main(String[] args) {
OutputStream os = null;
Socket socket = null;
try {
//创建地址和端口
InetAddress serveIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//创建socket连接
socket = new Socket(serveIP,port);
//发送消息 IO流
os = socket.getOutputStream();
os.write("我在永远在路上".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
|