马士兵学习笔记-Java基础网络编程

 TCP:可靠的

建立连接:三次握手

第一次:客户端向服务端发送小

第二次:服务端向客户端确认收到消息

第三次:客户端向服务端发送消息确定知道服务端收到客户端发送的消息


释放连接:四次挥手

第一次挥手:客户端向服务端发送消息请求断开

第二次挥手:服务端向客户端发送已经收到请求断开消息

第三次挥手:服务段再次向客户端发送已经收到请求断开消息

第四次挥手:客户端向服务发送断开消息

UDP:不可靠

 直接发包,不确认

InetAddress,InetSocketAddress获取IP

 public static void main(String[] args) throws UnknownHostException {
        InetAddress inetAddress0 = InetAddress.getByName("192.168.31.1");//封装ip
        System.out.println(inetAddress0);
        InetAddress inetAddress1 = InetAddress.getByName("localhost");//封装本机ip地址
        System.out.println(inetAddress1);
        InetAddress inetAddress2 = InetAddress.getByName("DESKTOP-S6L70L0");//封装计算机名
        System.out.println(inetAddress2);
        InetAddress inetAddress3 = InetAddress.getByName("www.baidu.com");//封装域名
        System.out.println(inetAddress3);
}
/192.168.31.1
localhost/127.0.0.1
DESKTOP-S6L70L0/192.168.31.188
www.baidu.com/182.61.200.7

套接字

V1 :客户端单向通信到服务端

服务端

public static void main(String[] args) throws IOException {
        // 1、创建套接字:指定服务器端口号
        ServerSocket serverSocket = new ServerSocket(8888);
        //2、等待客户端发来的消息
        Socket socket = serverSocket.accept();
        //接收到client的流
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        //3、接收客户端发来的数据
        String str = dataInputStream.readUTF();
        System.out.println("客户端发来信息 :"+str);
        //4、关闭流,关闭网络资源
        dataInputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }

客户端

  public static void main(String[] args) throws IOException {
        //1、创建套接字客户端,指定服务器的ip和端口
        Socket socket = new Socket("127.0.0.1", 8888);
        //2利用输出流发送数据
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeUTF("hello");
        //关闭流,关闭网络资源
        dataOutputStream.close();
        outputStream.close();
        socket.close();
    }

V2 :客户端、服务端双向通信

服务端

  public static void main(String[] args) throws IOException {
        // 1、创建套接字:指定服务器端口号
        ServerSocket serverSocket = new ServerSocket(8888);
        //2、等待客户端发来的消息
        Socket socket = serverSocket.accept();
        //接收到client的流
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        //3、接收客户端发来的数据
        String str = dataInputStream.readUTF();
        System.out.println("客户端发来信息 :"+str);
        //4、向客户端发送一个信息
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeUTF("response");
        
        // 关闭流,关闭资源
        dataOutputStream.close();
        outputStream.close();
        dataInputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
public static void main(String[] args) throws IOException {
        //1、创建套接字客户端,指定服务器的ip和端口
        Socket socket = new Socket("127.0.0.1", 8888);
        //2利用输出流发送数据
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeUTF("request");
        //接收服务端的数据
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        String response = dataInputStream.readUTF();
        System.out.println("服务端响应:"+response);

        //关闭流,关闭网络资源
        dataInputStream.close();
        inputStream.close();
        dataOutputStream.close();
        outputStream.close();
        socket.close();
    }

客户端

  public static void main(String[] args) throws IOException {
        // 1、创建套接字:指定服务器端口号
        ServerSocket serverSocket = new ServerSocket(8888);
        //2、等待客户端发来的消息
        Socket socket = serverSocket.accept();
        //接收到client的流
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        //3、接收客户端发来的数据
        String str = dataInputStream.readUTF();
        System.out.println("客户端发来信息 :"+str);
        //4、向客户端发送一个信息
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeUTF("response");
        
        // 关闭流,关闭资源
        dataOutputStream.close();
        outputStream.close();
        dataInputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }

V3:客户端向服务发送对象

服务端

public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 1、创建套接字:指定服务器端口号
        ServerSocket serverSocket = new ServerSocket(8888);
        //2、等待客户端发来的消息
        Socket socket = serverSocket.accept();
        //接收到client的流
        InputStream inputStream = socket.getInputStream();
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        Map map = (Map) objectInputStream.readObject();
        //3、接收客户端发来的数据
        System.out.print("接收到客户端发来的年级:"+map.get("grade")+",班级:"+map.get("class"));

        //4、向客户端发送一个信息
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeUTF("response");

        // 关闭流,关闭资源
        dataOutputStream.close();
        outputStream.close();
        objectInputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }

客户端

 public static void main(String[] args) throws IOException {
        //1、创建套接字客户端,指定服务器的ip和端口
        Socket socket = new Socket("127.0.0.1", 8888);
        //2利用输出流向服务端发送对象
        Map<String ,Object> map = new HashMap<>();
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你的年级:");
        String grade = scanner.next();
        System.out.print("请输入你的班级:");
        String clazz = scanner.next();
        map.put("grade",grade);
        map.put("class",clazz);

        OutputStream outputStream = socket.getOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(map);
        //接收服务端的数据
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        String response = dataInputStream.readUTF();
        System.out.println("服务端响应:"+response);

        //关闭流,关闭网络资源
        dataInputStream.close();
        inputStream.close();
        objectOutputStream.close();
        outputStream.close();
        socket.close();
    }

V4:处理异常

服务端

 public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ObjectInputStream objectInputStream = null;
        OutputStream outputStream = null;
        DataOutputStream dataOutputStream = null;
        try{

            // 1、创建套接字:指定服务器端口号
            serverSocket = new ServerSocket(8888);
            //2、等待客户端发来的消息
            socket = serverSocket.accept();
            //接收到client的流
            inputStream = socket.getInputStream();
            objectInputStream = new ObjectInputStream(inputStream);
            Map map = (Map) objectInputStream.readObject();
            //3、接收客户端发来的数据
            System.out.print("接收到客户端发来的年级:"+map.get("grade")+",班级:"+map.get("class"));

            //4、向客户端发送一个信息
            outputStream = socket.getOutputStream();
            dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeUTF("response");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭流,关闭资源
            try{
                if (dataOutputStream!=null) {
                    dataOutputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(outputStream!=null){
                    outputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if (objectInputStream!=null) {
                    objectInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(inputStream!=null) {
                    inputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(socket!=null){
                    socket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if (serverSocket!=null) {
                    serverSocket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

 客户端

public static void main(String[] args) throws IOException {
        Socket socket = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        ObjectOutputStream objectOutputStream = null;
        DataInputStream dataInputStream = null;
        try{
            //1、创建套接字客户端,指定服务器的ip和端口
            socket = new Socket("127.0.0.1", 8888);
            //2利用输出流向服务端发送对象
            Map<String ,Object> map = new HashMap<>();
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入你的年级:");
            String grade = scanner.next();
            System.out.print("请输入你的班级:");
            String clazz = scanner.next();
            map.put("grade",grade);
            map.put("class",clazz);

            outputStream = socket.getOutputStream();
            objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(map);
            //接收服务端的数据
            inputStream = socket.getInputStream();
            dataInputStream = new DataInputStream(inputStream);
            String response = dataInputStream.readUTF();
            System.out.println("服务端响应:"+response);

        }catch (Exception e){

        }finally {
            //关闭流,关闭网络资源
            try {
                if (dataInputStream!=null) {
                    dataInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                if (inputStream!=null) {
                    inputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                if(objectOutputStream!=null){
                    objectOutputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            try {
                if (outputStream!=null) {
                    outputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            try {
                if (socket!=null) {
                    socket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }

V5:服务端多线程处理多个客户端

服务端

public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ObjectInputStream objectInputStream = null;
        OutputStream outputStream = null;
        DataOutputStream dataOutputStream = null;
        try{

            // 1、创建套接字:指定服务器端口号
            serverSocket = new ServerSocket(8888);
            //2、等待客户端发来的消息
            int count = 0;
            while(true){
                socket = serverSocket.accept();
                new ServerThread(socket).start();
                count++;
                System.out.println("当前是第"+count+"个人访问,IP是"+socket.getInetAddress());
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭流,关闭资源
            try{
                if (dataOutputStream!=null) {
                    dataOutputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(outputStream!=null){
                    outputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if (objectInputStream!=null) {
                    objectInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(inputStream!=null) {
                    inputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(socket!=null){
                    socket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if (serverSocket!=null) {
                    serverSocket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }


public class ServerThread extends Thread{
    InputStream inputStream = null;
    ObjectInputStream objectInputStream = null;
    OutputStream outputStream = null;
    DataOutputStream dataOutputStream = null;
    Socket socket = null;

    public ServerThread(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //接收到client的流
            inputStream = socket.getInputStream();
            objectInputStream = new ObjectInputStream(inputStream);
            Map map = (Map) objectInputStream.readObject();
            //接收客户端发来的数据
            System.out.println("接收到客户端发来的年级:"+map.get("grade")+",班级:"+map.get("class"));

            //向客户端发送一个信息
            outputStream = socket.getOutputStream();
            dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeUTF("response");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭流,关闭资源
            try{
                if (dataOutputStream!=null) {
                    dataOutputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(outputStream!=null){
                    outputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if (objectInputStream!=null) {
                    objectInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                if(inputStream!=null) {
                    inputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值