use pipe to contact with each other
来源:互联网 发布:led播放软件有几种 编辑:程序博客网 时间:2024/04/29 13:38
2.由于时间比较紧迫,限制最大管道数量200
3.代码如下
package PUB;import java.io.*;
public class PipeManager
{
private static PipedInputStream[] pipedIS ;
private static PipedOutputStream[] pipedOS ;
private static int[] inchat ;
private static int maxpipe=20;
private static String[] mysessions ;
private static PipeManager instance = null;
private PipeManager()
{
mysessions = new String[maxpipe];
pipedIS = new PipedInputStream[maxpipe];
pipedOS = new PipedOutputStream[maxpipe];
inchat = new int[maxpipe];
for(int i=0;i<maxpipe;i++)
{
mysessions[i]="";
inchat[i]=0;
pipedIS[i] = new PipedInputStream();
pipedOS[i] = new PipedOutputStream();
try
{
pipedIS[i].connect(pipedOS[i]);
}
catch (IOException e)
{
System.err.println("连接失败");
}
}
System.out.println("PipeManager init");
}
/** *//**
* @return 单一实例
*/
public static PipeManager getInstance(){
if(instance==null) instance = new PipeManager();
return instance;
}
//找到管道名touser,内容mysessionid-msg
public synchronized boolean writemsg(String mysessionid,String touser,String msg)
{
if(msg.length()==0) return false;
// if(1==1) return true;
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nullget==-1)
{
//有问题
return false;
}
if(nget==-1)
{
mysessions[nullget]=mysessionid;
nget=nullget;
}
//写数据
try
{
String writestr=touser+"-"+msg;
byte[] b = writestr.getBytes("utf-8");
int len = b.length;
// 在该线程阻塞之前,有最多1024字节的数据被写入
pipedOS[nget].write( b, 0,len );
inchat[nget]++;
}
catch (IOException e)
{
System.err.println("写操作错误");
System.exit(1);
}
return true;
}
//找到管道名mysessionid,读出内容fromuser-msg
public synchronized String readmsg(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return "";
if( inchat[nget]>0) inchat[nget]--;
else return "";
//读取数据
byte[] inArray = new byte[1000];
int bytesRead = 0;
try
{
int canread = pipedIS[nget].available();
bytesRead = pipedIS[nget].read(inArray, 0, canread);
System.out.println("已经读取" + bytesRead + "字节...");
if(bytesRead>0)
{
return new String(inArray,"utf-8");
}
}
catch (IOException e)
{
System.err.println("读取输入错误.");
System.exit(1);
}
return "";
}
//删除管道,找到对应管道对,close,并=null
public boolean removepipe(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return false;
readmsg(mysessionid);
mysessions[nget]="";
return true;
}
public int getinchat(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return 0;
if( inchat[nget]>0) return inchat[nget];
else return 0;
}
}
- use pipe to contact with each other
- udp_socket with thread to contect with each other
- Use pthread_exit() To Terminate main() With The Other Threads Alive
- abercrombie online pipe Ming Fu also use this approach to fool other people
- 【转载】Allowing applications to play nice(r) with each other: Handling remote control buttons
- setup cluster -- ssh to each other with no key ssh免密钥登陆配置
- master communication with slave each other
- 第一单元 Getting to Know Each Other
- How to create aligned partitions in Linux for use with NetApp LUNs, VMDKs, VHDs and other virtual di
- We need to let each other go now
- An error caused by symbolic links pointing to each other
- Sort String Array Make All Anagrams Next To each Other
- If we can only encounter each other rather than stay with each other, then I wish we had never encou
- LNK4098:defaultlib "×××" conflicts with use of other libs
- default lib conflicts with use of other libs
- help each other
- Cascading Each Pipe 使用
- How to use Ruijie to contact to the Internet on Ubuntu
- myOpenChord 界面数据后台线程获取,动态显示实现
- 关掉浏览器时清除session
- SQL SERVER性能优化综述
- 在深圳
- SQL SERVER性能优化综述(转)
- use pipe to contact with each other
- ASP.NET From验证
- Inline内联节点(调用节点型组节点)
- 在页面中使用updatePanel和freeTextBox时,当页面刷子新后,ftb不能输入内容的解决办法
- 大学学习和生活的规划建议
- php error when download files
- How to get new record's id
- SQL SERVER性能优化综述
- 完成端口的BUG?