TCP/IP编程

2018-03-01  本文已影响0人  Charein

TCP/IP参考模型

TCP/IP参考模型是首先由ARPANET所使用的网络体系结构。这个体系结构在它的两个主要协议出现以后被称为TCP/IP参考模型(TCP/IP Reference Model)。这一网络协议共分为四层:网络访问层、互联网层、传输层和应用层。如下图:


14ce36d3d539b6005574d31eea50352ac75cb7e5.jpg

TCP简介

TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793定义。在简化的计算机网络OSI模型中,它完成第四层传输层所指定的功能,用户数据报协议(UDP)是同一层内 另一个重要的传输协议。在因特网协议族(Internet protocol suite)中,TCP层是位于IP层之上,应用层之下的中间层。不同主机的应用层之间经常需要可靠的、像管道一样的连接,但是IP层不提供这样的流机制,而是提供不可靠的包交换。

基于Socket的TCP编程

TCP通信中包含服务端和客户端,下面分别讲述。

服务端

第一步 创建ServerSocket实例;
第二步 调用ServerSocket的accept()方法,等待客户端连接;
第三步 客户端连接上服务端之后,数据读写。

下面贴上示例代码:

public class Server extends Base {
    private ServerSocket mServerSocket;
    private Socket mSocket;

    public Server(BaseListener listener) {
        super(listener);
    }

    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mServerSocket = new ServerSocket(8090);

                    mSocket = mServerSocket.accept();

                    mHandler.sendMsgConnected();

                    recv();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void stop() {
        if (mServerSocket != null) {
            try {
                mServerSocket.close();
                mServerSocket = null;
                mHandler.sendMsgDisconnected();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void recv() {
        if (mSocket == null) {
            return;
        }

        try {
            InputStream is = mSocket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            int i = 0;
            String content;
            while ((content = reader.readLine()) != null) {
                mHandler.sendMsgRecv(content);

                send("hello world:" + (i++));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean send(@NonNull String content) {
        if (mSocket == null || !mSocket.isConnected()) {
            return false;
        }

        try {
            OutputStream os = mSocket.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
            writer.write(content + "\n");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
客户端

第一步 创建Socket实例;
第二步 连接上服务端之后,读写数据。

下面贴上示例代码:

public class Client extends Base {
    private Socket mSocket;

    public Client(BaseListener listener) {
        super(listener);
    }

    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (mSocket != null) {
                    try {
                        mSocket.close();
                        mHandler.sendMsgDisconnected();
                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                }

                try {
                    mSocket = new Socket("127.0.0.1", 8090);

                    mHandler.sendMsgConnected();

                    read();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void stop() {
        if (mSocket != null) {
            try {
                mSocket.close();
                mSocket = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void read() {
        if (mSocket == null || !mSocket.isConnected()) {
            return;
        }

        try {
            InputStream is = mSocket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            String content;
            while ((content = reader.readLine()) != null) {
                mHandler.sendMsgRecv(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean send(@NonNull String content) {
        if (mSocket == null || !mSocket.isConnected()) {
            return false;
        }

        try {
            OutputStream os = mSocket.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
            writer.write(content + "\n");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
上一篇下一篇

猜你喜欢

热点阅读