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];
}