Domanda

I Just want to immitate the below PHP in objective C or C ,

<?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);

}

?>

I have implemented following in Objective C, but that was not that much reliable and fast, only the first message gets delivered and i need to reconnect for second data(i have tried https://github.com/robbiehanson/CocoaAsyncSocket but reflects the same result as below code does). I need to open the data ->send the data ->close connection (needs to be instant without any delay)

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];
È stato utile?

Soluzione

I highly suggest using a higher-level framework for network communication. I've been using CocoaAsyncSocket for most of my projects - much less brain damage than working directly with iOS' networking APIs.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top