如何使用“iphone 套接字”编写通信协议?

发布于 12-01 14:07 字数 632 浏览 2 评论 0原文

我已经为 Android 编写了一个客户端服务器应用程序,并且正在为 Iphone 编写相同的程序。 问题是,对于通信过程,使用 java 服务器和 Android 客户端,我编写了这样的代码(伪......):

data = read();
if ( data.compareTo("Something1") == 0) then
     write("Something2");
     data = read();
     if ( data.compareTo("Something3") == 0) then
          //End communitacion
     endif
endif

现在我的问题是,对于“iphone 套接字”,我不知道如何这样做。我正在使用 AsyncScoket 库,并且使用该库,读取不会阻塞(这对我来说很复杂),并且它使用我真的不知道如何正确使用它的回调。

我想当读取完成时,我应该在正确的回调中测试发送的数据是否等于“Something1”或“Something2”......但是,第一个小问题是它没有任何顺序。

我应该说我没有时间修改协议。

如果有人可以指导我,我将非常感激。或者使用 AsyncSocket 库完成网络通信的任何代码示例。

谢谢你!

I've programmed a client-server application for Android and I'm doing the same for Iphone.
The thing is that for the communication process, with the java server and the Android client, I've programmed something like this (in pseudo...):

data = read();
if ( data.compareTo("Something1") == 0) then
     write("Something2");
     data = read();
     if ( data.compareTo("Something3") == 0) then
          //End communitacion
     endif
endif

Now my problem is that with the "iphone sockets" I don't know how to do that. I'm using the AsyncScoket library, and with that library the read is not blocking (something that is complicated for me) and it uses callbacks that I don't really know how to use it correctly.

I guess that when a read is done, I should test in the proper callback if the data sent is equal to "Something1" or "Something2"... But, the first little problem is that it doesn't have any order.

I should say that I have no time to modify the protocol.

If someone could guide me, I would be so grateful. Or any code example where a network communication is done with the AsyncSocket library.

Thank you!

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

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

发布评论

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

评论(1

梦明2024-12-08 14:07:22

我最近开始使用 asyncsocket 和 TCP java 服务器。也许这段代码可能有帮助?

要写入套接字,您需要从 asyncsocket 对象调用以下方法,

-(void)writeData:withTimeout:tag:;

此处的数据应该是 NSData 对象。 您可以通过执行以下操作轻松地将字符串转换为 NSData:

[someStringObject dataUsingEncoding:NSUTF8Encoding];

从套接字读取,只需调用此方法,

-(void)readDataWithTimeOut:tag:;

如果您不需要超时,

传递 -1 表示超时。就是这样。使用 cocoaasyncsocket 进行读/写比在 Java 中处理套接字要容易得多,在 Java 中您需要 datainputstream 和 dataoutputstream 以及其他类似的东西。

重要的委托方法是

-(void)onSocket:didReadData:withTag: gets called whenever the socket reads some data.

该委托的示例是..

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

      NSString *string = [[NSString alloc] initWithBytes:[data bytes] 
                                                  length:[data length]
                                                encoding:NSUTF8StringEncoding];
     //Do what ever comparison you'd like here.

}

如果您有任何不明白的地方,请告诉我,我会尽力回答您的疑问。

I've recently begun using asyncsocket along with a TCP java server. Perhaps this code may help?

To write to the socket you need to call the following method from the asyncsocket object

-(void)writeData:withTimeout:tag:;

Data here should be an NSData object. You can easily convert strings to NSData by doing

[someStringObject dataUsingEncoding:NSUTF8Encoding];

To read from the socket just call this method

-(void)readDataWithTimeOut:tag:;

pass -1 for the timeout if you do not need a timeout.

And that's it. Reading/Writing using cocoaasyncsocket is much easier than dealing with sockets in Java where you need datainputstream and dataoutputstream and other such stuff.

Important delegate methods are

-(void)onSocket:didReadData:withTag: gets called whenever the socket reads some data.

Example of this delegate is..

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

      NSString *string = [[NSString alloc] initWithBytes:[data bytes] 
                                                  length:[data length]
                                                encoding:NSUTF8StringEncoding];
     //Do what ever comparison you'd like here.

}

If there is anything that you don't understand then let me know and I'll try and answer your doubts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文