从 UDP 服务器消息中检索字符串 [英] Retrieving a string from a UDP server message

查看:23
本文介绍了从 UDP 服务器消息中检索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于 Swift 的 iOS 应用中使用了 CocoaAsyncSocket 库.我已经为网络上的 UDP 服务器创建了一个异步 UDP 套接字,它会发回回复.

我正在阅读这个回复:

func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress 地址: NSData!, withFilterContext filterContext: AnyObject!) {println("didReceiveData")让数据=地址var 主机:NSString?var port1: UInt16 = 0GCDAsyncUdpSocket.getHost(&host, port: &port1, fromAddress: address)println("来自 \(host!)")let gotdata = NSString(data: data, encoding: NSASCIIStringEncoding)//let gotdata: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)打印(得到数据)}

结果如下:

didReceiveData从 192.168.1.114wþÀ¨r

如您所见,这是一堆奇怪的字符.

原始数据为<100277fe c0a80172 00000000 00000000>(通过编写println(data)).这里前 4 个字节似乎是 wþÀ¨r 部分,接下来的 4 个字节是 IP 地址,我已经从 getHost() 函数中检索到了.>

我似乎找不到任何帮助函数来提取 UDP 服务器接收到的实际数据.我知道 UDP 回复的 ASCII 表示是你好".但是,如果我将其更改为其他内容,则不会反映在我在 Xcode 调试部分中看到的数据中.总是 wþÀ¨r.

我已经尝试过 NSASCIIStringEncodingNSUTF8StringEncoding,但后者导致 EXC_BAD_ACCESS 运行时异常.

据我所知,这就是人们通常在 Objective-C 中的做法(在 didReceiveData 函数内):

NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

如何在 UDP 回复中找到hello"消息作为字符串表示?

解决方案

相当愚蠢,但是从 didReceiveData 数据:NSData! 部分删除 ! !code>udpSocket 函数解决了这个问题.

也就是说,函数应该这样定义:

func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData, fromAddress 地址: NSData!, withFilterContext filterContext: AnyObject!) {....}

I'm using the CocoaAsyncSocket library in my Swift-based iOS app. I have created an async UDP socket to a UDP server on my network, and it sends back a reply.

I'm reading this reply like this:

func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) { 
    println("didReceiveData")
    let data = address

    var host: NSString?
    var port1: UInt16 = 0
    GCDAsyncUdpSocket.getHost(&host, port: &port1, fromAddress: address)
    println("From \(host!)")

    let gotdata = NSString(data: data, encoding: NSASCIIStringEncoding)
    //let gotdata: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)
    println(gotdata)
}

This is the result:

didReceiveData
From 192.168.1.114
wþÀ¨r

As you can see, it's a bunch of strange characters.

The raw data is <100277fe c0a80172 00000000 00000000> (by writing println(data)). Here the first 4 bytes seem to be the wþÀ¨r part, and the next 4 are the IP address, which I already retrived from the getHost() function.

I can't seem to find any helper function to extract the actual data received by the UDP server. I know that the ASCII representation of the UDP reply is "hello". However, if I change this to something else, it is not reflected in the data I see in Xcode's debug part. It is always wþÀ¨r.

I have tried both with NSASCIIStringEncoding and NSUTF8StringEncoding, but the latter results in a EXC_BAD_ACCESS runtime exception.

As far as I could find, this is how people normally do it in Objective-C (inside the didReceiveData function):

NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

How do I find the "hello" message in the UDP reply as a string representation?

解决方案

It is rather stupid, but removing ! from the didReceiveData data: NSData! part of the udpSocket function solved the issue.

That is, the function should be defined like this:

func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {
    ....
}

这篇关于从 UDP 服务器消息中检索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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