objective-c - iOS uses GCDAsyncSocket to establish a long connection to send messages
怪我咯
怪我咯 2017-05-02 09:30:53
0
1
767

iOS uses GCDAsyncSocket to establish a long connection to send messages. Why does GCDAsyncSocket need to be initialized once before sending a message? Is there something wrong with my code?

This is the button method to send a message. When I initialize GCDAsyncSocket and write to viewDidLoad, the server cannot receive the message

-(void)allPhotoAction:(UIButton *)btn{

//建立连接
NSString *host = @"192.168.0.199";
int port = 54111;
asyncsocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
asyncsocket.delegate = self;
NSError *error = nil;
if (![asyncsocket connectToHost:host onPort:port error:&error]) {
    //该方法异步
    GFFLog(@"%@",  @"连接服务器失败");
}

NSString *sendMessage = @"25";

[asyncsocket writeData:[sendMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];

}

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
给我你的怀抱

It is recommended to look at more official examples.

Call connectToHost The success returned does not mean that you are connected, it just means that there is nothing wrong with the host and port you entered. It has a callback method for successful connection, and call writeData after that method. To maintain a long connection, read must be called after each write/receive to keep the socket listening.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template