GCDAsyncSocket - not receiving data - AsyncSocket is working fine

I made an iPhone client connect to the server using GCDAsyncSocket. The server runs .Net on a Windows server. The connection is good and it also sends data.

Then I give the client the opportunity to receive immediately after sending ...

[sock readDataToData:[GCDAsyncSocket LFData] withTimeout:15 tag:1]; 

I also have this setting for receiving:

 - (void)onSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag 

and:

  - (NSTimeInterval)socket:(GCDAsyncSocket *)sock shouldTimeoutReadWithTag:(long)tag 

If I wait for a timeout, the timeout method is called.

If I send data from the server, the timeout is not called, so I assume the client saw something, but there is no indication that the client side.

I also added:

 - (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength: (NSUInteger)partialLength tag:(long)tag 

I hope I see a partial package, but it will not be called or.

As I mentioned above, the timeout does not work if I send something from server to client. However, I would have thought it would also time out if he didn't get a character terminator. I also tried reading with a length of 3, but it didnt anyway.

GCDAsyncSocket problem. AsyncSocket is working fine.

Maybe its initiator is wrong?

dispatch_queue_t mainQueue = dispatch_get_main_queue ();

asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate: self delegateQueue: mainQueue]

Any ideas what I did wrong?

I put a message in goggle code for this, but there is no activity, so I'm not sure if I will get a response or not.

Ideally, if someone has an example of code that gets working, that would be great! thanks!

+6
source share
6 answers

Maybe something happened to the spelling. (It happened to me, and I looked for a watch until I saw a small difference)

GCDAsyncSocket renamed all delegate callbacks from onSocket to socket to match Apple's style name.

In the example above you mentioned

 - (void)onSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag 

rename it to

 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag 

This should solve this problem.

And in my code, I also had a different problem. Make sure that the object that calls GCDAsyncSocket is still alive while you are connecting. The GCDAsyncSockets delegate is a weak reference to your object. And since its an asynchronous process, the delegate can become null (auto-advertisement, or an instance was created inside a method that has already been left) while the socket is responding. As a result, it does not seem to be called, but the class that launched the socket is already freed from memory. This happens, especially if you use ARC with GCDAsyncSocket.

Hope this helps

+9
source

I had the same problem and was ready to give up. My problem was server side. In my delegate method onSocket: didAcceptNewSocket: I was causing read on the wrong socket. This solved it.

 -(void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket { NSLog(@"Connection Socket connected to ipad at %@",[newSocket connectedHost]); _ipadSocket = newSocket; [_ipadSocket setDelegate:self]; //***This code was the faulty line. I was calling listen on sock not newSocket [_ipadSocket readDataToData:[AsyncSocket CRLFData] withTimeout:10 tag:0]; } 
+3
source

Are you sure your server sends data completed only with LineFeed and not CRLF? Or perhaps the data that you send from your server does not end there? readDataToData will wait until it hits this trailing character in the received data.

Here is the code I use to connect. I removed what I think is not necessary - I hope I have not accepted anything important.

Init socket

 asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; 

Connect

 [asyncSocket connectToAddress:addr error:&err] 

When connected

 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port { connected = YES; 

Sending (no timeout)

 [asyncSocket writeData:dataData withTimeout:-1 tag:0]; 

Data Read Request

 [asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:30.0 tag:0]; 

The data has passed (I just send the rows)

 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
+2
source

I think the problem comes from what you submit. If the content contains LF somewhere in your file, reading ends when it reads LF. You must encode base64 before sending the file or reading to size.

+1
source

I developed almost the same code as the ios side client vb net server. From vb.net I am sending a file with 147 kb with metod socket.sendfile, while ios uses AsyncSocket, so:

 - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag; { [sock readDataToData:[AsyncSocket CRData] withTimeout:-1 tag:0]; [dataFile appendData:data]; } 

I have no problem connecting and starting send / receive, but my only problem is that I get 152 kb if I use CRData and 143 kb if I use LFData delimiter.

Answer: how to add a separator to vb net when sending a file?

0
source

Add:
[sock readDataWithTimeout: -1 tag: 0];

at the end of didConnectToHost.

 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { //Your Code [sock readDataWithTimeout:-1 tag:0]; } 
0
source

Source: https://habr.com/ru/post/891818/


All Articles