设置套接字流不起作用

Right, I want to create a connection with my server (TCP Open) for this I'm learning with this tutorial from apple docs (No, I don't want to use other projects). Below is my code:

ViewController.h

@interface ViewController : UIViewController <NSStreamDelegate>{

}

@property (nonatomic, retain) NSInputStream *inputStream;

@property (nonatomic, retain) NSOutputStream *outputStream;

@end

ViewController.m

- (IBAction)searchForSite:(id)sender
{

    NSString *urlStr = @"http://mysitefake.com/host.php";
    if (![urlStr isEqualToString:@""]) {
        NSURL *website = [NSURL URLWithString:urlStr];
        if (!website) {
            NSLog(@"is not a valid URL");
            return;
        }

        CFReadStreamRef readStream;
        CFWriteStreamRef writeStream;
        CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)[website host], 80, &readStream, &writeStream);

        inputStream = (__bridge_transfer NSInputStream *)readStream;
        outputStream = (__bridge_transfer NSOutputStream *)writeStream;
        [inputStream setDelegate:self];
        [outputStream setDelegate:self];
        [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [inputStream open];
        [outputStream open];

        /* Store a reference to the input and output streams so that
         they don't go away.... */

    }
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

    switch(eventCode) {
        case NSStreamEventHasSpaceAvailable:
        {
            NSLog(@"has space available");
            if (stream == outputStream) {
                NSLog(@"Equals");
                NSString * str = [NSString stringWithFormat:
                                  @"String to send to my server"];
                const uint8_t *rawstring = (const uint8_t *)[str UTF8String];
                [outputStream write:rawstring maxLength:10240];
                [outputStream close];
            }else{
                NSLog(@"Not equals");
            }
            break;
        }
           default:
           break;
    }
}

And in my server has a PHP file with the following code:

host.php

<?php
error_reporting(E_ALL);
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');

/* Get the IP address for the target host. */
$address = gethostbyname('www.mysitefake.com');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . 
         socket_strerror(socket_last_error()) . "
";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.
Reason: ($result) " . 
          socket_strerror(socket_last_error($socket)) . "
";
}

$in = "HEAD / HTTP/1.1
";
$in .= "Host: www.mysite.com
";
$in .= "Connection: Close

";
$out = '';

echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.
";

echo "Reading response:

";
while ($out = socket_read($socket, 2048)) {
    echo $out;
}

socket_close($socket);
?>

Great the output from php file is:

Attempting to connect to 'Fake Ip' on port '80'...Sending HTTP HEAD request...OK. Reading response: HTTP/1.1 200 OK Date: Sun, 03 Jan 2016 15:53:10 GMT Server: Apache Cache-Control: max-age=31536000 Expires: Mon, 02 Jan 2017 15:53:10 GMT Vary: Accept-Encoding,User-Agent Connection: close Content-Type: text/html; charset=UTF-8

The output from app is:

Has space available Equals

It seems that something was sent but when I try to check it in host.php I can not see the message that was send, which in the case was "String to send to my server".

Whats the problem with my code?

I suspect you have an Apache web server running on port 80, possibly hosting your PHP test script. The app is opening a client connection to port 80, which means it will talk to Apache. Your PHP script is also opening a client connection to port 80, so it would also connect to Apache (this would explain the Apache messages you get in your PHP log). The way you are trying to set up a connection does not make sense to me. Normally you would have one application running as a server. This means it has opened a listening socket on a specific port. Then you have a second application opening a client connection to the said port and sending some initial data. The listening server will get that data and react by sending back some data, etc. The tutorial you followed is for setting up such a client connection to a server.