views:

869

answers:

6

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?

A: 

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.

Brock Woolf
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.
Steve McLeod
+1  A: 

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.

geowa4
+1  A: 

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.

bogertron
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.
OscarRyz
I weighed up using JNI versus sockets. JNI is nasty stuff, prone to messy bugs.
Steve McLeod
+2  A: 

Hey Jack this is Oscar.

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.

OscarRyz
+1  A: 

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.

Bill K
A: 

Hi, Can anybody help me how to maintain the connection in open status and writes the data repeatedly using objective c.

Thanks.

Ramesh
You should create a new question to ask this - you'll get more answers quicker!
Steve McLeod