设置套接字流don';不起作用


Setting Up Socket Streams don't work

好的,我想创建一个与我的服务器(TCP-Open)的连接,我正在从苹果文档中学习本教程(不,我不想使用其他项目)。以下是我的代码:

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;
    }
}

在我的服务器中有一个PHP文件,其中包含以下代码:

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()) . "'n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.'nReason: ($result) " . 
          socket_strerror(socket_last_error($socket)) . "'n";
}
$in = "HEAD / HTTP/1.1'r'n";
$in .= "Host: www.mysite.com'r'n";
$in .= "Connection: Close'r'n'r'n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.'n";
echo "Reading response:'n'n";
while ($out = socket_read($socket, 2048)) {
    echo $out;
}
socket_close($socket);
?>

很棒php文件的输出是:

正在尝试连接到端口"80"上的"Fake Ip"。。。发送HTTPHEAD请求。。。好。阅读响应:HTTP/1.1 200好日期:1月3日,星期日2016 15:53:10 GMT服务器:Apache缓存控制:最大年龄=31536000到期时间:2017年1月2日星期一15:53:10 GMT变化:接受编码,用户代理连接:关闭内容类型:text/html;charset=UTF-8

应用程序的输出为:

有可用空间等于

似乎有什么东西被发送了,但当我试图在host.php中检查它时,我看不到发送的消息,在这种情况下是"发送到我的服务器的字符串"。

我的代码有什么问题?

我怀疑您有一个Apache web服务器在端口80上运行,可能托管您的PHP测试脚本。该应用程序正在打开与端口80的客户端连接,这意味着它将与Apache进行通信。您的PHP脚本还打开了一个到端口80的客户端连接,因此它也将连接到Apache(这将解释您在PHP日志中收到的Apache消息)。你试图建立连接的方式对我来说没有意义。通常你会有一个应用程序作为服务器运行。这意味着它在特定端口上打开了一个侦听套接字。然后,第二个应用程序打开了与该端口的客户端连接,并发送了一些初始数据。监听服务器将获取这些数据,并通过发送回一些数据等做出反应。您遵循的教程是关于设置这样一个客户端到服务器的连接。