使用套接字的iPhone通信 [英] iPhone communication using Sockets

查看:47
本文介绍了使用套接字的iPhone通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用应用程序通过套接字与Windows通信.至少,我至少要弄清楚如何从iPhone创建连接(也许使用iPhone来对Windows计算机执行ping操作?),我不清楚我应该从哪里开始.总的来说,我对iOS开发还很陌生,对套接字/网络编程又是全新的.我已经尝试了一些教程,但这些教程并没有使我走得太远.我的目标是:

I'm trying to to use an application to communicate with Windows via sockets. At the minimum, I'm trying to at least figure out how I can create a connection from the iPhone (maybe using the iPhone to ping the Windows machine?) I'm not really clear on where I need to start. I'm pretty new to iOS development in general, and brand new to socket/network programming. I've tried several tutorials that haven't gotten me far. My goal is:

  • 通过套接字连接到服务器(该服务器将是Windows计算机,其服务正在等待来自iPhone的传入连接)
  • 我最终将向服务器发送JSON数据包,并从服务器接收JSON数据包
  • 在iPhone中显示成功或失败的响应

如果可能的话,我想先编写/构建客户端,但是到目前为止我迷路了.希望SO社区中的好人能伸出援手,向正确的方向指点我.

If possible, I would like to write/build the client piece first, but I have been lost thus far. Hopefully the nice folks in the SO community can lend a hand and point me in the right direction.

提前谢谢!

推荐答案

您的问题非常广泛,尤其是它没有针对特定的编程问题.我会为您提供一些指导原则或起点.

Your question is very wide especially that it does not target a specific programing problem. I will give you some guidelines or point of start.

对于iPhone,您需要学习如何使用NSStream的方法,并且可能从这里开始:

For iPhone you need to learn how to work with NSStream and you might start from here: Stream Programming Guide for Cocoa. You will need an output stream and an input stream so you can manage outgoing and incoming communications easily and in parallel. In other words you will benefit from the NSInputStream and the NSOutputStream classes which are derived from the main NSStream class. When you will send data using streams you will send raw data using uint8_t buffers so it is not obligatory to use JSON packets; I personally don't, but it is up to you.

在Windows上,我假设您将使用C#,因此您将需要学习TcpListener,它侦听您指定的特定IP地址和端口号.这可以使您入门:

On Windows I assume you will be using C# so you will need to learn TcpListener which listens on a certain IP address and a port number that you specify. This can start you up a bit: TcpListener Class. Also you will need TcpClient through which you will read and write to the stream.

对于高效服务器,您需要异步工作,并且TcpClient具有同步和异步方法.另外,为了获得更好的功能,您可能需要在服务器中使用线程,或者使用内置的BackgroundWorker类,这使事情变得更加简单.

For efficient server you need to work Asynchronously and TcpClient has synchronous and asynchronous methods for that. Also for better functionality you might need to use threading in your server, or use the built in BackgroundWorker class which makes things much more easy.

我不建议仅对客户端进行编程,而不是对服务器进行编程,我认为它们应该并行进行,因为这是一种双向通信,如果尝试这样做,将会发现您在这里和那里都应该工作很少.如果我告诉你,当我在使用客户端/服务器应用程序时,我的办公桌上有一台Mac和PC,并且每次都在它们之间进行切换就不足为奇了.

I don't suggest programming the client alone then the server, I believe that they should go in parallel because it is a 2 sided communication and if you try that you will see that you must work little here and little there. It is not a surprise if I tell you that when I work on my client/server app I have on my desk a Mac and a PC and I switch between them every while.

最后,我想对您没有问过的问题发表评论.由于客户端是移动设备,因此您必须期望它不总是处于打开状态(或者不总是具有Internet连接),因此请做好准备在服务器中进行一些数据库工作以能够存储需要发送的消息以后...

Finally, I would like to comment on something which you did not ask about. Since your client is a mobile device then you have to expect that it is not always on (or no Internet connection always on it), therefore prepare yourself to have some database work in the server to be able to store messages that need to be sent later...

我希望这可以帮助您入门.如果您有更具体的问题,我也许可以提供更好的说明.顺便说一句,这项工作并不是那么容易,但是如果您真的喜欢编程,那将是非常有趣的,尤其是当您开始获得第一个结果时;)

I hope this can help you start up. If you have a more specific question I might be able to illustrate better. By the way, this job is not that easy but it is great fun if you really like programming, especially when you start to get your first results ;)

这篇关于使用套接字的iPhone通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆