kohlschutter / junixsocket

Unix Domain Sockets in Java 7 and newer (AF_UNIX), AF_TIPC, AF_VSOCK, and more
Apache License 2.0
438 stars 114 forks source link

Error when using junixsocket client to connect python3 domain unit socket server ? #128

Closed shenbinglife closed 1 year ago

shenbinglife commented 1 year ago

Describe the bug

  1. Runing a Domain SocketServer by python 3.9
  2. Try using junixsocket 2.6.1 to connect, but get error
    
    Cannot connect to server. Have you started it?

Exception in thread "main" java.net.SocketException: at org.newsclub.net.unix.NativeUnixSocket.connect(Native Method) at org.newsclub.net.unix.AFSocketImpl.connect0(AFSocketImpl.java:392) at org.newsclub.net.unix.AFSocket.connect0(AFSocket.java:267) at org.newsclub.net.unix.AFSocket.connect(AFSocket.java:236) at org.newsclub.net.unix.AFSocket.connect(AFSocket.java:231) at org.newsclub.net.unix.AFSocket.connectTo(AFSocket.java:179) at org.newsclub.net.unix.AFUNIXSocket.connectTo(AFUNIXSocket.java:109) at junixsocket.SimpleTestClient.main(SimpleTestClient.java:31)


**To Reproduce**
Steps to reproduce the behavior:

This is Python3.9 Domain Server Code

import socket

class SocketServer: def init(self):

常规tcp监听写法

    # server_address = ('127.0.0.1', 9999)
    # socket_family = socket.AF_INET
    # socket_type = socket.SOCK_STREAM

    # unix domain sockets 监听写法
    server_address = '/home/shenb/baeldung.socket'
    socket_family = socket.AF_UNIX
    socket_type = socket.SOCK_STREAM

    # 其他代码完全一样
    self.sock = socket.socket(socket_family, socket_type)
    self.sock.bind(server_address)
    self.sock.listen(1)
    print(f"listening on '{server_address}'.")
    pass

def wait_and_deal_client_connect(self):
    while True:
        connection, client_address = self.sock.accept()
        data = connection.recv(1024)
        print(f"recv data from client '{client_address}': {data.decode()}")
        connection.sendall("hello client".encode())

def __del__(self):
    self.sock.close()

if name == "main": socket_server_obj = SocketServer() socket_server_obj.wait_and_deal_client_connect()


And this is junixsock client
```java
public class SimpleTestClient {
  public static void main(String[] args) throws IOException {
    final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")),
        "junixsocket-test.sock");

    boolean connected = false;
    try (AFUNIXSocket sock = AFUNIXSocket.connectTo(AFUNIXSocketAddress.of(socketFile));
        InputStream is = sock.getInputStream(); //
        OutputStream os = sock.getOutputStream();
        DataInputStream din = new DataInputStream(is);
        DataOutputStream dout = new DataOutputStream(os);) {
      System.out.println("Connected");
      connected = true;

      byte[] buf = new byte[128];

      int read = is.read(buf);
      System.out.println("Server says: " + new String(buf, 0, read, "UTF-8"));

      System.out.println("Replying to server...");
      os.write("Hello Server".getBytes("UTF-8"));
      os.flush();

      System.out.println("Now reading numbers from the server...");
      while (!Thread.interrupted()) {
        int number = din.readInt();
        if (number == -123) {
          // by convention of this demo, if the number is -123, we stop.
          // If we don't do this, we'll get an EOFException upon the next unsuccessful read.
          break;
        }
        System.out.println(number);

        int ourNumber = number * 2;

        System.out.println("Sending back " + ourNumber);
        dout.writeInt(ourNumber);
      }
    } catch (SocketException e) {
      if (!connected) {
        System.out.println("Cannot connect to server. Have you started it?");
        System.out.println();
      }
      throw e;
    }

    System.out.println("End of communication.");
  }
}

Expected behavior

Output/Screenshots If applicable, add console output/screenshots to help explain your problem.

Environment (please complete the following information):

Please also add the output of java -jar junixsocket-selftest-x.y.z-SNAPSHOT-jar-with-dependencies.jar (x.y.z being the latest version, e.g., 2.3.3). — The selftest jar is available from the junixsocket-dist package in the Release section.

Notes Add any other context about the problem here. Please link/attach any source code that is useful to diagnose the issue.

Lastly, please make sure to test the problem still occurs on the latest version of junixsocket All minor version updates (e.g., 2.4.x -> 2.5.x) are supposed to be backwards compatible. If you find that this isn't the case, please mention it in your report. Thank you!

shenbinglife commented 1 year ago

Sorry for this issue, I wrote a different socket file path