Barzille
Barzille

Reputation: 405

Objective-C Socket read/write minimal example

hopefully someone can give me a minimal working example of opening a socket, sending a command ans receiving the answer.

In php I would use this code:

$socket = getsock('127.0.0.1', 4028);
socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket);
socket_close($socket);

In $line I will receive the answer from the socket's target. How would I accomplish that in objective-c (and yes, I have taken a look at apple's docs but I couldn't find/understand the way to send and receive commands correctly).

Thank you :)

Upvotes: 0

Views: 214

Answers (1)

bbum
bbum

Reputation: 162712

Trying to convert code line by line from one system to another doesn't really work. You need to understand how the target system is architected and what is expected of your code first.

Specifically:

  • how run loops work (and queues)
  • the difference between the main run loop & non-main run loops (or queues)
  • how iOS applications do data communications

Then, you'll need to decide if you want to run your request asynchronously (most likely) or synchronously on a background thread (unlikely).

From there, you'll probably want to leverage the URL loading infrastructure provided by the NSURLConnection class.

Upvotes: 1

Related Questions