我只是想immitate下面的PHP在客觀C或C,目標C中的TCP/IP?
<?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);
}
?>
我已經實現了目標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];
它可能是操作系統特定的。閱讀http://advancedlinuxprogramming.com/for Linux。 –