资源简介

1. 满足具有Socket客户端需求的基本应用. 2. 满足具有Socket服务端的基本应用. 具备并发能力, 能满足可设定个数客户端连接. 参考个人博客: http://blog.csdn.net/ostrichmyself/article/details/6618349

资源截图

代码片段和文件信息

package com.ostrichmyself.util.socket;

import java.io.InputStream;

/**
 * IO的相关处理 这里面提供的都是静态类 满足IO处理
 * @author tiant5
 *
 */
public class IOUTIL {


public static byte[] inputStream2bytes(InputStream in) throws Exception
{
byte[] bufIn = new byte[BUFFER_SIZE];
int bytesLen = in.available();
int totalCount = 0;
//注意 要设置Socket超时. 不然会死死等待
while( bytesLen > 0)
{

try
{
bytesLen = in.read(bufIn totalCount bytesLen);
totalCount += bytesLen;
bytesLen = in.available();
}catch(Exception e)
{
//超时抛出异常 这样可以中断read阻塞
break;
}
}

byte[] stores = new byte[totalCount];
System.arraycopy(bufIn 0 stores 0 totalCount);
return stores;
}
// 设置一次接收数据的大小 如果不设置默认为1M 后续最好不要修改
private static int BUFFER_SIZE = 1024 * 1024;

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2011-07-28 22:22  XXXX\
     目录           0  2011-07-28 22:22  XXXX\src\
     目录           0  2011-07-28 22:22  XXXX\src\com\
     目录           0  2011-07-28 22:22  XXXX\src\com\ostrichmyself\
     目录           0  2011-07-28 22:22  XXXX\src\com\ostrichmyself\util\
     目录           0  2011-07-28 22:22  XXXX\src\com\ostrichmyself\util\socket\
     文件         907  2011-07-28 22:22  XXXX\src\com\ostrichmyself\util\socket\IOUTIL.java
     文件        4680  2011-07-28 22:22  XXXX\src\com\ostrichmyself\util\socket\SanySocketClient.java
     文件        2276  2011-07-28 22:22  XXXX\src\com\ostrichmyself\util\socket\SanySocketServer.java
     文件         947  2011-07-28 22:22  XXXX\src\com\ostrichmyself\util\socket\SingleTask.java
     目录           0  2011-07-28 22:22  XXXX\usage\
     目录           0  2011-07-28 22:22  XXXX\usage\com\
     目录           0  2011-07-28 22:22  XXXX\usage\com\ostrichmyself\
     目录           0  2011-07-28 22:22  XXXX\usage\com\ostrichmyself\socket\
     目录           0  2011-07-28 22:22  XXXX\usage\com\ostrichmyself\socket\client\
     文件        1517  2011-07-28 22:22  XXXX\usage\com\ostrichmyself\socket\client\SanySocketClientTest.java
     目录           0  2011-07-28 22:22  XXXX\usage\com\ostrichmyself\socket\server\
     文件         332  2011-07-28 22:22  XXXX\usage\com\ostrichmyself\socket\server\SanySocketServerTest.java
     文件        1450  2011-07-28 22:22  XXXX\usage\com\ostrichmyself\socket\server\SimpleTask.java

评论

共有 条评论