AsyncUdpSocketの使い方を受け
-
26-09-2019 - |
質問
のようなプログラムのためのiPhoneでの走行をシミュレーション。私の問題を受けるUDPのデータです。使っていasyncUdpSocket.ばソケット用 sendData:(NSData) toHost:
,...ものです。
のえるのではないかと思っていけなので、受信機能です。
思うようになります:
socket = [[AsyncUdpSocket alloc] initWithDelegate:self];
[socket bindToPort:8000] error:nil] //returns YES
[socket receiveWithTimeout:-1 tag:1];
と思うのでそのメソッドを呼び出し -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long) fromHost:(NSString *)host port:(UInt16)port
やりたいのNSLogその方法、ということはありません。【ソケットの受け..]のみでの受取方法ないくらいだと思うからすると一---やはりもう一つの方法しています。やってくれる一部追加に私の委譲など...などしていますか
私はasyncUdpSocket例(s)、チュートリアル、(s)以上のものだけでな例です。なのでもう説明をしたりは知っても良い説明ができます。
ままにしておいてくださいえんか。
解決
私は新しい客観的なC(熊自分の知らないものができていくAsyncUdpSocketDelegate、届けすることができます。いくつかにされていないの確認:
を確認し
self
クラスで初期化してソケットの委譲のクラスにいることに期待してコールバックします。必ずクラスを採用してお
AsyncUdpSocketDelegate
プロトコルです。くなった場合には実際に必要となることができなかった傷ついてしまう。クラスヘッダは、次のように記述されています。@interface |your class| : |super class| <|Other protocol(s)|, AsyncUdpSocketDelegate> {
いただくには問の方法で宣言されてお
面実装されます。のメソッドシグニチャーはこのようになっているでしょう:- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port;
み呼び出し
receiveWithTimeout
非ゼロのタイムアウト値です。があります。利用Wiresharkめていることを確認してください 実際に受UDPのデータが す"という思いを大切にしています。ません しようと侮辱お知能 もっとはなかったのでしょうか?う バイスネットワークコードのバグ 過去の経験を行った 私のネットワークの設定をします。
他のヒント
AsyncUdpSocket *socket=[[AsyncUdpSocket alloc]initWithDelegate:self];
//receiveWithTimeout is necessary or you won't receive anything
[socket receiveWithTimeout:-1 tag:2]; //-------here
NSData *data=[@"Hello from iPhone" dataUsingEncoding:NSUTF8StringEncoding];
[socket sendData:data toHost:bchost port:9003 withTimeout:-1 tag:1];
これが役立つかどうかはわかりませんが、私は同じ問題を抱えていましたが、これがどのように修正されたかを示します。
私の場合、問題は次のとおりです。
[self.socket receiveWithTimeout:-1 tag:0];
「間違った」場所にありました。
あなたが電話するなら self.socket receivewithtimeout:-1タグ:0]; メソッドで didfinishlaunching withoptions, 、ソケットは、何をするかに関係なく機能しません(新しいスレッドで起動しても)。この問題を修正するために、ボタンを作成して移動しました receivewithtimeout ボタンがクリックされたときに呼び出されるメソッドを呼び出します。私の推測では、asyncudpsocketはスレッドの取り扱いについて何かが好きではないと思います didfinishlaunching withoptions.
以下に作業サンプルコードを投稿しました(Xcode 5.1.1を使用)。これらは、Xcodeプロジェクトの完全なAppDelegateファイルです。
appdelegate.h
#import <UIKit/UIKit.h>
#import "AsyncUdpSocket.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate, AsyncUdpSocketDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) AsyncUdpSocket *udpSocket;
@property (strong, nonatomic) UILabel *receiver;
@end
appdelegate.m
#import "AppDelegate.h"
#import "AsyncUdpSocket.h"
#import <UIKit/UIKit.h>
#import <CFNetwork/CFNetwork.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create the main window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Create a label for showing received text
self.receiver = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 80.0)];
self.receiver.text = @"No message, yet!";
self.receiver.textColor = [UIColor blackColor];
[self.window addSubview:self.receiver];
// Create a button for sending messages
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(80.0, 210.0, 160.0, 40.0)];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Start Game" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor blueColor]];
[self.window addSubview:button];
@try {
self.udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
if (![self.serverSocket bindToPort:9003 error:nil]) {
NSLog(@"COULD NOT BIND TO PORT");
}
if (![self.udpSocket enableBroadcast:YES error:nil]) {
NSLog(@"COULD NOT ENABLE BROADCASTING");
}
} @catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
return YES;
}
- (void)buttonClick:(UIButton*)button {
NSData * data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
[self.udpSocket receiveWithTimeout:-1 tag:0];
if (![self.udpSocket sendData:data toHost:@"127.0.0.1" port:9003 withTimeout:0.2 tag:1]) {
NSLog(@"COULD NOT SEND DATA");
} else {
NSLog(@"Sent packet (from %@:%d to 127.0.0.1:9001)", self.udpSocket.localHost, self.udpSocket.localPort);
}
}
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port {
NSLog(@" Received data (from %@:%d) - %@", host, port, data);
self.receiver.text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[self.udpSocket receiveWithTimeout:-1 tag:0];
return YES;
}
@end
これが誰かに役立つことを願っています。
私はこのライブラリに精通していませんが、Googleコードプロジェクトのサンプルコードを見ると、いくつかのことが明らかになります。
まず、あなたが説明するこのコールバックについて言及していません。あなたが実装することになっているように見えます:
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
また、この例では、サーバーは次の行で開始されます。
[listenSocket acceptOnPort:port error:&error]
これが次のリンクです サンプルコード.