CocoaAsyncSocket 的读写委托没有触发并且没有触发。代码组织

发布于 2024-11-16 06:46:19 字数 2819 浏览 2 评论 0原文

我正在尝试使用 cocoaasyncsocket 库附带的 echo 服务器示例的修改版本执行以下操作:

1)打开与充当服务器的 python 脚本的连接
2) 发送数据 // 有效,但委托不触发
3) 接收返回数据 // 委托不会触发
4)断开//不会断开连接,显然仍在我的线程中

当前我在didFinishLaunchingWithOptions委托中打开一个连接,然后尝试在didConnectToHost委托中发送数据。然后我尝试读取从客户端返回的数据,然后断开连接。

我能够打开连接并发送数据(服务器验证是否已收到),但 didWriteDataWithTag 委托永远不会触发。然而,服务器接收到数据。然后服务器会触发一些数据,但 didReadData 也不会触发。

除了读/写委托没有触发这一事实之外,我组织代码的方式似乎不正确,但我不确定这在事件驱动系统中与运行循环相反(我'我是事件驱动的东西+网络的新手)。如果我有一系列操作,其各自的完成由其代表触发,那么代表是否应该共享某种消息 - 即我们收到“xxx”消息,写回“yyy”?我更希望有一个功能来管理所有这一切。有没有规范的方法来做到这一点?

IPhoneConnectTestAppDelegate.m(片段)

- (void)localConnect {
    NSError *error = nil;
    if (![asyncSocket connectToHost:@"localhost" onPort:5000 error:&error]) {
        DDLogError(@"Error connecting: %@", error);
    }
}

- (void)disconnect {
    [asyncSocket setDelegate:nil];
    [asyncSocket disconnect];
    [asyncSocket release];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Setup our socket (GCDAsyncSocket).
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];

    [self localConnect];

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)onSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    NSString *output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"didReadData: %@", output);

}

- (void)onSocket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"didWriteDataWithTag");
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
    NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    if(port == 5000)
    {
        NSString *msg = @"q";
        NSData *dataOut = [msg dataUsingEncoding:NSASCIIStringEncoding];
        [asyncSocket writeData:dataOut withTimeout:-1 tag:0];
        [asyncSocket readDataWithTimeout:-1 tag:0];
        [self disconnect];
    }
}

tcpserver.py

# TCP server example
import socket, time
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)

print "TCPServer Waiting for client on port 5000"

while 1:
    client_socket, address = server_socket.accept()
    print "I got a connection from ", address
    while 1:
        data = client_socket.recv(512)
        print "Data from client",data

        time.sleep(2)
        data = "xxx"
        print "Sending data to client",data
        client_socket.send (data)
        break;

I'm trying to do the following with a modified version of the echo server example that comes with the cocoaasyncsocket library:

1) open a connection to a python script acting as a server
2) send data // works, but delegate doesn't fire
3) receive data back // delegate doesn't fire
4) disconnect // doesn't disconnect, apparently still in my thread

Currently I open a connection in the didFinishLaunchingWithOptions delegate, and then attempt to send data in the didConnectToHost delegate. I then attempt to read data coming back from the client and then disconnect.

I am able to open a connection and send data (which the server verifies as received) but the didWriteDataWithTag delegate never fires. However, the server receive the data. The server then fires back some data, but the didReadData doesn't fire either.

Beside the fact the read/write delegates aren't firing, it seems the way I'm organizing my code is not right, but I'm not sure how this looks in an event-driven system as opposed to run loop (I'm a novice at event-driven stuff + networking). If I have a series of actions whose respective completions are triggered by their delegates, should the delegates be sharing some sort of messages- ie we recieved an "xxx" message, write back "yyy"? I'd prefer to have one function which manages all of this. Is there a canonical way of doing this?

IPhoneConnectTestAppDelegate.m (snippets)

- (void)localConnect {
    NSError *error = nil;
    if (![asyncSocket connectToHost:@"localhost" onPort:5000 error:&error]) {
        DDLogError(@"Error connecting: %@", error);
    }
}

- (void)disconnect {
    [asyncSocket setDelegate:nil];
    [asyncSocket disconnect];
    [asyncSocket release];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Setup our socket (GCDAsyncSocket).
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];

    [self localConnect];

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)onSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    NSString *output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"didReadData: %@", output);

}

- (void)onSocket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"didWriteDataWithTag");
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
    NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    if(port == 5000)
    {
        NSString *msg = @"q";
        NSData *dataOut = [msg dataUsingEncoding:NSASCIIStringEncoding];
        [asyncSocket writeData:dataOut withTimeout:-1 tag:0];
        [asyncSocket readDataWithTimeout:-1 tag:0];
        [self disconnect];
    }
}

tcpserver.py

# TCP server example
import socket, time
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)

print "TCPServer Waiting for client on port 5000"

while 1:
    client_socket, address = server_socket.accept()
    print "I got a connection from ", address
    while 1:
        data = client_socket.recv(512)
        print "Data from client",data

        time.sleep(2)
        data = "xxx"
        print "Sending data to client",data
        client_socket.send (data)
        break;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终不够爱げ你 2024-11-23 06:46:19

我知道这是一个老问题,答案已经被接受,但为了向发现此线程寻找某些东西的人澄清,委托方法没有被调用的原因是因为 GCDAsynchSocket 以 socket: 而不是 onsocket: 即:

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag

变为:

- (void) socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag

I know this is an old question with an already accepted answer, but to clarify for people who find this thread looking for something, the reason the delegate methods didn't get called is because the GCDAsynchSocket start with socket: instead of onsocket: ie:

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag

becomes:

- (void) socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
一片旧的回忆 2024-11-23 06:46:19

一个朋友的朋友帮我解决了这个问题!我们无法让 GCDAsyncSocket 正常工作(连接和写入,但无法读取)。然而,AsyncSocket 在所有 3 个方面都起作用,并且所有委托都正常工作。

#import "AsyncSocket.h"
#import "tcp_clientViewController.h"

@implementation tcp_clientViewController

@synthesize socket;



-(IBAction)connect:(id)sender { 
    NSLog(@"(IBAction)connect");
    NSError *error = nil;
    if (![socket connectToHost:@"localhost" onPort:5000 error:&error]){
        NSLog(@"Error connecting: %@", error);
    }
}

-(IBAction)send:(id)sender {
    NSLog(@"(IBAction)send");


    char bytes[] = "abcd\r\n";
    NSData* data = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)];

    //NSString *msg = @"xxxxx\r\n";
    //NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];


    [socket writeData:data withTimeout:-1 tag:0];
    //NSData *data = [asyncSocket readDataWithTimeout:-1 tag:0];
    [data release];

    [socket readDataToData:[AsyncSocket LFData] withTimeout:-1 tag:0];
}

- (void)viewDidLoad {
    // initialize socket
    socket = [[AsyncSocket alloc] initWithDelegate:self];
}

#pragma mark AsyncSocket Delegate Methods
-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"socket:%p didWriteDataWithTag:%@", sock, tag);
}

- (void)socket:(AsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
    NSLog(@"socket:%p didWritePartialDataOfLength:%@ tag:%@", sock, partialLength, tag);
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
}

- (void)socketDidSecure:(AsyncSocket *)sock
{
    NSLog(@"socket:%p socketDidSecure", sock);
}

- (void)socketDidDisconnect:(AsyncSocket *)sock withError:(NSError *)err
{
    NSLog(@"socket:%p socketDidDisconnect withError: %@", sock, err);
}

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSString* newStr = [NSString stringWithUTF8String:[data bytes]];    
    NSLog(@"socket socketDidReadData:%@", newStr);
}

-(IBAction)disconnect:(id)sender { }

#pragma mark View stuff
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {}

- (void)dealloc {
    self.socket = nil;
    [super dealloc];
}

@end

A friend of a friend figured it out for me! We were not able to get GCDAsyncSocket to work properly (connect and write, but not read). AsyncSocket however functions in all 3 respects, and all the delegates work properly.

#import "AsyncSocket.h"
#import "tcp_clientViewController.h"

@implementation tcp_clientViewController

@synthesize socket;



-(IBAction)connect:(id)sender { 
    NSLog(@"(IBAction)connect");
    NSError *error = nil;
    if (![socket connectToHost:@"localhost" onPort:5000 error:&error]){
        NSLog(@"Error connecting: %@", error);
    }
}

-(IBAction)send:(id)sender {
    NSLog(@"(IBAction)send");


    char bytes[] = "abcd\r\n";
    NSData* data = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)];

    //NSString *msg = @"xxxxx\r\n";
    //NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];


    [socket writeData:data withTimeout:-1 tag:0];
    //NSData *data = [asyncSocket readDataWithTimeout:-1 tag:0];
    [data release];

    [socket readDataToData:[AsyncSocket LFData] withTimeout:-1 tag:0];
}

- (void)viewDidLoad {
    // initialize socket
    socket = [[AsyncSocket alloc] initWithDelegate:self];
}

#pragma mark AsyncSocket Delegate Methods
-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"socket:%p didWriteDataWithTag:%@", sock, tag);
}

- (void)socket:(AsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
    NSLog(@"socket:%p didWritePartialDataOfLength:%@ tag:%@", sock, partialLength, tag);
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
}

- (void)socketDidSecure:(AsyncSocket *)sock
{
    NSLog(@"socket:%p socketDidSecure", sock);
}

- (void)socketDidDisconnect:(AsyncSocket *)sock withError:(NSError *)err
{
    NSLog(@"socket:%p socketDidDisconnect withError: %@", sock, err);
}

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSString* newStr = [NSString stringWithUTF8String:[data bytes]];    
    NSLog(@"socket socketDidReadData:%@", newStr);
}

-(IBAction)disconnect:(id)sender { }

#pragma mark View stuff
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {}

- (void)dealloc {
    self.socket = nil;
    [super dealloc];
}

@end
迷爱 2024-11-23 06:46:19

尝试使用本地sock来读取命令并将它们放入写入命令中

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    [sock readDataToData:[AsyncSocket LFData] withTimeout:-1 tag:tag];
}

Try to use local sock for reading commands and to put them in the write command

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    [sock readDataToData:[AsyncSocket LFData] withTimeout:-1 tag:tag];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文