Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCDAsyncSocket server receive data only first time

Client sent message every time when I press send button but Server receive message only first time. What is the issue in server

Server:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    NSError *err = nil;
    if (![asyncSocket acceptOnPort:10000 error:&err]){

        NSLog(@"Error in acceptOnPort:error: -> %@", err);

    }
}

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);

    self.asyncSocket = newSocket;
    NSString *welcomMessage = @"Hello from the server\r\n";
    [self.asyncSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];

    [self.asyncSocket readDataWithTimeout:-1 tag:0];

}

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

    NSLog(@"MSG: %@",msg);

}

Client:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

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

}

-(IBAction)connectToServer {
    NSError *err = nil;
    if (![socket connectToHost:self.txtIp.text onPort:10000 error:&err]) // Asynchronous!
    {
        // If there was an error, it's likely something like "already connected" or "no delegate set"
        NSLog(@"I goofed: %@", err);
        return;
    }
}

- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"Cool, I'm connected! That was easy.");

     [socket readDataWithTimeout:-1 tag:0];
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    if (tag == 1)
        NSLog(@"First request sent");
    else if (tag == 2)
        NSLog(@"Second request sent");
}

- (void)socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
{
    NSLog(@"Received Data: %@",data);
}


-(void)sendMessage {

    NSData *msg = [self.txtMsg.text dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"Data Send: %@",msg);

   [socket writeData:msg withTimeout:-1 tag:1];

}
like image 661
MaxEcho Avatar asked Sep 11 '14 11:09

MaxEcho


People also ask

How does gcdasyncudpsocket work?

GCDAsyncUdpSocket @interfaceGCDAsyncUdpSocket:NSObject GCDAsyncUdpSocket uses the standard delegate paradigm, but executes all delegate callbacks on a given delegate dispatch queue. This allows for maximum concurrency, while at the same time providing easy thread safety.

How to create a socket queue for gcdasyncsocket?

The socket queue is optional. If you pass NULL, GCDAsyncSocket will automatically create its own socket queue. If you choose to provide a socket queue, the socket queue must not be a concurrent queue, then please see the discussion for the method markSocketQueueTargetQueue.

How does gcdasyncsocket maintain thread safety?

-markSocketQueueTargetQueue: GCDAsyncSocket maintains thread safety by using an internal serial dispatch_queue. In most cases, the instance creates this queue itself. However, to allow for maximum flexibility, the internal queue may be passed in the init method.

Is it safe to alter the data sent by gcdasyncudpsocket?

the socket is sending it. In other words, it’s not safe to alter the data until after the delegate method udpSocket:didSendDataWithTag: or udpSocket:didNotSendDataWithTag:dueToError: is invoked signifying that this particular send operation has completed. This is due to the fact that GCDAsyncUdpSocket does NOT copy the data.


1 Answers

You have to make a read call from your server class in didReadData: delegate. Rest is fine. Use below code.

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

   [sock readDataWithTimeout:-1 tag:0];

    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"MSG: %@",msg);
}
like image 81
Gandalf Avatar answered Nov 15 '22 07:11

Gandalf