简体   繁体   English

iOS上的Objective-C套接字发送和接收

[英]Objective-C Sockets Send and Receive on iOS

I'm very new to Objective-C and would like to communicate between an iOS app which I'm working on and my Python 3 socket server (which works). 我是Objective-C的新手,并且想在我正在使用的iOS应用程序和Python 3套接字服务器(可以工作)之间进行通信。 The problem is I don't know how to use sockets in Objective-C and don't know where to start when installing libraries in Xcode 8. For now I just want to be able to send data to the server and receive a response back on the iOS device. 问题是我不知道如何在Objective-C中使用套接字,也不知道在Xcode 8中安装库时从哪里开始。现在,我只想能够将数据发送到服务器并收到响应在iOS设备上。 I have seen sys.socket.h but again, I don't know how to use it, any help would be much appreciated. 我已经看过sys.socket.h了,但是再次,我不知道如何使用它,任何帮助将不胜感激。

Thanks! 谢谢!

Few years ago I used SocketRocket . 几年前,我使用了SocketRocket I am posting some of the code of it from my old project. 我正在从我的旧项目中发布一些代码。 I don't know if that still works but you might get the idea of using it. 我不知道这是否仍然有效,但是您可能会想到使用它的想法。

  1. Connecting to server 连接到服务器

NSMutableURLRequest *pushServerRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"ws://192.168.1.1"]]; [pushServerRequest setValue:@"WebSocket" forHTTPHeaderField:@"Upgrade"]; [pushServerRequest setValue:@"Upgrade" forHTTPHeaderField:@"Connection"]; [pushServerRequest setValue:"somekey" forHTTPHeaderField:@"Sec-WebSocket-Protocol"]; SRWebSocket *wsmain = [[SRWebSocket alloc] initWithURLRequest:pushServerRequest]; //Declare this as a global variable in a header file wsmain.delegate=self; [wsmain open];

  1. Delegate Methods 委托方法

-(void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { NSLog(@"Message %@",message); } - (void)webSocketDidOpen:(SRWebSocket *)webSocket { NSLog(@"Connected"); }

  1. Sending Commands 发送命令

    [wsmain send:commands];

By sending commands, you will receive a response in didReceiveMessage method. 通过发送命令,您将在didReceiveMessage方法中收到响应。

have you seen https://github.com/robbiehanson/CocoaAsyncSocket ? 你见过https://github.com/robbiehanson/CocoaAsyncSocket吗? It's easy to use socket 易于使用的插座

NSString *host = @"10.70.0.22";
int port = 1212;

_socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

NSError *error = nil;
[_socket connectToHost:host onPort:port error:&error];
if (error) {
    NSLog(@"%@",error);
}

delegate 代表

-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
NSLog(@"success");
}
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
if (err) {
    NSLog(@"error %@",err);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self connectToServer];
    });
}else{
    NSLog(@"disconnect");
}

} }

Another option is socket.io . 另一个选项是socket.io It has server libraries and a iOS client library written in Swift. 它具有用Swift编写的服务器库和iOS客户端库。 It's API is quite extensive. 它的API相当广泛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM