在iOS应用程序从UDP服务器接收数据不是从Linux服务器,但工作从macbook pro [英] Receiving data from UDP server on iOS app is not working from Linux server, but works from macbook pro

查看:282
本文介绍了在iOS应用程序从UDP服务器接收数据不是从Linux服务器,但工作从macbook pro的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我做了一个基于UDP的IPhone套接字应用程序。当它与Linux [Ubuntu]服务器通信时,它作为客户端和服务器工作。
我使用GCDAsyncUdpSocket发送和接收数据。我还在我的Macbook上有一个备份服务器/客户端应用程序来测试/验证套接字通信应用程序。它只是像Linux [Ubuntu]服务器。它工作完美。

BACKGROUND: I made an IPhone socket application based on UDP. It works as a client and server while it communicates with a Linux [Ubuntu] server. I’m using a GCDAsyncUdpSocket to send and receive data. I also have a back-up server/client application on my Macbook to test/verify the socket communication application. It simply acts like the Linux [Ubuntu] server. It works perfect.

问题:我无法从Linux [Ubuntu]服务器接收任何数据。

PROBLEM: I can’t receive any data from the Linux [Ubuntu] server.

详细信息:我可以成功地将数据发送到Linux服务器,并对该数据做出反应/处理。但是当服务器发送回复或回声时,我的iPhone应用程序无法查看/读取它。
我为Android开发的一个类似应用程序不能从服务器读取/查看数据。
请参阅下面的代码。干杯!

DETAILS: I can successfully send data to the Linux server and it reacts/processes upon that data. But when the server sends replies or echoes, my iPhone app can't see/read it. A similar application I developed with a colleague for Android does read/see data from the server. Please see the code below. Cheers!

这是它的代码:
.m文件:

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]) //check ff of dit werkt!
    {
        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");
    //[self logError:@"Error converting received data into UTF-8 String"];
}
//NSString *test = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(@"%@",test); 
//[udpSocket sendData:data toAddress:address withTimeout:-1 tag:0];
    NSLog(@"HMMMM");
    messageField.text = [[NSString alloc] initWithFormat:@"RE: %@", msg]; 
}


推荐答案

写在ViewDidLoad中绑定到端口:0,因为这个通知是给我的。

I found out that I had written in the ViewDidLoad to bind to port: 0, because this advice was given to me before.

这导致移动设备侦听随机端口,而不是端口12345.将绑定更改为端口12345并起作用!

This caused the mobile to listen to a random port instead of port 12345. Changing the binding to port 12345 and works!

这篇关于在iOS应用程序从UDP服务器接收数据不是从Linux服务器,但工作从macbook pro的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆