Prerequisites: I made a UDP based IPhone application. It works as a client and server when it communicates with a Linux server [Ubuntu]. Im using GCDAsyncUdpSocket to send and receive data. I also have a backup server / client application on my Macbook to check / verify socket communication application. It just acts as a Linux server [Ubuntu]. It works great.
PROBLEM: I cannot get any data from the Linux server [Ubuntu].
DETAILS: I can successfully send data to a Linux server and it responds / processes this data. But when the server sends responses or echoes, my iPhone application does not see / read. A similar application developed by me with an Android colleague reads / sees data from the server. See code below. Hooray!
This is the code for it: .m file:
-(void)viewDidLoad
{
[super viewDidLoad];
backGround.userInteractionEnabled=YES;
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
if (![udpSocket bindToPort:0 error:&error])
{
return;
}
if (![udpSocket beginReceiving:&error])
{
return;
}
}
- (IBAction)sendData:(id)sender
{
NSString *msg = messageField.text;
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[self sendingRealData:data];
}
-(void)sendingRealData:(NSData *) Content
{
NSString *msg = [[NSString alloc] initWithData:Content encoding:NSUTF8StringEncoding];
NSLog(@"msg is: %@", msg);
NSString *host = [self getHost];
int port = [self getPort];
if ((port == 65536) && (host == @"-1"))
{
NSLog(@"Port and IP are not filled in!");
[self errorManag:2];
}
else if ((port == 65536) && (host != @"-1"))
{
NSLog(@"return was -2");
[self errorManag:1];
}
else if (host == @"-1")
{
NSLog(@"return was -1");
[self errorManag:0];
}
else
{
[udpSocket sendData:Content toHost:host port:port withTimeout:1 tag:tag];
NSLog(@"Gestuurd");
}
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
NSLog(@"niets.");
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (msg)
{
NSLog(@"iets gekregen");
}
else
{
NSLog(@"Error convertion");
}
NSLog(@"HMMMM");
messageField.text = [[NSString alloc] initWithFormat:@"RE: %@", msg];
}
source
share