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


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

	/**
	 * 
	 * @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;
		}
	}

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

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

博主QQ交流群:Java技术|SpringCloud交流群 413531925

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值