微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JUDS Java的Unix Socket开发包

程序名称:JUDS

授权协议: LGPL

操作系统: Linux

开发语言: Java

JUDS 介绍

Java Unix Domain Sockets (JUDS) 提供了 Java 的方法用来访问 Unix domain sockets 套接字。

示例代码

package com.google.code.juds.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.google.code.juds.*;

public class TestUnixDomainSocket {

        public static void main(String[] args) throws IOException {
                if (args.length != 1) {
                        System.out
                                        .println("usage: java TestUnixDomainSocket socketfilename");
                        System.exit(1);
                }
                String socketFile = args[0];

                byte[] b = new byte[128];
                // Testcase 1.1: Test UnixDomainSocketClient with a stream socket
                UnixDomainSocketClient socket = new UnixDomainSocketClient(socketFile,
                                UnixDomainSocket.soCK_STREAM);
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getoutputStream();
                in.read(b);
                System.out.println("Text received: \"" + new String(b) + "\"");
                String text = "[2] Hello! I'm the client!";
                out.write(text.getBytes());
                System.out.println("Text sent: " + "\"" + text + "\"");
                socket.close();

                // Testcase 1.2: Test UnixDomainSocketClient with a datagram socket
                socket = new UnixDomainSocketClient(socketFile,
                                UnixDomainSocket.soCK_DGRAM);
                System.out.println("Provoke and catch an "
                                + "UnsupportedOperationException:");
                try {
                        in = socket.getInputStream();
                } catch (UnsupportedOperationException e) {
                        System.out.println("UnsupportedOperationException has been "
                                        + "thrown as expected.");
                }
                out = socket.getoutputStream();
                text = "[3] Hello! I'm the client!";
                out.write(text.getBytes());
                System.out.println("Text sent: \"" + text + "\"");
                socket.close();

                // Testcase 2.1: Test UnixDomainSocketServer with a stream socket
                System.out.println("\nTest #2: Test UnixDomainSocketServer\nTestcase "
                                + "2.1: Test UnixDomainSocketServer with a stream socket...");
                UnixDomainSocketServer ssocket = new UnixDomainSocketServer(socketFile,
                                UnixDomainSocket.soCK_STREAM);
                in = ssocket.getInputStream();
                out = ssocket.getoutputStream();
                in.read(b);
                System.out.println("Text received: \"" + new String(b) + "\"");
                text = "[5] Hello! I'm the server!";
                out.write(text.getBytes());
                System.out.println("Text sent: " + "\"" + text + "\"");
                ssocket.close();
                ssocket.unlink();

                // Testcase 2.2: Test UnixDomainSocketServer with a datagram socket
                System.out.println("Testcase 2.2: Test UnixDomainSocketServer with "
                                + "a datagram socket...");
                ssocket = new UnixDomainSocketServer(socketFile,
                                UnixDomainSocket.soCK_DGRAM);
                System.out.println("Provoke and catch an "
                                + "UnsupportedOperationException:");
                in = ssocket.getInputStream();
                try {
                        out = ssocket.getoutputStream();
                } catch (UnsupportedOperationException e) {
                        System.out.println("UnsupportedOperationException has been "
                                        + "thrown as expected.");
                }
                in.read(b);
                System.out.println("Text received: \"" + new String(b) + "\"");
                ssocket.close();
                ssocket.unlink();
        }
}

JUDS 官网

http://code.google.com/p/juds

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐