I
I
iogurt892016-03-02 13:46:10
Objective-C
iogurt89, 2016-03-02 13:46:10

CFSocket! Why is the data not coming?

Good afternoon. Understanding CFSocket. And when you try to transfer data from one socket to another, for some reason they are not transferred. It seems like I'm doing everything right. I read books, watched lectures, read articles on this topic. But so far I can't figure out what's going on. Please help me figure it out. Code below.
client

CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, AF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketNoCallBack, NULL, NULL);
struct sockaddr_in addressSend;
    memset(&addressSend, 0, sizeof(addressSend));
    addressSend.sin_len = sizeof(addressSend);
    addressSend.sin_family = AF_INET;
    addressSend.sin_addr.s_addr = inet_addr("127.0.0.1");
    addressSend.sin_port = htons(8908);
    CFDataRef addressSendData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&addressSend, sizeof(addressSend));

NSString *dataString = @"Hello World";
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    CFDataRef sendData = CFDataCreate(kCFAllocatorDefault, [data bytes], data.length);

CFTimeInterval timeOut = 5.0;
    CFSocketError errorConnect = CFSocketConnectToAddress(socket, addressSendData, timeOut);
    if (errorConnect != kCFSocketSuccess) {
        NSLog(@"Connect error");
    }
    CFSocketError errorSendData = CFSocketSendData(socket, addressSendData, sendData, timeOut);
    if (errorSendData != kCFSocketSuccess) {
        NSLog(@"Send data error");
    }

CFRelease(addressSendData);
CFRelease(sendData);

server
CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, AF_INET, SOCK_STREAM, IPPROTO_TCP,
                            kCFSocketDataCallBack, TCPServerAcceptCallBack, NULL);
    int reuse = true;
    int fileDescriptor = CFSocketGetNative(socket);
    if (setsockopt(fileDescriptor, SOL_SOCKET, SO_REUSEADDR,
                   (void *)&reuse, sizeof(int)) != 0) {
        NSLog(@"Error");
        return;
    }

struct sockaddr_in address;
    memset(&address, 0, sizeof(address));
    address.sin_len = sizeof(address);
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8908);
CFDataRef addressData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&address, sizeof(address));

CFSocketSetAddress(socket, addressData);
    
CFRunLoopSourceRef socketsource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), socketsource, kCFRunLoopDefaultMode);

CFRelease(addressData);

//-----------------------------------------------------------------------------------------------------------------
// Обработка событий сокета!
 void TCPServerAcceptCallBack(CFSocketRef socket,
                                    CFSocketCallBackType type,
                                    CFDataRef address,
                                    const void *data,
                                    void *info) {
    switch (type) {
        case kCFSocketAcceptCallBack:
            NSLog(@"Connection");
            break;
        case kCFSocketDataCallBack:
            NSLog(@"Data %@",  data); //Тут я ожидаю что в консоль выведется строка в бинарном виде, которую я передал. 
//но в консоли вижу это "Data <>"
            break;  
        default:
            break;
    }

To everyone who answers in advance, many thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rettpop, 2016-03-10
@rettpop

Until mankind teaches extrasensory perception at school, logging is the most important of the arts (c) VIL.
We need logs from both the client and the server. What is being sent, what is being received, is it connecting, is the port being bound...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question