如何编写连接到本地文件以发送和接收数据包的客户端套接字流

Posted

技术标签:

【中文标题】如何编写连接到本地文件以发送和接收数据包的客户端套接字流【英文标题】:How can I write the client socket stream which connects to local file for sending and receiving packets 【发布时间】:2013-01-21 11:56:31 【问题描述】:

如何在 Java 中的本地文件系统客户端上编写此套接字流?

Python 工作:

# Echo server program
import socket,os

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
    os.remove("/tmp/socketname")
except OSError:
    pass
s.bind("/tmp/socketname")
s.listen(1)
conn, addr = s.accept()
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()


# Echo client program
import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/socketname")
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

Java:

  /* Client: how do you mention the /tmp/socketname? */
  public static String localsendTCPBytes(String bytes) throws IOException 
    String downloaded = null;
    Socket socket = new Socket("localhost", 58888);
    DataOutputStream upload = new DataOutputStream(socket.getOutputStream());
    BufferedReader download = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    String caps = bytes;
    upload.writeBytes(caps);
    upload.flush();
    String get;
    downloaded = download.readLine();
    System.out.println("[TCP]: downloading: " + downloaded);
    socket.close();
    return downloaded;
  

跟进:

1) 导入jar并附加到项目中:

JUnixsocket - download JAR

2) Unitest Java 客户端代码

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
import org.newsclub.net.unix.AFUNIXSocketException;

public class SimpleTestClient 
  public static void main(String[] args) throws IOException 
    final File socketFile = new File("/tmp/socketname");
    AFUNIXSocket sock = AFUNIXSocket.newInstance();
    try 
      sock.connect(new AFUNIXSocketAddress(socketFile));
     catch (AFUNIXSocketException e) 
      System.out.println("Cannot connect to server. Have you started it?");
      System.out.flush();
      throw e;
    
    System.out.println("Connected");
    InputStream is = sock.getInputStream();
    OutputStream os = sock.getOutputStream();
    byte[] buf = new byte[128];
    int read = is.read(buf);
    System.out.println("Server says: " + new String(buf, 0, read));
    System.out.println("Replying to server...");
    os.write("Hello Server".getBytes());
    os.flush();
    os.close();
    is.close();
    sock.close();
    System.out.println("End of communication.");
  

3) 构建/编译问题:

java.lang.UnsatisfiedLinkError: Could not load junixsocket library, tried [/opt/newsclub/lib-native/libjunixsocket-linux-1.6-amd64.so, /opt/newsclub/lib-native/libjunixsocket-linux-1.5-amd64.so, lib:junixsocket-linux-1.6-amd64, lib:junixsocket-linux-1.5-amd64]; please define system property org.newsclub.net.unix.library.path
    at org.newsclub.net.unix.NativeUnixSocket.load(NativeUnixSocket.java:95)
    at org.newsclub.net.unix.NativeUnixSocket.<clinit>(NativeUnixSocket.java:105)
    at org.newsclub.net.unix.AFUNIXSocket.<init>(AFUNIXSocket.java:36)
    at org.newsclub.net.unix.AFUNIXSocket.newInstance(AFUNIXSocket.java:50)
    at unixdomainsocket.SimpleTestClient.main(SimpleTestClient.java:26)
Caused by: java.lang.UnsatisfiedLinkError: no junixsocket-linux-1.5-amd64 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
    at java.lang.Runtime.loadLibrary0(Runtime.java:840)
    at java.lang.System.loadLibrary(System.java:1047)
    at org.newsclub.net.unix.NativeUnixSocket.load(NativeUnixSocket.java:77)
    ... 4 more
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.newsclub.net.unix.NativeUnixSocket
    at org.newsclub.net.unix.AFUNIXSocketImpl.connect(AFUNIXSocketImpl.java:125)
    at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:97)
    at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:87)
    at unixdomainsocket.SimpleTestClient.main(SimpleTestClient.java:28)
Java Result: 1

4) 解决方案:

-Djava.library.path=/usr/lib/jni -Djava.library.path=/usr/lib/rxtx -Djava.security.policy=applet.policy -Djava.library.path=/var/tmp/screwyou/jar/lib-native

有效!

【问题讨论】:

this 不相关吗? 【参考方案1】:

“junixsocket 是一个 Java/JNI 库,它允许从 Java 中使用 Unix 域套接字(AF_UNIX 套接字)。”

https://code.google.com/p/junixsocket/

“JUDS(Java Unix 域套接字)提供类来满足 Java 中访问 Unix 域套接字的需求。”

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

【讨论】:

【参考方案2】:

虽然问题中提到了对我有用的解决方案,但我发现我只需要像这样在 JVM 上设置一个 -D 标志来解决问题:

-Djava.library.path=/usr/lib/jni

【讨论】:

【参考方案3】:

您不能使用 Java JDK 中的 Unix 域套接字。如果你能找到,你需要一个外部包。

【讨论】:

以上是关于如何编写连接到本地文件以发送和接收数据包的客户端套接字流的主要内容,如果未能解决你的问题,请参考以下文章

如何编写 Netty 客户端以连接到 Signalr 集线器?

TCP C#包到特定客户端

Xamarin/WinForms 客户端在双线程架构中发送/接收时无法连接到套接字

Joomla - 如何在 php 文件中使用 Ajax 接收数据后连接到 db

如何查看连接到我的路由器的设备列表并选择其中一个,然后从中发送和接收数据

连接到 Spark 集群时的序列化问题