【问题标题】:Sending an int from Java to C using sockets使用套接字将 int 从 Java 发送到 C
【发布时间】:2010-03-15 23:57:26
【问题描述】:

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

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

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

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

【问题讨论】:

  • "DataOutputStream 产生 "prof_agent.so" ...我不这么认为。显示一些代码。

标签: java c sockets integer


【解决方案1】:

您可以使用DataOutputStream.writeInt。它通过合约以网络字节顺序写入一个 int。

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

【讨论】:

    【解决方案2】:

    您可以发送文本表示。所以数字 123 将作为 3 个字节 '1' '2' '3' 发送。

    【讨论】:

    • out.write(""+123456); - 但那是对网络的浪费,请努力通过字节来做到这一点
    【解决方案3】:

    这有点太晚了,但让这个答案在这里。使用 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);
                }
            }
    

    【讨论】:

      【解决方案4】:

      试试这个:

      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
              }
          }
      }
      

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

      【讨论】:

      • 我在 out.writeInt() 上不断收到 IOException。知道它可能是什么,因为根据我的 C 程序有一个连接。
      • 你能粘贴完整的堆栈跟踪吗?
      猜你喜欢
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多