如何使用“iphone 套接字”编写通信协议?
我已经为 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!
我最近开始使用 asyncsocket 和 TCP java 服务器。也许这段代码可能有帮助?
要写入套接字,您需要从 asyncsocket 对象调用以下方法,
此处的数据应该是 NSData 对象。 您可以通过执行以下操作轻松地将字符串转换为 NSData:
从套接字读取,只需调用此方法,
如果您不需要超时,
传递 -1 表示超时。就是这样。使用 cocoaasyncsocket 进行读/写比在 Java 中处理套接字要容易得多,在 Java 中您需要 datainputstream 和 dataoutputstream 以及其他类似的东西。
重要的委托方法是
该委托的示例是..
如果您有任何不明白的地方,请告诉我,我会尽力回答您的疑问。
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
Data here should be an NSData object. You can easily convert strings to NSData by doing
To read from the socket just call this method
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
Example of this delegate is..
If there is anything that you don't understand then let me know and I'll try and answer your doubts.