- (void)readData{
int err;
int sock;
struct sockaddr_storage addr;
socklen_t addrLen;
uint8_t buffer[65536];
ssize_t bytesRead;
sock = CFSocketGetNative(self->_cfSocket);
addrLen = sizeof(addr);
bytesRead = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &addr, &addrLen);
if (bytesRead < 0) {
err = errno;
} else if (bytesRead == 0) {
err = EPIPE;
} else {
NSData * dataObj;
NSData * addrObj;
err = 0;
dataObj = [NSData dataWithBytes:buffer length:bytesRead];
addrObj = [NSData dataWithBytes:&addr length:addrLen ];
// Tell the delegate about the data.
NSLog(#"receive data");
if ( (self.delegate != nil) && [self.delegate respondsToSelector:#selector(socket:didReceiveData:fromAddress:)] ) {
[self.delegate socket:self didReceiveData:dataObj fromAddress:addrObj];
}
}
if (err != 0) {
NSLog(#"Did Receive Error");
}
}
addrObj is NSData, How do I extract IP address and port number from addrObj?
I think you don't need to use a NSData object to extract the address. I use the following code to extract the IP address and port from which I read:
- (void)readData
{
int sock = CFSocketGetNative(self.cfSocket);
struct sockaddr_storage address;
socklen_t len = sizeof(address);
uint8_t buffer[65536];
ssize_t bytesRead = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *) &address, &len);
int error = (bytesRead < 0) ? errno : 0;
NSLog(#"%zi bytes read from %s:%d...", bytesRead,
inet_ntoa(((struct sockaddr_in*)&address)->sin_addr),
((struct sockaddr_in*)&address)->sin_port);
...
}
Sample output:
2011-05-04 10:41:57.051,-[myClass readData],5 bytes read from 10.112.15.81:37259...
2011-05-04 10:41:57.052,-[myClass readData] read:
"Hello"
Related
I am trying to stream AAC encoded audio data received as CMSampleBuffer. AudioConverterFillComplexBuffer returns 0 status code.
But after passing this data to my FFMPEG HLSWriter audio is not correctly saved (short truncated signals).
Below is the sample code.
static OSStatus inInputDataProc(AudioConverterRef inAudioConverter,
UInt32 *ioNumberDataPackets,
AudioBufferList *ioData,
AudioStreamPacketDescription **outDataPacketDescription,
void *inUserData)
{
KFAACEncoder *encoder = (__bridge KFAACEncoder *)(inUserData);
UInt32 requestedPackets = *ioNumberDataPackets;
if(requestedPackets > encoder.cycledBuffer.size / 2)
{
//NSLog(#"PCM buffer isn't full enough!");
*ioNumberDataPackets = 0;
return -1;
}
static size_t staticBuffSize = 4096;
static void* staticBuff = nil;
if(!staticBuff)
{
staticBuff = malloc(staticBuffSize);
}
size_t outputBytesSize = requestedPackets * 2;
[encoder.cycledBuffer popToBuffer:staticBuff bytes: outputBytesSize];
ioData->mBuffers[0].mData = staticBuff;
ioData->mBuffers[0].mDataByteSize = (int)outputBytesSize;
*ioNumberDataPackets = ioData->mBuffers[0].mDataByteSize / 2;
return noErr;
}
- (void) encodeSampleBuffer:(CMSampleBufferRef)sampleBuffer
{
CFRetain(sampleBuffer);
dispatch_async(self.encoderQueue,
^{
if (!_audioConverter)
{
[self setupAACEncoderFromSampleBuffer:sampleBuffer];
}
CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CFRetain(blockBuffer);
size_t pcmBufferSize = 0;
void* pcmBuffer = nil;
OSStatus status = CMBlockBufferGetDataPointer(blockBuffer, 0, NULL, &pcmBufferSize, &pcmBuffer);
[_cycledBuffer push:pcmBuffer size:pcmBufferSize];
NSError *error = nil;
if (status != kCMBlockBufferNoErr)
{
error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
}
memset(_aacBuffer, 0, _aacBufferSize);
AudioBufferList outAudioBufferList = {0};
outAudioBufferList.mNumberBuffers = 1;
outAudioBufferList.mBuffers[0].mNumberChannels = 1;
outAudioBufferList.mBuffers[0].mDataByteSize = _aacBufferSize;
outAudioBufferList.mBuffers[0].mData = _aacBuffer;
AudioStreamPacketDescription *outPacketDescription = NULL;
UInt32 ioOutputDataPacketSize = 1;
status = AudioConverterFillComplexBuffer(_audioConverter,
inInputDataProc,
(__bridge void *)(self),
&ioOutputDataPacketSize,
&outAudioBufferList,
NULL);
NSData *data = nil;
if (status == 0)
{
NSData *rawAAC = [NSData dataWithBytes:outAudioBufferList.mBuffers[0].mData length:outAudioBufferList.mBuffers[0].mDataByteSize];
if (_addADTSHeader) {
NSData *adtsHeader = [self adtsDataForPacketLength:rawAAC.length];
NSMutableData *fullData = [NSMutableData dataWithData:adtsHeader];
[fullData appendData:rawAAC];
data = fullData;
} else {
data = rawAAC;
}
} else {
error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
}
if (self.delegate) {
KFFrame *frame = [[KFFrame alloc] initWithData:data pts:pts];
NSLog(#"Bytes of data %lu", (unsigned long)data.length);
dispatch_async(self.callbackQueue, ^{
[self.delegate encoder:self encodedFrame:frame];
});
}
CFRelease(sampleBuffer);
CFRelease(blockBuffer);
});
}
I hope someone can help me. I am new to Objective-c and OSX and I am trying to play audio data I am receiving via socket into my audio queue. I found out this link https://stackoverflow.com/a/30318859/4274654 which in away address my issue with circular buffer.
However when I try to run my project it returns
It returns an error (OSStatus) -10865. That is why the code logs " Error enabling AudioUnit output bus".
status = AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &one, sizeof(one));
Here is my code:
Test.h
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import "TPCircularBuffer.h"
#interface Test : Communicator
#property (nonatomic) AudioComponentInstance audioUnit;
#property (nonatomic) TPCircularBuffer circularBuffer;
-(TPCircularBuffer *) outputShouldUseCircularBuffer;
-(void) start;
#end
Test.m
#import "Test.h"
#define kOutputBus 0
#define kInputBus 1
#implementation Test{
BOOL stopped;
}
static OSStatus OutputRenderCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData){
Test *output = (__bridge Test*)inRefCon;
TPCircularBuffer *circularBuffer = [output outputShouldUseCircularBuffer];
if( !circularBuffer ){
SInt32 *left = (SInt32*)ioData->mBuffers[0].mData;
for(int i = 0; i < inNumberFrames; i++ ){
left[ i ] = 0.0f;
}
return noErr;
};
int32_t bytesToCopy = ioData->mBuffers[0].mDataByteSize;
SInt16* outputBuffer = ioData->mBuffers[0].mData;
uint32_t availableBytes;
SInt16 *sourceBuffer = TPCircularBufferTail(circularBuffer, &availableBytes);
int32_t amount = MIN(bytesToCopy,availableBytes);
memcpy(outputBuffer, sourceBuffer, amount);
TPCircularBufferConsume(circularBuffer,amount);
return noErr;
}
-(void) start
{
[self circularBuffer:&_circularBuffer withSize:24576*5];
stopped = NO;
[self setupAudioUnit];
// [super setup:#"http://localhost" port:5321];
}
-(void) setupAudioUnit
{
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
OSStatus status;
status = AudioComponentInstanceNew(comp, &_audioUnit);
if(status != noErr)
{
NSLog(#"Error creating AudioUnit instance");
}
// Enable input and output on AURemoteIO
// Input is enabled on the input scope of the input element
// Output is enabled on the output scope of the output element
UInt32 one = 1;
status = AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &one, sizeof(one));
if(status != noErr)
{
NSLog(#"Error enableling AudioUnit output bus");
}
// Explicitly set the input and output client formats
// sample rate = 44100, num channels = 1, format = 16 bit int point
AudioStreamBasicDescription audioFormat = [self getAudioDescription];
status = AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat));
if(status != noErr)
{
NSLog(#"Error setting audio format");
}
AURenderCallbackStruct renderCallback;
renderCallback.inputProc = OutputRenderCallback;
renderCallback.inputProcRefCon = (__bridge void *)(self);
status = AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &renderCallback, sizeof(renderCallback));
if(status != noErr)
{
NSLog(#"Error setting rendering callback");
}
// Initialize the AURemoteIO instance
status = AudioUnitInitialize(_audioUnit);
if(status != noErr)
{
NSLog(#"Error initializing audio unit");
}
}
- (AudioStreamBasicDescription)getAudioDescription {
AudioStreamBasicDescription audioDescription = {0};
audioDescription.mFormatID = kAudioFormatLinearPCM;
audioDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
audioDescription.mChannelsPerFrame = 1;
audioDescription.mBytesPerPacket = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mFramesPerPacket = 1;
audioDescription.mBytesPerFrame = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mBitsPerChannel = 8 * sizeof(SInt16);
audioDescription.mSampleRate = 44100.0;
return audioDescription;
}
-(void)circularBuffer:(TPCircularBuffer *)circularBuffer withSize:(int)size {
TPCircularBufferInit(circularBuffer,size);
}
-(void)appendDataToCircularBuffer:(TPCircularBuffer*)circularBuffer
fromAudioBufferList:(AudioBufferList*)audioBufferList {
TPCircularBufferProduceBytes(circularBuffer,
audioBufferList->mBuffers[0].mData,
audioBufferList->mBuffers[0].mDataByteSize);
}
-(void)freeCircularBuffer:(TPCircularBuffer *)circularBuffer {
TPCircularBufferClear(circularBuffer);
TPCircularBufferCleanup(circularBuffer);
}
-(TPCircularBuffer *) outputShouldUseCircularBuffer
{
return &_circularBuffer;
}
-(void) stop
{
OSStatus status = AudioOutputUnitStop(_audioUnit);
if(status != noErr)
{
NSLog(#"Error stopping audio unit");
}
TPCircularBufferClear(&_circularBuffer);
_audioUnit = nil;
stopped = YES;
}
-(void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event{
switch (event) {
case NSStreamEventOpenCompleted:
NSLog(#"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (stream == [super inputStream]) {
NSLog(#"NSStreamEventHasBytesAvailable");
uint8_t buffer[1024];
NSUInteger len;
while ([[super inputStream] hasBytesAvailable]) {
len = [[super inputStream] read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
//converting buffer to byte data
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
//NSLog(#"server overideddddd said: %#", output);
}
NSData *data0 = [[NSData alloc] initWithBytes:buffer length:len];
if (nil != data0) {
SInt16* byteData = (SInt16*)malloc(len);
memcpy(byteData, [data0 bytes], len);
double sum = 0.0;
for(int i = 0; i < len/2; i++) {
sum += byteData[i] * byteData[i];
}
Byte* soundData = (Byte*)malloc(len);
memcpy(soundData, [data0 bytes], len);
if(soundData)
{
AudioBufferList *theDataBuffer = (AudioBufferList*) malloc(sizeof(AudioBufferList) *1);
theDataBuffer->mNumberBuffers = 1;
theDataBuffer->mBuffers[0].mDataByteSize = (UInt32)len;
theDataBuffer->mBuffers[0].mNumberChannels = 1;
theDataBuffer->mBuffers[0].mData = (SInt16*)soundData;
NSLog(#"soundData here");
[self appendDataToCircularBuffer:&_circularBuffer fromAudioBufferList:theDataBuffer];
}
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(#"Can't connect to server");
break;
case NSStreamEventEndEncountered:
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
break;
default:
NSLog(#"Unknown event");
}
[super stream:stream handleEvent:event];
}
#end
I would highly appreciate if there is any one with an example of playing buffers returned from a socket server into audio queue so that I can be able to listen to sound as it comes from the socket server.
Thanks
Your code seems to be asking for a kAudioUnitSubType_VoiceProcessingIO audio unit. But kAudioUnitSubType_RemoteIO would be a more suitable iOS audio unit for just playing buffers of audio samples.
Also, your code does not seem to first select an appropriate audio session category and activate it before playing audio. See Apple's documentation for doing this: https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html
I'm facing a problem with write function not working properly if it placed inside a loop with out sleep.what i'm trying to achieve is to transfer files from local machine to a remote FTP server the following code works fine but the write function not transferring the bytes properly it transfer only of the half bytes, it works fine with sleep blocks the loop.
struct sockaddr_in
{
int16_t sin_family;
uint16_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct in_addr
{
uint32_t s_addr;
};
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
int isocket = socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("some ip goes here");
servaddr.sin_port = htons(21);
char buf[1024];
int MAX_LENGTH = 1024;
char readBuf[MAX_LENGTH];
long cmd;
int res = connect(isocket, (struct sockaddr_in *)&servaddr, sizeof(servaddr));
if(res < 0) {
NSLog(#"problem connecting to server");
}
recv(isocket, (void *)readBuf,MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf);
NSLog(#"Issuing command");
strcpy(buf, "USER foo\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0); //send username
recv(isocket, (void *)readBuf,MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "PASS *******\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//send password
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "CWD /httpdocs/testing\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//send directory
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "TYPE I\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//set transfer type
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "PASV\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//set transfer mode to passive
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
NSString *pasvResponse = [NSString stringWithFormat:#"%s",readBuf];
strcpy(buf, "STOR mmov.jpeg\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//start tranfering files
pasvResponse = [pasvResponse stringByReplacingOccurrencesOfString:#"227 Entering Passive Mode (" withString:#""];
pasvResponse = [pasvResponse stringByReplacingOccurrencesOfString:#")." withString:#""];
pasvResponse = [pasvResponse stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *matches = [pasvResponse componentsSeparatedByString:#","];
NSString *ip = nil;
int port = 0;
if([matches count] > 2) {
ip = [NSString stringWithFormat:#"%#.%#.%#.%#",matches[0],matches[1],matches[2],matches[3]];
port = ([matches[4] intValue] * 256) + ([matches[5] intValue]);
}
if(port != 0 && ip != nil) {
struct sockaddr_in servaddr_pasv;
memset(&servaddr_pasv, 0, sizeof(servaddr_pasv));
int isocket_pasv = socket(AF_INET,SOCK_STREAM,0);
servaddr_pasv.sin_family = AF_INET;
servaddr_pasv.sin_addr.s_addr = inet_addr([ip UTF8String]);
servaddr_pasv.sin_port = htons(port);
int res_pasv = connect(isocket_pasv, (struct sockaddr_in *)&servaddr_pasv, sizeof(servaddr_pasv));
if(res_pasv < 0) {
NSLog(#"problem connecting to server passv");
}
NSInputStream *input = [NSInputStream inputStreamWithFileAtPath:#"/Users/myMac/Desktop/limitation.jpeg"];
[input open];
int i= 0;
while(1) {
if([input hasBytesAvailable]) {
i++;
uint8_t buffer[1024];
long res = [input read:buffer maxLength:1024];
NSLog(#"Bytes Read: %ld",res);
if(res != 0) {
long bytesSent = write(isocket_pasv, buffer, sizeof(buffer));
NSLog(#"%d Bytes Transfered: %ld",i,bytesSent);
// sleep(1);
}
if(res == 0) {
break;
}
}
}
[input close];
NSLog(#"bytes write completed");
close(isocket_pasv);
}
close(isocket);
Any help Thanks in advance.
You can't guarantee you can offload your entire buffer in one go. You'll have to loop over write until all your data is transferred. You should also check for errors returned by write, it's likely to be the place where you catch all kinds of network related events as well as more typical O/S write errors.
I'm guessing it's the write call that doesn't send all you ask it to send? That's actually not unexpected. You simply have to send the remaining data in a new call to write.
If you want to write all data, use something like this:
int write_all(int fd, const void *buffer, const size_t bufsize)
{
size_t to_write = bufsize;
const void *ptr = buffer;
while (to_write > 0)
{
ssize_t written = write(fd, ptr, to_write);
if (written < 0)
return -1;
to_write -= written;
ptr = ((char *) ptr) + written;
}
return bufsize;
}
Caveat: I write the following from memory, haven't tested it.
i need to connect an imap email server with below given -(BOOL) method
how can i call this method in IBACTION when connect button clicked?
- (BOOL) connectToHost: (NSString*) hostname
{
socket_ = socket(AF_INET, SOCK_STREAM, 0);
if (socket_ < 0) {
NSLog(#"socket");
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(993);
struct hostent* host = gethostbyname([hostname UTF8String]);
unsigned int** ptr = (unsigned int **) host->h_addr_list;
while (*ptr != NULL) {
addr.sin_addr.s_addr = *(*ptr);
if (connect(socket_, (struct sockaddr *) &addr, sizeof(addr)) == 0) {
break;
}
ptr++;
}
if (*ptr == NULL) {
NSLog(#"connect");
}
return [[self readLine] isEqualToString: #"* OK"];
}
... call it from another method?
- (IBAction) connectButtonClicked:(id)sender {
[self connectToHost:#"Your host name goes here, variable _or_ constant! :D"];
}
im using LXsocket as my networking provider
i have setup a server and client for testing. i also have wireshark in place to see whats being sent
client:
socket = [[LXSocket alloc] init];
if ([socket connect: host port: port] == NO)
{
//cant connect!
[socket release];
NSLog(#"cannot create socket");
return FALSE;
}
//connect hex string being sent
const char connectByteArray[] = {
0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40
};
//send bytes this works and the packets shown in wireshark reflect the byte array above
[socket sendBytes:connectByteArray length:sizeof(connectByteArray)];
//receive bytes
id *object = [socket readBytesWithLength:sizeof(connectByteArray)];
Server:
LXSocket* serverSocket = [[LXSocket alloc] init];
[serverSocket bindToPort: 1234];
[serverSocket listen: 5];
LXSocket *socket = [serverSocket accept];
const char connectByteArray[] = {
0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40
};
[socket sendBytes:connectByteArray length:sizeof(connectByteArray)];
my problem is that i cannot get cocoa to export the byte array properly. i have tried many different format tags without any luck. how can i get this to log the proper byte array, ie. 01:00:07:7f:00:00:01:00:03:40 ????
NSLog(#"return data %X",object);
NSLog(#"return data %f",object);
NSLog(#"return data %e",object);
NSLog(#"return data %llu",object);
NSLog(#"return data %hu",object);
2011-08-24 08:24:44.507 Chameleon[5574:207] return data 4C053B0
2011-08-24 08:24:44.508 Chameleon[5574:207] return data 0.000000
2011-08-24 08:24:44.509 Chameleon[5574:207] return data 2.125934e-313
2011-08-24 08:24:44.510 Chameleon[5574:207] return data 43029386160
2011-08-24 08:24:44.511 Chameleon[5574:207] return data 21424
#import "LXSocket.h"
#import <sys/socket.h>
#import <sys/types.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netdb.h>
#implementation LXSocket
+ (id)socket {
return [[[LXSocket alloc] init] autorelease];
}
- (id)initWithCSocket:(SOCKET)_socket {
if ((self = [super init])) {
m_socket = _socket;
}
return self;
}
- (id)init {
if ((self = [super init])) {
m_socket = socket(AF_INET, SOCK_STREAM, 0);
}
return self;
}
- (BOOL)bindToPort:(unsigned)port {
struct sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;
service.sin_port = htons(port);
if (bind(m_socket, (struct sockaddr *) &service, sizeof(service)) == -1) {
NSLog(#"bind() failed -- %s\n", strerror(errno));
close(m_socket);
return NO;
} else return YES;
}
- (BOOL)listen:(unsigned)limit {
if (listen(m_socket, limit) == -1) {
printf("listen(): Error listening on socket\n");
return NO;
} else return YES;
}
- (LXSocket*)accept {
SOCKET AcceptSocket;
while (YES) {
AcceptSocket = -1;
while (AcceptSocket == -1) {
AcceptSocket = accept(m_socket, NULL, NULL);
}
//printf("Server: Client Connected!\n");
//m_socket = AcceptSocket;
return [[LXSocket alloc] initWithCSocket: AcceptSocket];
}
return nil;
}
- (BOOL)connect:(NSString*)host port:(unsigned)port {
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr([host UTF8String]);
clientService.sin_port = htons(port);
if (connect(m_socket, (struct sockaddr *) &clientService, sizeof(clientService)) == -1) {
printf("Client: connect() - Failed to connect.\n");
return NO;
} else return YES;
}
- (BOOL)sendBytes:(const void*)bytes length:(unsigned)len {
int sent = send(m_socket, bytes, len, 0);
return sent == len;
}
- (void*)readBytesWithLength:(unsigned)len {
void* buffer = malloc(len);
int received = recv(m_socket, buffer, len, 0);
if (received <= 0) {
free(buffer);
return NULL;
}
if (received < len) {
unsigned remaining = len;
char* ptr = buffer;
do {
remaining -= received;
ptr += received;
received = recv(m_socket, ptr, remaining, 0);
if (received <= 0) {
free(buffer);
return NULL;
}
} while (received < remaining);
}
return buffer;
}
- (BOOL)sendData:(NSData*)data {
const void* ptr = [data bytes];
BOOL succeeded = NO;
if ([self sendInt: [data length]])
if ([self sendBytes: ptr length: [data length]]) succeeded = YES;
//free(ptr);
return succeeded;
}
- (NSData*)readData{
unsigned len = [self readInt];
if (len > 0) {
void* ptr = [self readBytesWithLength: len];
if (ptr != NULL) {
NSData* data = [NSData dataWithBytes: ptr length: len];
free(ptr);
return data;
}
}
return nil;
}
- (BOOL)sendObject:(id)object {
NSMutableData* data = [[NSMutableData alloc] init];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
[archiver encodeRootObject: object];
[archiver finishEncoding];
BOOL succeeded = [self sendData: data];
[archiver release];
[data release];
return succeeded;
}
- (id)readObject {
NSData* data = [self readData];
if (data != nil) {
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
id object = [[[unarchiver decodeObject] retain] autorelease];
[unarchiver release];
return object;
}
return nil;
}
- (BOOL)sendInt:(int)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendDouble:(double)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendInt64:(long long)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (int)readInt {
void* buffer = [self readBytesWithLength: sizeof(int)];
if (buffer == NULL) return 0;
int n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (double)readDouble {
void* buffer = [self readBytesWithLength: sizeof(double)];
if (buffer == NULL) return 0;
double n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (long long)readInt64 {
void* buffer = [self readBytesWithLength: sizeof(long long)];
if (buffer == NULL) return 0;
long long n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (BOOL)sendShort:(short)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (short)readShort {
void* buffer = [self readBytesWithLength: sizeof(short)];
if (buffer == NULL) return 0;
short n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (BOOL)sendLong:(long)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendUnsingedLongLong:(unsigned long long)n {
return YES;//return [self sendBytes: &n length: sizeof(n)];
}
- (long)readLong {
void* buffer = [self readBytesWithLength: sizeof(long)];
if (buffer == NULL) return 0;
long n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (void)sendString:(NSString*)string {
const char* s = [string UTF8String];
int len = strlen(s);
[self sendInt: len];
[self sendBytes: s length: len + 1];
}
- (NSString*)readString {
int len = [self readInt];
char* s = [self readBytesWithLength: len + 1];
NSString* string = [NSString stringWithUTF8String: s];
free(s);
return string;
}
- (NSString*)resolveHostName:(NSString*)hostName {
struct hostent* host = gethostbyname([hostName UTF8String]);
if (host == NULL) {
printf("Error resolving host name: %d\n", strerror(errno));
return nil;
} else {
struct in_addr* addr = (struct in_addr*) host->h_addr_list[0];
char* ip = inet_ntoa(*addr);
return [NSString stringWithUTF8String: ip];
}
}
- (void)dealloc {
close(m_socket);
[super dealloc];
}
#end
Some further results (remember original hex string being passed is 0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40)(0100077f000001000340):
id *object = [socket readBytesWithLength:10];
NSLog(#"return data %X",*object); = return data 7F070001
NSLog(#"return data %llu",*object); = return data 45080838145
double object = [socket readInt64];
NSLog(#"return data %X",*object); = return data F0700010
NSLog(#"return data %llu",*object); = return data 4823355235012444176 (hex: 0x42F00007F0700010)
You probably have to write a special function to print in this way because you need to loop through the characters:
- (NSString *)bytesToString:(char *)bytes length:(int)length {
// Each byte is 2 hex characters, plus we need a ':' between each byte
NSMutableString *result = [NSMutableString stringWithCapacity:2*length + length - 1];
for (int i = 0; i < length; i++) {
if (i < length - 1) {
[result appendFormat:#"%02X:", bytes[i]];
}
else {
[result appendFormat:#"%02X", bytes[i]];
}
}
return result; // Result is auto-released
}
With this function defined, you should be able to log as follows:
NSLog(#"return data %#",[self bytesToString:object length:sizeof(connectByteArray)]);