【发布时间】: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