IP、端口、TCP、UDP、文件上传
时间:2021-06-02 15:28:12
收藏:0
阅读:0
1.ip
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机IP,可以new
InetAddress byName = InetAddress.getByName("localhost");
System.out.println(byName);
InetAddress byName1 = InetAddress.getByName("127.0.0.1");
System.out.println(byName1);
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
System.out.println("=============================================");
//查询网站ip地址
InetAddress byName2 = InetAddress.getByName("www.baidu.com");
System.out.println(byName2);
System.out.println(byName2.getHostAddress());//ip
System.out.println(byName2.getHostName());//域名或者自己电脑的名字
} catch (UnknownHostException e) {
e.printStackTrace();
} finally {
}
}
}
2.端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号!用来区分软件!
-
被规定0-65535
-
TCP,UDP:65535*2 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 "端口号" #查看指定的端口 tasklist|findstr "端口号" #查看指定端口的进程
-
public class TestInetSocketAddress {
public static void main(String[] args) {
//可以new
InetSocketAddress socketAddress = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress);
System.out.println("================================");
System.out.println(socketAddress.getAddress().getHostAddress());
System.out.println(socketAddress.getHostName());//地址,域名
System.out.println(socketAddress.getPort());//端口
}
}
3.通信协议
- TCP:用户传输协议
- UDP:用户数据报协议
TCP和UDP对比:
-
TCP:打电话
-
连接,稳定
-
三次握手 四次挥手
最少需要三次,保证稳定连接! A:你愁啥? B:抽你咋地? C:干一场! A:我要走了! B:你真的要走了吗? B:你真的真的要走了吗? A:我真的要走了!
-
客户端和服务端
-
传输完成,释放连接,效率低
-
-
UDP:发短信
- 不连接,不稳定
- 客户端和服务端:没有明确的界限
- 不管准没准备好,都可以发给你
- DDOS:洪水攻击!
4.TCP
客户端
- 连接服务器Socket
- 发送消息
服务器
- 建立服务的端口ServerSocket
- 等待用户的连接accept
- 接收用户的消息
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
try {
//1.要知道服务器地址
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
//2.端口号
int port = 9999;
//3.创建一个socket连接
Socket socket = new Socket(serverIp,port);
//4.发送消息 IO流
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你好,欢迎学习".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//服务端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket= null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1.首先要有一个地址
serverSocket = new ServerSocket(9999);
while(true) {
//2.等待客户端连接
accept = serverSocket.accept();
//3.读取客户端消息
inputStream = accept.getInputStream();
//管道流
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
if ((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 (accept!=null) {
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.文件上传
//客户端
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("1.png"));
//4.写出文件
byte[] buffer = new byte[1024];
int len;
if ((len = fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//通知服务器,已经结束
socket.shutdownOutput();//我已经传输完了!
//确认服务器接收完毕才能断开连接
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[2048];
int len2;
if ((len2 = is.read(buffer1))!=-1){
baos.write(buffer1,0,len2);
}
System.out.println(baos.toString());
//5.关闭资源
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
}
//服务端
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的连接
Socket socket = serverSocket.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("2.png"));
byte[] buffer = new byte[1024];
int len;
if ((len = is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//通知客户端已接收完毕
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了,你可以断开了".getBytes());
//5.关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
评论(0)