计算机网络
就是将地理位置不同的具有独立功能的多台计算及外部设备,通过通信线路连接起来,在网络操作系统、网络管理软件以及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统
目的
传播交流信息、数据交换、通信
如何做到
1.如何准确定位网络上的一台主机 192.xxx.xx.xx:端口,定位到这个计算机上的某个资源
2.找到主机,如何传输数据
javaweb:网页编程 b/s
网络编程:tcp/ip c/s
网络通信的要素
如何实现网络的通信
1.ip、端口号
2.协议 tcp/ip协议
3.万物皆对象
IP
ip地址:InetAdress
唯一定位一台网络上的计算机
127.0.0.1:本机Locallost
IP地址分类
ipv4/ipv6
ipv4 127.0.0.1 4个字节 0~255 42亿 30亿在北美,4亿在亚洲,2011年用尽
ipv6:128 8个无符号整数
公网(互联网)/私网(局域网)
ABCD类地址
192.168.xx.xx 专门给组织内部使用的
域名:记忆ip问题
package com.xgglr;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class IPDemo {
public static void main(String[] args) {
try {
InetAddress[] inetAddresses = InetAddress.getAllByName("127.0.0.1");
System.out.println(Arrays.toString(inetAddresses));
InetAddress[] localHosts = InetAddress.getAllByName("localhost");
System.out.println(Arrays.toString(localHosts));
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
InetAddress[] baiduName = InetAddress.getAllByName("www.baidu.com");
System.out.println(Arrays.toString(baiduName));
for (InetAddress address : baiduName) {
String canonicalHostName = address.getCanonicalHostName();//规范名称
String hostName = address.getHostName();//域名
String hostAddress = address.getHostAddress();//IP
System.out.println(canonicalHostName);
System.out.println(hostName);
System.out.println(hostAddress);
System.out.println("============");
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口
表示计算机上的一个程序的进程,不同的进程端口号不同,区分如软件
被规定0~65535
tcp/udp 6553582 tcp:80 udp:80 单个协议下,端口号不能冲突
端口分类:共有端口0-1023
http:80 https:443 ftp:21 Telent:23
程序注册端口:1024-49151,分配用户或者程序
tomcat:8080 mysql:3306 oracle:1521
动态、私有:49152-65535
netstat -ano 查看所有端口
netstat -ano|findstr “5900” 查看指定端口
tasklist|findstr “8696” 查看指定端口的进程
通信协议
网络通信协议:速率,传输码率,代码结构,传输控制
分层思想
tcp/ip协议:一组协议
tcp:用户传输协议,udp:用户数据报协议
出名的协议:tcp,ip:网络互联协议
tcp和udp对比
tcp:打电话
连接 稳定
三次握手 四次挥手
客户端 服务端
传输完程 释放连接 效率低
udp:发短信
不连接 不稳定
客户端、服务端 没有明确的界限
不能有没有准备好 都可以发送
TCP
客户端:连接服务器Socket,发送消息
package com.xgglr.tcpDemo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class tcpClintDemo {
public static void main(String[] args) {
try {
InetAddress byName = InetAddress.getByName("localhost");
int port = 8888;
Socket socket = new Socket(byName,port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("您好!!!,这里时客户端".getBytes());
outputStream.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端:建立服务的接口ServerSocket,等待用户的连接accept,接受用户的消息文章来源:https://www.toymoban.com/news/detail-837993.html
package com.xgglr.tcpDemo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
//服务端
public class tcpServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
while (true){
socket = serverSocket.accept();
inputStream = socket.getInputStream();
//管道流
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1){
byteArrayOutputStream.write(bytes,0,len);
}
System.out.println(byteArrayOutputStream.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (byteArrayOutputStream != null){
try {
byteArrayOutputStream.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();
}
}
}
}
}
文件上传
package com.xgglr.tcpDemo;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpFileDownLoaderServerDemo {
public static void main(String[] args) {
try {
while (true){
ServerSocket socket = new ServerSocket(9999);
Socket accept = socket.accept();
InputStream is = accept.getInputStream();
FileOutputStream fos = new FileOutputStream(
new File("D:\\software\\JavaCode\\javaSE\\JavaSE01\\src\\com\\xgglr\\tcpDemo\\tcp.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
//确认接收到了,可以断开
accept.getOutputStream().write("已经接收,可以关闭!!!".getBytes());
fos.close();
is.close();
accept.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.xgglr.tcpDemo;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class TcpFileDownLoaderClintDemo {
public static void main(String[] args) {
try {
//创建连接
InetAddress byName = InetAddress.getByName("localhost");
int port = 9999;
Socket socket = new Socket(byName,port);
//创建输出流
OutputStream os = socket.getOutputStream();
//读取文件
FileInputStream fileInputStream = new FileInputStream(new File("2.jpg"));
//写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1){
os.write(buffer,0,len);
}
//通知服务器发送完毕
socket.shutdownOutput();
//确认是否接收
InputStream is = socket.getInputStream();
//管道流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int lens;
while ((lens = is.read(bytes)) != -1){
baos.write(bytes,0,lens);
}
System.out.println(baos.toString());
fileInputStream.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
UDP
不用连接,需要知道地址文章来源地址https://www.toymoban.com/news/detail-837993.html
package com.xgglr.udp;
import java.io.IOException;
import java.net.*;
//UDP客户端
public class UdpClint {
public static void main(String[] args) {
//建立连接Socket
try {
DatagramSocket socket = new DatagramSocket();
//建包
String msg = "你好!!! Server";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
/**
* byte buf[], 数据
* int offset, 数据的起始长度
* int length, 结束长度
* netAddress address, 接受ip
* int port 端口号
* */
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//发送包
socket.send(packet);
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.xgglr.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Arrays;
//UDP服务端
public class UdpServer {
public static void main(String[] args) {
//开放端口
try {
DatagramSocket socket = new DatagramSocket(9090);
//接受数据
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);// 阻塞接收
System.out.println(packet.getAddress().getHostName());
System.out.println(packet.getPort());
System.out.println(packet.getLength());
// System.out.println(Arrays.toString(packet.getData()));
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
到了这里,关于网络编程、UDP、TCP的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!