JUDS Java的Unix Socket开发包开源项目

我要开发同款
匿名用户2011年03月03日
43阅读
开发技术Java
所属分类程序开发、网络工具包
授权协议LGPL

作品详情

JavaUnixDomainSockets(JUDS)提供了Java的方法用来访问Unixdomainsockets套接字。

示例代码:

packagecom.google.code.juds.test;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importcom.google.code.juds.*;publicclassTestUnixDomainSocket{publicstaticvoidmain(String[]args)throwsIOException{if(args.length!=1){System.out.println("usage:javaTestUnixDomainSocketsocketfilename");System.exit(1);}StringsocketFile=args[0];byte[]b=newbyte[128];//Testcase1.1:TestUnixDomainSocketClientwithastreamsocketUnixDomainSocketClientsocket=newUnixDomainSocketClient(socketFile,UnixDomainSocket.SOCK_STREAM);InputStreamin=socket.getInputStream();OutputStreamout=socket.getOutputStream();in.read(b);System.out.println("Textreceived:\""+newString(b)+"\"");Stringtext="[2]Hello!I'mtheclient!";out.write(text.getBytes());System.out.println("Textsent:"+"\""+text+"\"");socket.close();//Testcase1.2:TestUnixDomainSocketClientwithadatagramsocketsocket=newUnixDomainSocketClient(socketFile,UnixDomainSocket.SOCK_DGRAM);System.out.println("Provokeandcatchan"+"UnsupportedOperationException:");try{in=socket.getInputStream();}catch(UnsupportedOperationExceptione){System.out.println("UnsupportedOperationExceptionhasbeen"+"thrownasexpected.");}out=socket.getOutputStream();text="[3]Hello!I'mtheclient!";out.write(text.getBytes());System.out.println("Textsent:\""+text+"\"");socket.close();//Testcase2.1:TestUnixDomainSocketServerwithastreamsocketSystem.out.println("\nTest#2:TestUnixDomainSocketServer\nTestcase"+"2.1:TestUnixDomainSocketServerwithastreamsocket...");UnixDomainSocketServerssocket=newUnixDomainSocketServer(socketFile,UnixDomainSocket.SOCK_STREAM);in=ssocket.getInputStream();out=ssocket.getOutputStream();in.read(b);System.out.println("Textreceived:\""+newString(b)+"\"");text="[5]Hello!I'mtheserver!";out.write(text.getBytes());System.out.println("Textsent:"+"\""+text+"\"");ssocket.close();ssocket.unlink();//Testcase2.2:TestUnixDomainSocketServerwithadatagramsocketSystem.out.println("Testcase2.2:TestUnixDomainSocketServerwith"+"adatagramsocket...");ssocket=newUnixDomainSocketServer(socketFile,UnixDomainSocket.SOCK_DGRAM);System.out.println("Provokeandcatchan"+"UnsupportedOperationException:");in=ssocket.getInputStream();try{out=ssocket.getOutputStream();}catch(UnsupportedOperationExceptione){System.out.println("UnsupportedOperationExceptionhasbeen"+"thrownasexpected.");}in.read(b);System.out.println("Textreceived:\""+newString(b)+"\"");ssocket.close();ssocket.unlink();}}
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论