There is a code that establishes a connection at a given address to a given port, sends and receives a message from it.

addr.sin_family = AF_INET; addr.sin_port = htons([self.portField.stringValue intValue]); // или любой другой порт... addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 

In addr.sin_port I transfer the port number from the form. Also from the form I want to pass the address to addr.sin_addr.s_addr.

I tried to do in two ways.

 addr.sin_addr.s_addr = inet_addr([self.portField.stringValue cStringUsingEncoding:NSUTF8StringEncoding]); 

and

 addr.sin_addr.s_addr = inet_addr([self.portField.stringValue UTF8String]); 

But none of these methods works. Tell me how to properly pass a string to a parameter?

Update

Indeed, the problem was with the strings. The following solution helped:

 addr.sin_port = htons([ports integerValue]); // или любой другой порт... addr.sin_addr.s_addr = inet_addr([[NSString stringWithFormat:@"%@", adr]UTF8String]); 

    1 answer 1

    I think the fact is that we should not take self.portField.stringValue, but self.portField.text

    In general, it was possible to use the wrapper on obj-c https://github.com/robbiehanson/CocoaAsyncSocket

    Update

    https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Intro_GCDAsyncSocket - here is an example. In words, I can say that I used it exactly to catch multicast packages and everything was fine except for the fact that the Aipad network card is cut down after 5 minutes, but when working directly the same.

    By the way, when we thought that the problem with multicast because of the implementation wrote their version of the socket and there we used sockAddr.sin_addr.s_addr = inet_addr([group UTF8String]); where group is NSString and our port was declared as NSInteger , so in general it’s like yours. Maybe not in this case?

    • So there is no self.portField.text. I do not write under iOS, but under OSX. - maxprig
    • so you did not specify it anywhere) - aknew
    • Do you have a sample code using this wrapper?)) - maxprig
    • Unfortunately no. I used it only on iOS - aknew
    • Well, at least for iOS, there is not much difference. - maxprig