网络编程
public class TestInetAddress {
public static void main(String[] args) throws Exception {
//查询本机的ip地址
InetAddress localhost = InetAddress.getByName("localhost");
System.out.println(localhost);
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
System.out.println("=================");
//查询网站ip地址
InetAddress name = InetAddress.getByName("www.baidu.com");
System.out.println(name);
System.out.println("=================");
//常用方法
//System.out.println(name.getAddress());
System.out.println(name.getHostAddress());//获取主机ip地址
System.out.println(name.getHostName());// 获取域名
System.out.println(name.getCanonicalHostName());//获取规范的主机ip地址
}
}
端口
端口表示计算机上的一个程序的进程。
1.一栋楼表示一个ip ,这栋楼里面的 门牌号 就是端口号。 2.不同的进程有不同的端口号!用来区分软件的。 3.端口被规定为:0-65535 4.TCP ,UDP: 每个都有 0-65535 * 2 ,单个协议下,端口号不能冲突。 5.端口分类 共有端口0-1023 HTTP : 80 HTTPS :443 FTP : 21 Telet : 23 程序注册端口:1024-49151,分配给用户或者程序 Tomcat:8080 Mysql:3306 Oracle:1521 动态、私有:49152-65535
public class TesyInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress socketAddress1 = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress);
System.out.println(socketAddress1);
System.out.println("====================");
System.out.println(socketAddress.getAddress());//ip地址
System.out.println(socketAddress.getHostName());//主机名称
System.out.println(socketAddress.getHostString());
System.out.println(socketAddress.getPort());//端口
}
}
通信协议
协议:约定,就好比我们现在说的是普通话。
网络通信协议: 1.速率 2.传输码率 3.代码结构 4.传输控制
问题:非常的复杂
TCP/IP协议簇:实际上是一组协议 重要: TCP:用户传输协议 UDP:用户数据报协议
出名的协议: TCP IP
TCP和UDP 对比:
TCP:打电话 连接: 稳定 三次握手 A:你愁啥? B:瞅你咋地? A:干一次!
四次挥手
A:我要断开了 (我要走了) B:我知道你要断开了(你真的要走了吗?) B:你真的断开了吗?(你真的真的要走了吗?) A:我真的断开了 (我真的要走了)
客户端,服务端 传输完成,释放连接、效率低
UDP:发短信 1.不连接,不稳定 2.客户端、服务端:没有明确的界限 3.不管有没有准备好,都可以发给你…
客户端和服务器
-
客户端
1.建立连接 2.发送消息
public class TcpClientDemo1 {
public static void main(String[] args) throws Exception{
Socket socket = null;
OutputStream os = null;
//要知道服务器地址
try {
InetAddress serverIp = InetAddress.getByName("localhost");
int port = 9999;
//2.创建连接
socket = new Socket(serverIp,port);
//3.发生消息 IO流
os = socket.getOutputStream();
os.write("你好".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (os != null) {
os.close();
}
if (socket != null) {
socket.close();
}
}
}
}
-
服务器
1.建立服务连接的端口 ServerSocket 2.等待用户的连接 accept 3.接收用户信息
public class TcpServerDemo01 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1. 我得有一个地址
serverSocket = new ServerSocket(9999);
//2.等待客户端连接过来
accept = serverSocket.accept();
//3.读取客户端消息
is = accept.getInputStream();
/*byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1 ){
String s = new String(buf, 0, len);
System.out.println(s);
}*/
//管道流
baos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = -1;
while ((len = is.read(buff)) != -1) {
baos.write(buff, 0, len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (baos != null) {
baos.close();
}
if (is != null) {
is.close();
}
if (accept != null) {
accept.close();
}
if (serverSocket != null) {
serverSocket.close();
}
}
}
}
TCP实现文件上传
-
服务端
public class TcpServerDemo2 {
public static void main(String[] args) throws Exception{
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9999);
//2.监听客户端连接
Socket accept = serverSocket.accept();
//3.获取输入流
InputStream is = accept.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));//接收文件就要用文件的管道流
byte[] buff = new byte[1024];
int len;
while ((len = is.read(buff)) != -1){
fos.write(buff,0,len);
}
//通过客户端我接收完毕了
OutputStream os = accept.getOutputStream();
os.write("我接收完毕了,你可以断开了".getBytes());
fos.close();
is.close();
accept.close();
serverSocket.close();
}
}
-
客户端
public class TcpClientDemo2 {
public static void main(String[] args) throws Exception {
//1.建立连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream is = new FileInputStream(new File("1.jpg"));
byte[] buff = new byte[1024];
int len;
//4.写出文件
while ((len = is.read(buff)) != -1) {
os.write(buff, 0, len);
}
//通知服务器,我已经结束了
socket.shutdownOutput();//我已经传输完了的意思
//确定服务器接收完毕,才能够断开连接
InputStream inputStream = socket.getInputStream();//接收字符、就用字节的管道流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buff2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(buff)) != -1) {
bos.write(buff2, 0, len2);
}
System.out.println(bos.toString());
//5.释放资源
bos.close();
inputStream.close();
is.close();
os.close();
socket.close();
}
}
UDP
发短信,需要IP地址
-
发送消息
public class UdpClientDemo1 {
public static void main(String[] args) throws Exception{
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建个包
String msg = "你好啊,服务器";
//3.发送给谁
InetAddress address = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, address, port);
//4.发送包
socket.send(packet);
}
}
-
接收端
public class UdpServerDemo1 {
public static void main(String[] args) throws Exception{
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buff =new byte[1024];
DatagramPacket packet = new DatagramPacket(buff, 0, buff.length);
socket.receive(packet);//阻塞接收
System.out.println(packet.getAddress());
System.out.println(new String(packet.getData(),0,packet.getData().length));
socket.close();
}
}
实现聊天
-
发送方
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
//获取连接
DatagramSocket socket = new DatagramSocket(8080);
while (true) {
//准备数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket
(datas, 0,datas.length, new InetSocketAddress("localhost", 6666));
//发送数据
socket.send(packet);
if (data.equals("bye")) {
break;
}
}
socket.close();
}
}
-
接收方
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
//准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接收包裹
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(receiveData);
//断开连接 bye
if (receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
URL下载网络资源