Socket iPhone Communication

I am trying to use an application to communicate with Windows through sockets. At a minimum, I'm trying to at least figure out how I can create a connection to an iPhone (perhaps using an iPhone to ping a Windows machine?) I don't quite understand where I need to start. I am new to iOS development in general and completely new to socket / network programming. I tried several tutorials that did not make me far. My goal:

  • Connecting to the server via sockets (the server will be a Windows machine with a service waiting for incoming connections to the iPhone)
  • I will ultimately send JSON packets to the server, and also receive JSON packets from the server
  • Come up with an answer on the iPhone that indicates success or failure.

If possible, I would like to first write / build a part of the client, but so far I am lost. Hope the lovely people in the SO community can reach out and point me in the right direction.

Thanks in advance!

+1
source share
4 answers

Your question is very broad, especially that it is not aimed at a specific programming problem. I will give you some recommendations or a starting point.

For iPhone, you need to learn how to work with NSStream , and you can start here: Flow Programming Guide for Cocoa . You will need an output stream and an input stream so that you can easily and simultaneously control the outgoing and incoming communications. In other words, you will benefit from the NSInputStream and NSOutputStream , which are derived from the main NSStream class. When you send data using streams, you will send raw data using uint8_t buffers, so it is not necessary to use JSON packets; I personally do not do this, but it is up to you.

On Windows, I assume that you will use C #, so you will need to find out a TcpListener that listens for a specific IP address and a specified port number. This may help you a bit: TcpListener Class . You will also need TcpClient , through which you will read and write to the stream.

For an efficient server, you need to work asynchronously, and TcpClient has synchronous and asynchronous methods for this. In addition, to improve functionality, you may need to use streams on your server or use the built-in BackgroundWorker class, which makes things much easier.

I do not propose to program the client on my own, and then the server, I believe that they should go in parallel, because it is a two-way communication, and if you try, you will see that you have to work a little here and a little there. It is not surprising if I tell you that when I work on my client / server application, I have a Mac and a PC on my desk, and I switch between them every time.

Finally, I would like to comment on what you did not ask. Since your client is a mobile device, you should expect that it is not always turned on (or will not always be connected to the Internet), so be prepared to have a database running on the server in order to be able to store messages that need to be sent later. .

Hope this helps you get started. If you have a more specific question, I could better illustrate. By the way, this work is not so easy, but it's great if you really like programming, especially when you start getting your first results;)

+3
source

If you really have to use sockets, do yourself a favor and grab a pretty socket library like AsyncSocket. https://github.com/robbiehanson/CocoaAsyncSocket

But it's better to try to do this via http first and only go to sockets when you really need to do this.

+1
source

If the server uses some kind of user protocol, you can use the BSD socket API. If the server says the HTTP protocol, use either:

  • NSURLConnection - for asynchronous communication from the main thread.
  • Download synchronously using NSData, but from a separate thread.

Example for # 2:

 NSString *url = @"http://example.com"; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: url]]; NSString *str = [[NSString alloc] initWithBytes: [data bytes] length:[data length] encoding: NSUTF8StringEncoding]; 
0
source

If you use HTTP, as already mentioned, life will be much easier. It makes no sense to reinvent the wheel when you already have a protocol as extensive and widely used as HTTP.

AFNetworking is a good library for communicating with a web server and comes with the ability to encode / decode JSON.

An example of using AFNetworking to communicate using HTTP, JSON, and the REST API.

 NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST" path:@"user/login" parameters:[[NSDictionary alloc] initWithObjectsAndKeys:user, @"username", password, @"password", nil]]; //Make operation from request AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { //Block will be called when request is successful } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { //Block will be called if request has failed }]; //Start request [httpClient enqueueHTTPRequestOperation:jsonOperation]; 

Pretty easy to use, I think it's easier than NSURLConnection. Using blocks makes it a lot more proactive. Plus, you don't have to worry about JSON encoding / decoding. JSON will be encoded / decoded for you directly from the objective-c object to JSON (or from the JSON object to objective-c).

0
source

All Articles