Java的Unix Socket开发包 JUDS

码农软件 · 软件分类 · 网络工具包 · 2019-03-01 11:13:25

软件介绍

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

本文地址:https://codercto.com/soft/d/381.html

数据结构与算法分析

数据结构与算法分析

韦斯 / 机械工业 / 2007-1 / 55.00元

本书是国外数据结构与算法分析方面的标准教材,使用最卓越的Java编程语言作为实现工具讨论了数据结构(组织大量数据的方法)和算法分析(对算法运行时间的估计)。   随着计算机速度的不断增加和功能的日益强大,人们对有效编程和算法分析的要求也在增长。本书把算法分析与最有效率的Java程序的开发有机地结合起来,深入分析每种算法,内容全面、缜密严格,并细致讲解精心构造程序的方法。   第......一起来看看 《数据结构与算法分析》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具