본문으로 바로가기

iOS 소켓통신 참고

category [ 모바일 관련 ]/아이폰(iOS) 2018. 11. 5. 17:14




데이터 형 자료형에 데이터 삽입

NSMutableData* bufferToSend;
bufferToSend = [[NSMutableData alloc] initWithCapacity:0];

NSData *dataFromTextField = [[textfield1 text] dataUsingEncoding:NSUTF8StringEncoding];
[bufferToSend appendData:dataFromTextField];


=======================



문자열 -> 바이트 변경

NSString *test = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

NSData *bytes = [test dataUsingEncoding:NSUTF8StringEncoding];
NSString *test=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSUInteger bytes = [test lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%i bytes", bytes);


데이터 형변환 참고

http://savinglove.tistory.com/24

http://dolfalf.tistory.com/99


=======================


엔디안 변경시 참조 (iOS는 기본적으로 little endian : http://bartysways.net/?p=499)


// Bytes to uint32_t 
NSData *data = <THE_DATA>; 
void *bytes = [data bytes]; 
uint32_t *intBytes = (NSInteger*)bytes; 
uint32_t swapped = CFSwapInt32BigToHost(*intBytes); ///< If the data in `data' is big endian 

// uint32_t to bytes 
uint32_t someInt = 1234; 
uint32_t swappedInt = CFSwapInt32HostToBig(someInt); ///< If we want to store in big endian 
NSData *data = [NSData dataWithBytes:&swappedInt length:sizeof(swappedInt)]; 


https://stackoverrun.com/ko/q/2217051



uint64_t myInt = 42;

uint64_t netInt = CFSwapInt64HostToLittle(myInt); // use this for little endian
uint64_t netInt = CFSwapInt64HostToBig(myInt);    // use this for big endian

[outputStream write:(uint8_t*)&netInt maxLength:sizeof(netInt)];


https://stackoverflow.com/questions/3239284/writing-64-bit-int-value-to-nsoutputstream/3239366



======================



바이트 오더 유틸리티


https://developer.apple.com/documentation/corefoundation/byte-order_utilities?language=objc



======================


정리가 잘된 샘플코드

https://gist.github.com/rjungemann/446256

https://blog.naver.com/kenny817/20154825792




https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html

https://stackoverflow.com/questions/19003063/ios-simple-tcp-connection-example

http://hamait.tistory.com/702

https://effectivecode.tistory.com/690

http://downman.tistory.com/47

http://devzzing.tistory.com/3




=====================


샘플


char bytes1[] = "value1";

char bytes2[] = "value2";

char bytes3[] = "value3";

    

    

NSMutableData *myData = [NSMutableData data];

//myData = [[NSMutableData alloc] initWithCapacity:0]; //제외

    

short s1 = 0x29; //서버와의 규약에 따른 바이트 수 (크기 2바이트)

[myData appendBytes:&s1 length:sizeof(s1)];

    

short s2 = 0x6021; //서버와의 규약에 따른 프로토콜 (크기 2바이트)

[myData appendBytes:&s2 length:sizeof(s2)];

    

NSData *data1 = [NSData dataWithBytes:bytes1 length:7];

[myData appendData:data1];

    

NSData *data2 = [NSData dataWithBytes:bytes2 length:9];

[myData appendData:data2];

    

NSData *data3 = [NSData dataWithBytes:bytes3 length:21];

[myData appendData:data3];


NSInteger i = [outputStream write:[myData bytes] maxLength:myData.length];


NSLog(@"보낸 바이트:%ld", (long)i);

NSLog(@"outputStream:%@", outputStream);