java网络编程


package net;

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);
            InetAddress inetAddress2=InetAddress.getByName("localhost");
            System.out.println(inetAddress2);
            InetAddress inetAddress3=InetAddress.getLocalHost();
            System.out.println(inetAddress3);

            InetAddress inetAddress4=InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress4);

            System.out.println(inetAddress4.getAddress());
            System.out.println(inetAddress4.getCanonicalHostName());
            System.out.println(inetAddress4.getHostAddress());
            System.out.println(inetAddress4.getHostName());



        } catch (UnknownHostException e) {
            e.printStackTrace();
        }


    }
}

端口

表示计算机上的一个程序的进程

  • 不同的进程有不同的端口号,用来区分软件;
  • 被规定0~65536
  • TCP/UDP:单个协议下,端口号不能冲突
  • 端口分类: 
    •         公有端口:0~1023
      •         HTTP端口:80
      •         HTTPS:43
      •         FTP:21
      •         Telent:23
    • 程序注册端口:1024~49151,分配给用户或者程序
      • Tomcat:8080
      • MySQL:3306
      • Oracle:1521
    • 动态、私有端口:49152~65525
  1. netstat -ano        #查看所有端口
  2. netstat -ano|findstr "5900"        #查看指定的端口
  3. tasklist|findstr ""        #查看指定端口的进程


package net;

import java.net.InetAddress;
import java.net.InetSocketAddress;

public class TestInetSocketAddress {
    private static InetAddress address;

    public static void main(String[] args) {
        InetSocketAddress inetSocketAddress=new InetSocketAddress("127.0.0.1",8080);

        InetSocketAddress inetSocketAddress2=new InetSocketAddress("localhost",8080);

        System.out.println(inetSocketAddress);
        System.out.println(inetSocketAddress2);

        System.out.println(inetSocketAddress.getAddress());
        System.out.println(inetSocketAddress.getHostName());
        System.out.println(inetSocketAddress.getPort());



    }
}

协议

TCP/IP协议

  • TCP:用户传输协议 —— IP:网络互联协议  两者是同一个协议
  • UDP:用户数据报协议

TCP与UDP对比:

TCP:打电话

  • 连接稳定
  • 三次握手,四次挥手:最少需要三次,才能保证稳定连接
  1. A:你瞅啥?
  2. B:瞅你咋地?
  3. A:干一场!

  1. A:我准备断开了
  2. B:你真的要断开了吗?
  3. B:你真的真的要断开了吗?
  4. A:我真的要断开了
  • 客户端、服务端
  • 传输完成就释放连接,效率低

UDP:发短信

  • 连接不稳定
  • 客户端、服务端:没有明确的界限
  • 不管有没有准备好,都可以发送


package net;


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) {
        Socket socket=null;
        OutputStream os=null;
        try {
            //1、要知道服务器的地址
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port=9999;
            //2、创建一个socket连接
            socket=new Socket(serverIP,port);
            3、发送消息IO流
            os=socket.getOutputStream();
            os.write("hi,welcome!".getBytes());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            if (socket != null) {
                try{
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try{
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


package net;

import java.io.ByteArrayOutputStream;
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;
        ByteArrayOutputStream baos =null;
        InputStream is=null;
        try {
            //1、我先要有一个端口
            serverSocket=new ServerSocket(9999);
            //2、等待客户端连接过来
            socket=serverSocket.accept();
            //3、读取客户端的消息
            is=socket.getInputStream();

            baos = new ByteArrayOutputStream();

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

//            byte[] buffer = new byte[1024];
//            int len;
//            while((len=is.read(buffer))!=-1){
//                String msg=new String(buffer,0,len);
//                System.out.println(msg);
//            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            if (baos != null) {
                try{
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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();
                }
            }

        }
    }
}

TCP:

客户端

1、连接服务器Socket

2、发送消息

服务器

1、建立服务的端口ServerSocket

2、等待用户的连接accept

3、接收用户的消息

文件上传


package net;

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

public class TcpClientDemo02 {
    public static void main(String[] args) throws IOException {
        try(
                Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
                OutputStream os=socket.getOutputStream();
                FileInputStream fis = new FileInputStream("E:\\code\\java\\javaSE\\基础语法\\src\\net\\extuiHL.png");
                )
        {
            byte[] buffer = new byte[1024];
            int len;
            while((len=fis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        }
    }
}


package net;

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

public class TcpServerDemo02 {
    public static void main(String[] args) throws IOException {
        try(
            ServerSocket serverSocket = new ServerSocket(9000);
            Socket socket=serverSocket.accept();
            InputStream is=socket.getInputStream();
            FileOutputStream fos=new FileOutputStream(new File("receive"));
        ) {
            byte[] buffer=new byte[1024];
            int len;
            while((len=is.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值