【问题标题】:Setting Up Socket Streams don't work设置套接字流不起作用
【发布时间】:2016-01-03 16:03:31
【问题描述】:

是的,我想为此创建与我的服务器的连接(TCP Open),我正在使用来自苹果文档的tutorial 进行学习(不,我不想使用其他项目)。以下是我的代码:

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”上的“假 IP”...发送 HTTP HEAD 请求...好的。读取响应:HTTP/1.1 200 OK 日期:Sun,1 月 3 日 2016 15:53:10 GMT 服务器:Apache 缓存控制:max-age=31536000 过期时间:2017 年 1 月 2 日星期一 15:53:10 GMT 变化: Accept-Encoding,User-Agent Connection: close Content-Type: text/html; charset=UTF-8

应用程序的输出是:

有可用空间 等于

似乎发送了一些东西,但是当我尝试在 host.php 中检查它时,我看不到发送的消息,在这种情况下是“要发送到我的服务器的字符串”。

我的代码有什么问题?

【问题讨论】:

    标签: php ios objective-c sockets


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-18
      • 2019-07-22
      • 1970-01-01
      • 2017-09-14
      • 2016-07-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多