java_网络编程

27 篇文章 0 订阅

目录

1.  IP

2.端口

3.通信协议

4. tcp文件传输

5.UDP聊天

6.URL


1.  IP

        127.0.0.1 :本机localhost

        分类:

                公网/私网

                        私网:192.158.xxx.

                ipv4/ipv6:

                        ipv4:127.0.0.1 4个字节,0~255,

                        ipv6:128位,8个无符号整数

import java.net.InetAddress;

public class TestInetAddress {
    public static void main(String[] args) throws Exception{
        InetAddress inetAddress= InetAddress.getByName("127.0.0.1");
        System.out.println(inetAddress);
        //查询网站ip
        InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
        System.out.println(inetAddress2);
        //常用方法
        System.out.println(inetAddress2.getAddress());
        System.out.println(inetAddress2.getCanonicalHostName());
        System.out.println(inetAddress2.getHostName());
    }
}

2.端口

 进口号用来区分软件的

公有端口: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" 查看指定端口的进程

3.通信协议

        tcp:用户传输协议

                连接,稳定

                三次握手,四次挥手

                客户端和服务端

 

        udp:用户数据报协议

                客户端和服务端没有明确界限

                不连接,不稳定

4. tcp文件传输 

package TCP;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TCP_server {
    public static void main(String[] args)throws Exception {
        //客户端
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),3434);
        OutputStream os = socket.getOutputStream();
        FileInputStream fis = new FileInputStream("C:/Users/Administrator/Desktop/kail_ack.txt");

        byte[] buffer = new byte[1024];
        int len;
        while((len=fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }

        fis.close();
        os.close();
        socket.close();
    }
}

package TCP;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
public class TCP_socketserver {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket =new ServerSocket(3434);
        Socket socket = serverSocket.accept();
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\gao.txt");

        byte[] buffer = new byte[1024];
        int len;
        while((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}

5.UDP聊天

6.URL

        统一资源定位符:定位物联网上某一个资源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值