通过套接字从 Objective-C 向 Java 发送数据

发布于 2024-07-26 10:37:18 字数 194 浏览 2 评论 0原文

我有一个 Java 程序(称为 Jack)和一个 Objective-C 程序(称为 Oscar),它们在同一台 Mac OS X 计算机上运行。 Oscar 通过套接字向 Jack 每秒发送一次字符串消息。

为了可靠性和性能,在 Jack 和 Oscar 之间保持开放套接字会更好吗? 或者重复打开套接字,发送消息,然后再次关闭套接字会更好吗?

I have a Java program (call it Jack) and an Objective-C program (call it Oscar), which I run on the same Mac OS X computer. Oscar sends a string message via a socket to Jack, once per second.

For reliability and performance would it be better to maintain an open socket between Jack and Oscar? Or would it be better to repeatedly open a socket, send the message, then close the socket again?

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

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

发布评论

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

评论(5

七色彩虹 2024-08-02 10:37:19

保持打开状态 杰克,保持打开状态。 我已经花费了一些 CPU 周期来打开和关闭连接,然后下一秒再次执行此操作。

Keep it open Jack, keep it open. It takes me some CPU cycles already to open and close the connection only to do it again the next second.

千仐 2024-08-02 10:37:19

进行 Java Native Interface 调用会更容易吗? 就我个人而言,我认为在本地搞乱套接字可能有点矫枉过正,但话又说回来,我不知道你想要完成什么的整个故事。

Would it be easier to make a Java Native Interface call? Personally, I think that messing with sockets locally might be a little overkill, but then again, I do not know the whole story about what you are trying to accomplish.

岁月静好 2024-08-02 10:37:19

如果您可以忍受丢弃一两个数据包,您可能需要改用 UDP。

每隔一段时间,长期 TCP 连接就会变得有点奇怪,并在连接出现问题时挂起。 通常会恢复,但并非总是如此,同时它们可能会变慢。

如果您每次都重新发送所有数据并且不需要每个数据包,因为您不关心历史记录,则 UDP 可以更好地运行...

保持开放连接可能对您有用,并且理论上是没问题的。 ..我只是并不总是有最好的运气。

If you can stand to drop a packet or two, you might want to use UDP instead.

Every once in a while, long-term TCP connections get a little funky and hang when the connection goes bad. Usually the recover, but not always--and in the meantime they can get slow.

UDP is made to operate better in cases where you are resending all the data every time and didn't need every single packet because you don't care about the history...

Keeping an open connection may work for you and is theoretically fine... I just haven't always had the best luck.

谜兔 2024-08-02 10:37:19

抱歉,我很快就读到了这个问题。 是的,如果套接字位于本地计算机上,我会保持套接字打开。 打开和关闭每个需要分配的内存是没有意义的。 在这种情况下,来回打磨是没有帮助的。

我理解正确的是,您正在编写一个 Cocoa 服务器应用程序来侦听连接,以便将一些数据传递给无法访问从 Cocoa API 返回的信息的 Java 应用程序?

您确定不能仅从 Java 中的终端命令获取结果吗? 我完全是猜测,但我认为如果是这种情况,你可以改进你的计划。

Sorry I read the question rather quickly. Yes I would keep the socket open if it is on the local machine. It makes no sense to be opening and closing every memory needs to be allocated. Grinding back and forth won't help in that case.

Just so I understand correctly, you are writing a Cocoa server app that listens for a connection just so it can pass some data to a Java app that doesn't have access to the information returned from the Cocoa API?

Are you sure you couldn't just get the results from a terminal command in Java. I'm totally guessing but I thought if this is the case you could improve what you plan to do.

情话已封尘 2024-08-02 10:37:18

保持打开状态。 您将经常需要它(每秒一次),并且打开新套接字会涉及一些开销。 另外,您将用新对象咀嚼堆,直到垃圾收集器到来。

Keep it open. You are going to need it a lot (once per second), and there's some overhead involved in opening new sockets. Plus, you will be chewing up the heap with new objects until the garbage collector comes by.

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