ios - SmallSocket causing app to crash on iPhone -
i trying use smallsockets library create tcp socket connection. currently, i'm using button test connection server iphone. here code looks like:
-(ibaction)btnconnect:(id)sender { bool loopconnection = true; while(loopconnection == true) { socket *socket; int port = 11005; nsstring *host = @"199.5.83.63"; socket = [socket socket]; @try { nsmutabledata *data; [socket connecttohostname:host port:port]; [socket readdata:data]; // [socket writestring:@"hello world!"]; //** connection successful **// [socket retain]; // must retain if want use out of action block. } @catch (nsexception* exception) { nsstring *errmsg = [nsstring stringwithformat:@"%@",[exception reason]]; nslog(errmsg); socket = nil; } } } when press button, app freezes. freezes on following function (which part of smallsockets library):
- (int)readdata:(nsmutabledata*)data // // append available data socket supplied buffer. // returns number of bytes received. (may 0) // { ssize_t count; // data must not null ptr if ( data == null ) [nsexception raise:socket_ex_invalid_buffer format:socket_ex_invalid_buffer]; // socket must created , connected if ( socketfd == socket_invalid_descriptor ) [nsexception raise:socket_ex_bad_socket_descriptor format:socket_ex_bad_socket_descriptor]; if ( !connected ) [nsexception raise:socket_ex_not_connected format:socket_ex_not_connected]; // request read of as can. should return if no data. count = recv(socketfd, readbuffer, readbuffersize, 0); if ( count > 0 ) { // got data, append user's buffer [data appendbytes:readbuffer length:count]; } else if ( count == 0 ) { // other side has disconnected, close down our socket [self close]; } else if ( count < 0 ) { // recv() returned error. if ( errno == eagain ) { // no data available read (and socket non-blocking) count = 0; } else [nsexception raise:socket_ex_recv_failed format:socket_ex_recv_failed_f, strerror(errno)]; } return count; } the app freezes on line: [data appendbytes:readbuffer length:count]; , says on same line: thread 1:exc_bad_access (code=1, address=0x5d18c48b). see in output (lldb) in green letters. client connects, server sends 4 byte packet client.
if shed light why app crashing here, i'd appreciate it. i've been banging head couple hours trying figure out issue is.
thanks!
i don't know if right answer or not, first thing retain socket right away. if have problem socket, release in exception handler.
Comments
Post a Comment