简体   繁体   English

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

[英]How can I write the client socket stream which connects to local file for sending and receiving packets

How to write this socket stream on local file system client in Java? 如何在Java中的本地文件系统客户端上编写此套接字流?

Python works: 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: 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;
  }

Follow up: 跟进:

1) Import jar and attach to the project: 1)导入jar并附加到项目:

JUnixsocket - download JAR JUnixsocket - 下载JAR

2) Unitest Java client code 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) Build/compile problem: 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) Solution: 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

Works! 作品!

"junixsocket is a Java/JNI library that allows the use of Unix Domain Sockets (AF_UNIX sockets) from Java." “junixsocket是一个允许使用Java的Unix域套接字(AF_UNIX套接字)的Java / JNI库。”

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

"JUDS (Java Unix Domain Sockets) provide classes to address the need in Java for accessing Unix domain sockets." “JUDS(Java Unix Domain Sockets)提供了一些类来满足Java中访问Unix域套接字的需求。”

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

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

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

You can't use Unix domain sockets from the Java JDK. 您不能使用Java JDK中的Unix域套接字。 You would need an external package, if you can find one. 你需要一个外部包,如果你能找到一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何编辑此代码,以便服务器在发送到客户端之前先对文件进行压缩,而在接收后客户端将其解压缩 - How I can edit this code, so that server zips the file before sending to the client and Client unzips this after receiving 我如何使用 java 中的客户端服务器套接字编程在网络上使用 stream 镶木地板文件 - How can i stream parquet file over network using client server socket programming in java 如何从包含文本标题的套接字输入流中读取二进制文件?: - How can I read a binary file from a socket input stream which includes textual headers?: 发送和接收UDP数据包? - Sending and receiving UDP packets? Socket.io android java客户端接收消息并发送文件示例 - Socket.io android java client receiving messages and sending file example 如何在Java中创建文件包 - How can I Create packets of a file in Java Android套接字已连接但无法写入 - Android socket connects but cant write to it 服务器/客户端不通过套接字[Java]发送或接收数据? - Server/client not sending or receiving data through socket [Java]? Android客户端/ Java服务器套接字; android发送但没有收到? - Android client/Java server socket; android sending but not receiving? Java多线程套接字客户端/服务器:发送和接收Enummap对象 - Java Multi threaded socket client/server: sending and receiving Enummap objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM