0

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?

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

2 Comments

This is a comment not an answer. I guess the next problem will be if you by some reason use two machines instead of one.
I weighed up using JNI versus sockets. JNI is nasty stuff, prone to messy bugs.
1

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.

Comments

0

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.

1 Comment

It's not really over the network. Both processes run locally, on the same machine. I need to get some Mac system info regularly that's not available via Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.