java网络编程之TCP/IP连接

1. 百度知识科普:
TCP/IP是为了在互联网实现通信传输,以及为了能够实现在互联网上共享彼此资源所规定的一种通信协议。
TCP/IP 通信协议是对计算机必须遵守的规则的描述,只有遵守这些规则,计算机之间才能进行通信。就比如生活中对于 “一斤是500克,而不是800克” 的重量计数单位约定,是为了方便彼此而建立的一种约定。

从上图TCP/IP四层模型中可以看到,TCP是位于传输层,IP则在于网络层。
而客户端要实现与服务器端进行通信连接,则需要引申出TCP的三次握手。其实还有改进后的TCP四次挥手(分手),就像TCP/IP四层模型是从OSI七层网络模型中改良进来的。

--

也可以这样理解为是两个人见面时的交流:

  A:你瞅啥
  B:瞅你咋滴
  A:来,咋俩来唠唠

TCP三次握手:是客户端与服务器端进行的三次互相确认通信,是为了确定对方收到了进行连接的信号,才进行彼此的通信。
只有当双方两个人每次都听到了对方的话,并给出了回应,通信连接才会成功建立。如果当B听到A说的 你瞅啥 ,但不理会,则连接也不会建立。

下面为模仿服务器与客户端进行通信的程序代码:

2. 模仿服务器端程序代码:

package com.kuang.lesson02;

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;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1、首先得有一个地址
            serverSocket = new ServerSocket(9999);

            //只要符合条件就一直接收从客户端传过来的信息
            while (true){
                //2、等待客户端的连接过来
                socket = serverSocket.accept();
                //3、读取客户端信息
                is = socket.getInputStream();

            /*
            //一般写法,但如果传过来的信息超过处理范围,则会有隐患
            byte[] buffer = new byte[1024];
            int len;//长度
            while ((len=is.read(buffer)) != -1){
                String msg = new String(buffer,0,len);
                System.out.println(msg);
            }
            */

                //管道流
                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());
            }

        } catch (Exception 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();
                }
            }
        }
    }  }

3.模仿客户端程序代码:

package com.kuang.lesson02;

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

//客户端
public class TcpClineDemo01 {
    public static void main(String[] args) {

        Socket socket = null;
        OutputStream os = null;

        try {
            //1、知道服务器ip地址和端口号
            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("2021/5/4/19:59分学习狂神的网络编程".getBytes());

            os.close();
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }  }

点击此链接跳转知网上关于TCP/IP的文章链接

本文作者:飞飞吻

本文链接:https://www.cnblogs.com/flyingkisses/p/14774784.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   飞飞吻  阅读(835)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起