开发者

How to send a veriable in a string to server?

开发者 https://www.devze.com 2023-02-24 12:31 出处:网络
I am communicating to a server on socket via TCP connection in an iPhone application. I have two text fields uTextField (for Username) & pTextField (for password) and now I want to send this reque

I am communicating to a server on socket via TCP connection in an iPhone application. I have two text fields uTextField (for Username) & pTextField (for password) and now I want to send this request to the server. can any body 开发者_运维百科tell me how can I pass the value of my text fields? here plz check my code:

UInt8 message[] = "LOGN|rms01,123456|<END>";
                CFDataRef data = CFDataCreate(NULL, message, sizeof(message));
                CFSocketSendData(s, NULL, data, 0);
                CFRelease(data);
                NSLog(@"msg sent");

Note: My code is working good as it is, issue is to pass the values of my text fields instead of hard code. here rms01 is username and 123456 is a password LOGN is login identifier & to tell server that query end.

I know this is a stupid question but I am new in iPhone development so please help me

Thanks in advance


NSString can do most of the work for you with stringWithFormat: and dataUsingEncoding::

NSString *username = @"username";
NSString *password = @"password";

NSString *message = [NSString stringWithFormat:@"LOGN|%@,%@|<END>", username, password];
CFDataRef messageData = (CFDataRef)[message dataUsingEncoding:NSUTF8StringEncoding];

CFSocketSendData(s, NULL, messageData, 0);

Note that you do not need to release messageData.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号