Linux下的c历程和java历程的通信-UnixSocket

打印 上一主题 下一主题

主题 1009|帖子 1009|积分 3027

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1、开发c代码

引用的库

/usr/include
c代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/un.h>
  6. #include <unistd.h>
  7. #define SOCKET_PATH "/tmp/my_socket"
  8. int main() {
  9.         int server_fd, new_socket;
  10.         struct sockaddr_un address;
  11.         int opt = 1;
  12.         int addrlen = sizeof(address);
  13.         char buffer[1024] = { 0 };
  14.         const char *hello = "Hello from C server!";
  15.         // 创建套接字文件描述符
  16.         if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == 0) {
  17.                 perror("socket failed");
  18.                 exit(EXIT_FAILURE);
  19.         }
  20.         // 设置套接字地址结构
  21.         address.sun_family = AF_UNIX;
  22.         strcpy(address.sun_path, SOCKET_PATH);
  23.         // 绑定套接字到指定路径
  24.         if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
  25.                 perror("bind failed");
  26.                 exit(EXIT_FAILURE);
  27.         }
  28.         // 监听连接
  29.         if (listen(server_fd, 3) < 0) {
  30.                 perror("listen");
  31.                 exit(EXIT_FAILURE);
  32.         }
  33.         printf("Server listening on %s...\n", SOCKET_PATH);
  34.         // 接受客户端连接
  35.         if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
  36.                 perror("accept");
  37.                 exit(EXIT_FAILURE);
  38.         }
  39.         // 读取客户端发送的数据
  40.         int valread = read(new_socket, buffer, 1024);
  41.         if (valread < 0) {
  42.                 perror("read");
  43.                 exit(EXIT_FAILURE);
  44.         }
  45.         printf("Received from client: %s\n", buffer);
  46.         // 发送响应给客户端
  47.         send(new_socket, hello, strlen(hello), 0);
  48.         printf("Message sent to client\n");
  49.         // 关闭套接字
  50.         close(new_socket);
  51.         close(server_fd);
  52.         unlink(SOCKET_PATH);
  53.         return 0;
  54. }
复制代码
编译

gcc -o myserver server.c
执行


2、开发java代码

引入第三方库junixsocket ,注意这里的type是pom

GitCode - 全球开发者的开源社区,开源代码托管平台
  1. https://gitcode.com/gh_mirrors/ju/junixsocket/blob/main/junixsocket-demo/src/main/java/org/newsclub/net/unix/demo/SimpleTestClient.java
复制代码
  1.         <dependency>
  2.             <groupId>com.kohlschutter.junixsocket</groupId>
  3.             <artifactId>junixsocket-core</artifactId>
  4.             <version>2.10.1</version>
  5.             <type>pom</type>
  6.         </dependency>
复制代码
代码

  1. package com.example.service;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.SocketException;
  9. import java.nio.charset.StandardCharsets;
  10. import org.newsclub.net.unix.AFUNIXSocket;
  11. import org.newsclub.net.unix.AFUNIXSocketAddress;
  12. public class SimpleTestClient {
  13.     public static void main(String[] args) throws IOException {
  14. //        final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")),
  15. //                "junixsocket-test.sock");
  16.         final File socketFile = new File("/tmp/my_socket");
  17.         boolean connected = false;
  18.         try (AFUNIXSocket sock = AFUNIXSocket.connectTo(AFUNIXSocketAddress.of(socketFile));
  19.              InputStream is = sock.getInputStream(); //
  20.              OutputStream os = sock.getOutputStream();
  21.              DataInputStream din = new DataInputStream(is);
  22.              DataOutputStream dout = new DataOutputStream(os);) {
  23.             System.out.println("Connected");
  24.             connected = true;
  25.             byte[] buf = new byte[128];
  26.             int read = is.read(buf);
  27.             System.out.println("Server says: " + new String(buf, 0, read, StandardCharsets.UTF_8));
  28.             System.out.println("Replying to server...");
  29.             os.write("Hello Server".getBytes(StandardCharsets.UTF_8));
  30.             os.flush();
  31.             System.out.println("Now reading numbers from the server...");
  32.             while (!Thread.interrupted()) {
  33.                 int number = din.readInt();
  34.                 if (number == -123) {
  35.                     // by convention of this demo, if the number is -123, we stop.
  36.                     // If we don't do this, we'll get an EOFException upon the next unsuccessful read.
  37.                     break;
  38.                 }
  39.                 System.out.println(number);
  40.                 int ourNumber = number * 2;
  41.                 System.out.println("Sending back " + ourNumber);
  42.                 dout.writeInt(ourNumber);
  43.             }
  44.         } catch (SocketException e) {
  45.             if (!connected) {
  46.                 System.out.println("Cannot connect to server. Have you started it?");
  47.                 System.out.println();
  48.             }
  49.             throw e;
  50.         }
  51.         System.out.println("End of communication.");
  52.     }
  53. }
复制代码
打包

利用idea,创建Artifacts

Build Artifacts

执行

java -jar nearWeb.jar

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宝塔山

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表