简体   繁体   中英

raw socket communication between two applications one in c and one in java

I have a application (acting as the server) written in c that is listening on a raw socket, with this socket descriptor: socket(AF_UNIX, SOCK_RAW, 0); . The 0 indicates IPPROTO_IP

I want to write a code in java (acting as the client) to send an ip packet to this raw socket that is listening. Now i read around that java needs a 3rd party library to manage raw sockets. People recommend junixsocket and juds so i'll use one of them i guess.

Junixsocket does something like this to define a raw socket:

File socketFile = new File("/path/to/your/socket");
AFUNIXSocket sock = AFUNIXSocket.newInstance();
sock.connect(new AFUNIXSocketAddress(socketFile));

So the question is:

Is it possible to make these 2 applications communicate with eachother through this raw socket? In java you need to establish the socketfilename and path whereas in c thats not compulsory. My c code does not specify a socketfilename or path so i dont know how to make them communicate on the same socket. How do i make sure they're both sending/receiving on the same raw socket? All this communication is happening only in local!

Thank you

You are using the unix domain sockets ( AF_UNIX ). They are effectively designed for processes running on the same kernel, and use filesystem pathnames for addressing (see man 4 unix for details). So you have to share a pathname between both processes.

Edit as requested:

To set up the unix domain socket in C app, you need an address of type sockaddr_un , and fill up its sun_path member with a path to your socket:

    sockaddr_un sockaddr;
    memset(&sockaddr, 0, sizeof(sockaddr));
    sockaddr.sun_family = AF_UNIX;
    strncpy(sockaddr.sun_path, "/path/to/your/socket", UNIX_MAX_PATH);

and either bind() or connect() your socket to this sockaddr .

However, it has nothing to do with SOCK_RAW . Correct me if I am wrong, but you want a raw socket over the Internet, that is socket(AF_INET, SOCK_RAW, 0) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM