使用套接字将 int 从 Java 发送到 C

发布于 2024-08-25 15:14:58 字数 315 浏览 2 评论 0原文

我只是想知道如何使用套接字将 int 从 Java 应用程序发送到 C 应用程序。我有不同的 C 程序相互通信,并且 Java 应用程序从 C 应用程序检索数据,但我无法发送。

C 应用程序充当数据库,然后 Java 应用程序将用户 ID(4 位数字)发送到 C 应用程序,如果存在,则返回该记录的详细信息。

在Java中,我尝试使用printWriter和DataOutputStream来发送数据,printWriter产生奇怪的符号,DataOutputStream产生“prof_agent.so”。

任何帮助将不胜感激,因为我目前对套接字还没有很好的掌握。

I was just wondering how to send an int from a Java application to a C application using sockets. I have got different C programs communicating with each other and have got the Java application retrieving data from the C application, but I can't work out sending.

The C application is acting as database, the Java application then sends a user id (a 4 digit number) to the C application, if it exists it returns that record's details.

In Java I have tried using a printWriter and DataOutputStream to send the data, printWriter produces weird symbols and DataOutputStream produces "prof_agent.so".

Any help would be appreciated as I don't have a good grasp of sockets at the moment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤单情人 2024-09-01 15:14:58

您可以使用 DataOutputStream。 writeInt。它按照约定以网络字节顺序写入一个 int 。

C端,您可以调用recvread来填充4字节缓冲区,然后您可以使用ntohl ( Network-TO-Host-Long ) 将您刚刚读取的值转换为平台 int 表示形式。

You can use DataOutputStream.writeInt. It writes an int already in network byte order by contract.

On a C side you can call recv, or read to fill in the 4-byte buffer, and then you can use ntohl ( Network-TO-Host-Long ) to convert the value you've just read to your platform int representation.

风月客 2024-09-01 15:14:58

您可以发送文本表示。因此数字 123 将作为 3 个字节“1”“2”“3”发送。

You can send the textual representation. So the number 123 would be sent as 3 bytes '1' '2' '3'.

卷耳 2024-09-01 15:14:58

虽然有点晚了,但还是把这个答案放在这里吧。使用 UDP 套接字:

Java 代码:

public void runJavaSocket() {
            System.out.println("Java Sockets Program has started."); int i=0;    
    try {
            DatagramSocket socket = new DatagramSocket();
            System.out.println("Sending the udp socket...");
            // Send the Message "HI"
            socket.send(toDatagram("HI",InetAddress.getByName("127.0.0.1"),3800));
            while (true)
            {
              System.out.println("Sending hi " + i);
              Thread.currentThread();
              Thread.sleep(1000);
              socket.send(toDatagram("HI " +             String.valueOf(i),InetAddress.getByName("127.0.0.1"),3800));
                    i++;
            }
        } 
    catch (Exception e) 
    {
         e.printStackTrace();
    }
}

public DatagramPacket toDatagram(
          String s, InetAddress destIA, int destPort) {
    // Deprecated in Java 1.1, but it works:
    byte[] buf = new byte[s.length() + 1];
    s.getBytes(0, s.length(), buf, 0);
    // The correct Java 1.1 approach, but it's
    // Broken (it truncates the String):
    // byte[] buf = s.getBytes();
    return new DatagramPacket(buf, buf.length, 
    destIA, destPort);
    }

C# 代码:

        string returnData;
        byte[] receiveBytes;
        //ConsoleKeyInfo cki = new ConsoleKeyInfo();

        using (UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800)))
        {
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800);
            while (true)
            {
                receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
                returnData = Encoding.ASCII.GetString(receiveBytes);
                Console.WriteLine(returnData);
            }
        }

It's a bit too late but let this answer be here. Using UDP sockets:

Java code:

public void runJavaSocket() {
            System.out.println("Java Sockets Program has started."); int i=0;    
    try {
            DatagramSocket socket = new DatagramSocket();
            System.out.println("Sending the udp socket...");
            // Send the Message "HI"
            socket.send(toDatagram("HI",InetAddress.getByName("127.0.0.1"),3800));
            while (true)
            {
              System.out.println("Sending hi " + i);
              Thread.currentThread();
              Thread.sleep(1000);
              socket.send(toDatagram("HI " +             String.valueOf(i),InetAddress.getByName("127.0.0.1"),3800));
                    i++;
            }
        } 
    catch (Exception e) 
    {
         e.printStackTrace();
    }
}

public DatagramPacket toDatagram(
          String s, InetAddress destIA, int destPort) {
    // Deprecated in Java 1.1, but it works:
    byte[] buf = new byte[s.length() + 1];
    s.getBytes(0, s.length(), buf, 0);
    // The correct Java 1.1 approach, but it's
    // Broken (it truncates the String):
    // byte[] buf = s.getBytes();
    return new DatagramPacket(buf, buf.length, 
    destIA, destPort);
    }

C# code:

        string returnData;
        byte[] receiveBytes;
        //ConsoleKeyInfo cki = new ConsoleKeyInfo();

        using (UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800)))
        {
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800);
            while (true)
            {
                receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
                returnData = Encoding.ASCII.GetString(receiveBytes);
                Console.WriteLine(returnData);
            }
        }
悲歌长辞 2024-09-01 15:14:58

试试这个:

Socket s = ...;
DataOutputStream out = null;
try {
    out = new DataOutputStream( s.getOutputStream() );
    out.writeInt( 123456 );
} catch ( IOException e ) {
    // TODO Handle exception
} finally {
    if ( out != null ) {
        try {
            out.close();
        } catch ( IOException e ) {
            // TODO Handle exception
        }
    }
}

如果您能多解释一下您的问题是什么,将会有所帮助。

Try this:

Socket s = ...;
DataOutputStream out = null;
try {
    out = new DataOutputStream( s.getOutputStream() );
    out.writeInt( 123456 );
} catch ( IOException e ) {
    // TODO Handle exception
} finally {
    if ( out != null ) {
        try {
            out.close();
        } catch ( IOException e ) {
            // TODO Handle exception
        }
    }
}

It whould help if you could explain a little more what your problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文