我只想在目标 C 或 C 中模仿下面的 PHP,
<?php
$host="192.168.1.4";
$port = 1000;
$message="Hi";
// open a client connection
$fp = fsockopen ($host, $port, $errno, $errstr);
if (!$fp){
$result = "Error: could not open socket connection";
}
else{
fputs ($fp, $message);
fputs ($fp, "END");
fclose ($fp);
}
?>
我在Objective C中实现了以下功能,但这并不是那么可靠和快速,只有第一条消息被传递,我需要重新连接第二个数据(我已经尝试过https://github.com/robbiehanson/CocoaAsyncSocket,但反映了结果与以下代码相同)。我需要打开数据->发送数据->关闭连接(需要即时无延迟)
NSString *ipaddress =[NSString stringWithFormat:@"192.168.1.4"];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ipaddress, 1000, &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];