TCP/IP和UDP简单使用

TCP/IP Sorcket流的输出流和输入流

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;

public class SocketConnect {

    private Socket socketConnect;
    private OutputStream outputStream;
    private InputStream inputStream;
    private BlockingQueue PacketQueue = new BlockingQueue(128);
    private byte[] RecData = new byte[2048];

    private SocketService mySocketService;

    // 构造连接
    public SocketConnect(Socket socketConnect, SocketService mySocketService) {

        try {
            this.mySocketService = mySocketService;
            this.socketConnect = socketConnect;
            InetAddress inetAddress = socketConnect.getInetAddress();
            System.out.println("连接Socket的IP地址:" + inetAddress);
            // 设置缓冲区
            socketConnect.setReceiveBufferSize(SysUtil.RECV_BUF_SIZE);
            // GET输入流
            outputStream = socketConnect.getOutputStream();
            // GET输出流
            inputStream = socketConnect.getInputStream();
            // 启动线程池
            StartThread();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @SEname 宋伟
     */

    public void StartThread() {
        // 输出流线程
        SysUtil.ThreadPools.submit(new ThreadReadCabinetMsgs());
        // 启动队列线程
        SysUtil.ThreadPools.submit(new ThreadParseCabinetMsgs(mySocketService));
    }

    /**
     * @SEname 宋伟
     * @method 关闭连接
     * @throws IOException
     */
    public void CloseSocket() throws IOException {

        if (outputStream != null) {
            outputStream.close();
        }

        if (inputStream != null) {
            inputStream.close();
        }

        if (socketConnect != null) {
            if (!socketConnect.isClosed())
                socketConnect.close();
            socketConnect = null;
        }
    }

    /**
     * @SEname 宋伟
     */
    public void SendPacket(byte[] bs) throws IOException {
        if (outputStream != null) {
            InetAddress inetAddress = socketConnect.getInetAddress();
            outputStream.write(bs);
        } else {
            outputStream = socketConnect.getOutputStream();
        }
    }

    /**
     * @SEname 宋伟
     */

    public class ThreadReadCabinetMsgs implements Runnable {

        public int length;

        public void run() {
            while (true) {
                if (inputStream != null) {
                    try {
                        InetAddress inetAddress = socketConnect.getInetAddress();
                        // inputStream流的read方法自带阻塞,阻塞的时候返回-1
                        length = inputStream.read(RecData);
                        if (length > 0)
                            PacketQueue.enqueue(Arrays.copyOf(RecData, length));
                    } catch (IOException e) {
                        e.printStackTrace();
                        continue;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        inputStream = socketConnect.getInputStream();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_20197983/article/details/81487728