3
votes

I have a server application wrote on C:

int main(int argc, char const *argv[]) {

int sock;

struct sockaddr_un serv_addr;

size_t serv_addr_len;

int msgsock;
char buf[1024];
int msg_len;

remove(SOCKET_ADDRESS);

sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);

if (sock < 0) {
    perror("Opening stream socket");
    exit(1);
}

memset(&serv_addr, 0, sizeof(serv_addr));

serv_addr.sun_family = AF_UNIX;

strcpy(serv_addr.sun_path, SOCKET_ADDRESS);

serv_addr_len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);

if (bind(sock, (struct sockaddr *) &serv_addr, serv_addr_len)) {
    perror("Binding stream socket");
    exit(1);
}

if (listen(sock, 5) < 0) {
    perror("Listening falied");
    exit(1);
}

while (TRUE) {
    msgsock = accept(sock, (struct sockaddr *) 0, 0);

    if (msgsock == -1) {
        perror("Accept");
    }
    else {
        do {
            memset(buf, 0, sizeof(buf));

            if ((msg_len = recv(msgsock, buf, sizeof(buf), MSG_WAITALL)) < 0) {
                perror("Reading stream message");
            }
            else if (msg_len == 0) {
                printf("Ending connection\n");
            }
            else {
                printf("%s\n", buf);
            }
        } while (msg_len > 0);

        close(msgsock);
    }
}
return 0;
}

And have a client wrote on Java with junixsocket lib:

package client;

import java.io.*;
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

public class Client {

public static void main(String args[]) {

    final String socketAddress = "/home/andrew/IdeaProjects/Sample/tmp/SOCKET_ADDRESS";

    String message = "Hello Server!";

    try {
        final File socketFile = new File(socketAddress);

        AFUNIXSocket socket = AFUNIXSocket.newInstance();

        socket.connect(new AFUNIXSocketAddress(socketFile));

        OutputStream out = socket.getOutputStream();

        out.write(message.getBytes());
        out.flush();
        socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
}

I started server, and try to connnect, then get exception:

/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java -Didea.launcher.port=7533 -Didea.launcher.bin.path=/home/andrew/Downloads/idea-IU-163.7743.44/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/rt.jar:/home/andrew/IdeaProjects/Sample/out/production/Sample:/home/andrew/Workspace/libs/junixsocket-1.3.jar:/home/andrew/Workspace/libs/junixsocket-demo-1.3.jar:/home/andrew/Workspace/libs/junixsocket-mysql-1.3.jar:/home/andrew/Workspace/libs/junixsocket-rmi-1.3.jar:/home/andrew/Downloads/idea-IU-163.7743.44/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain client.Client org.newsclub.net.unix.AFUNIXSocketException: Protocol wrong type for socket (socket: /home/andrew/IdeaProjects/Sample/tmp/SOCKET_ADDRESS) at org.newsclub.net.unix.NativeUnixSocket.connect(Native Method) 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 client.Client.main(Client.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Please help me!

1
Java and C programs should be able to communicate since they are both using sockets. The specific error you are getting seems to be this part "AFUNIXSocketException: Protocol wrong type for socket"dahui

1 Answers

0
votes

The unix domain sockets is a mechanism that is platform specific and it is not supported by java without a native library. You use the org.newsclub.net.unix.AFUNIXSocket. But this library supports socket types SOCK_STREAM and SOCK_DGRAM only. Your C application uses SOCK_SEQPACKET and it is a linux specific extension of unix domain sockets that is not supported by this library.