If you don't want to create a new socket each time, you could add some sort of framing to your messages and have your server parse them while the connection is still open instead of waiting for it to close. It seems like you'd want to do this anyway so that the server could send some sort of confirmation back to the client that it received the message successfully.

(Aug 22, 2011 12:19 PM)ThemsAllTook Wrote: If you don't want to create a new socket each time

it is not about what i want or not, im asking how it should be? if all developers who working with a sockets create socket each time they send data, than ill do the same, no problem
I was just wondering is it right desition? is it the way it should be? as for example in C or Java u define socket just once and than just open/close streams, so i was pretty surprised that in Objective-C u cant open stream after u close it, even if u didnt delete your socket definition and still using same parameters...

(Aug 22, 2011 12:19 PM)ThemsAllTook Wrote: you could add some sort of framing to your messages and have your server parse them while the connection is still open instead of waiting for it to close. It seems like you'd want to do this anyway so that the server could send some sort of confirmation back to the client that it received the message successfully.

I didnt touch yet any "read" part... All i know from the official API Docs that ill have to schedule it for run loop to delegate, it will let me to handle all incoming data as event and will prevent program from blocking and i will be able to receive data unexpectedly, what i exactly need.

but how should i do/ implement this framing? could you direct me on some example code?

(Aug 22, 2011 12:28 PM)Briksins Wrote: it is not about what i want or not, im asking how it should be? if all developers who working with a sockets create socket each time they send data, than ill do the same, no problem
I was just wondering is it right desition? is it the way it should be? as for example in C or Java u define socket just once and than just open/close streams, so i was pretty surprised that in Objective-C u cant open stream after u close it, even if u didnt delete your socket definition and still using same parameters...

Well, I'd say just to think about what it means to open a new connection and whether it makes sense for your situation. If you're in continual communication with the server, it may make more sense to use just one socket and keep it open the whole time. If you're just sending messages back and forth occasionally, it may be more convenient to make a new connection each time you want to send something.

It does seem a bit arbitrary that Cocoa would make you create a new stream object rather than being able to reopen one after closing it, but I guess that's just the way they designed their API. You can always drop down to C and use the BSD socket API instead if you prefer.

(Aug 22, 2011 12:28 PM)Briksins Wrote: but how should i do/ implement this framing? could you direct me on some example code?

Framing can be any information sent along in your stream that allows the server to distinguish between messages. For example, if you're only sending text data, you could send a null byte at the end of every message. The server could then seek for a null byte every time it receives new data, and if it finds one, snip out the preceding data and respond appropriately to it. You could also send a number indicating the number of bytes in the message at the beginning of each one, and the server would handle the message when it had received that many bytes, then expect another byte count and message to follow. There are plenty of other ways you can do it; anything that lets the server unambiguously figure out message boundaries in a stream of data will work.

Thank you very much it is very helpful! I probably cant afford to keep constant connection open client->server as there might be other players banging to the server at the same port, so i think the best decision is to keep sending short messages and close connection, its not a problem to include socket creation line in to the sendData() code. but for read() i can create another constant open socket for the server->client communication as only server will contact client and there will not be any other client banging to that door.

how practically its possible?
//Read
CFStreamCreatePairWithSocket (*** myRead ***)
schedule for run loop
implement event handler for the loop