Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

iPhone Network Programming : Page 2

Learn how to communicate with a server using TCP/IP and how to build a simple chat application.


advertisement

Disconnecting

To disconnect from the server, define the disconnect method as shown below:

-(void) disconnect { [iStream close]; [oStream close]; }

Also, add in the following lines in bold to the dealloc method:


- (void)dealloc { [self disconnect]; [iStream release]; [oStream release]; if (readStream) CFRelease(readStream); if (writeStream) CFRelease(writeStream); [super dealloc]; }

Testing the Application


Figure 1. Populate: Populate the View window with views.
 
Figure 2. Verify: Verify the connections on the File’s Owner item.
 
Figure 3. View: Add more views.

You are now ready to put all the pieces together and test the code against a server. In the NetworkViewController.h file, declare the following outlet and action:

#import @interface NetworkViewController : UIViewController { IBOutlet UITextField *txtMessage; } @property (nonatomic, retain) UITextField *txtMessage; -(IBAction) btnSend: (id) sender; @end

Double-click on the NetworkViewController.xib file to edit it in Interface Builder. In the View window, populate it with the following views (see also Figure 1):
  • Text Field

  • Round Rect Button
Perform the following actions:
  • Control-click on the File’s Owner item and drag and drop it over the Text Field view. Select txtMessage.

  • Control-click on the Round Rect Button view and drag and drop it over the File’s Owner item. Select btnSend:.
Right-click on the File’s Owner item to verify its connections (see Figure 2).

Back in the NetworkViewController.m file, add the following lines of code in bold to the viewDidLoad method:

- (void)viewDidLoad { [self connectToServerUsingStream:@"192.168.1.102" portNo:500]; //---OR--- //[self connectToServerUsingCFStream:@"192.168.1.102" portNo:500]; [super viewDidLoad]; }

The above code assumes you are connecting to a server of IP address 192.168.1.102, at port 500. You will see how to write the server code shortly.

Implement the btnSend: method as follows:

-(IBAction) btnSend: (id) sender { const uint8_t *str = (uint8_t *) [txtMessage.text cStringUsingEncoding:NSASCIIStringEncoding]; [self writeToServer:str]; txtMessage.text = @""; }

Release the txtMessage outlet in the dealloc method:

- (void)dealloc { [txtMessage release]; [self disconnect]; [iStream release]; [oStream release]; if (readStream) CFRelease(readStream); if (writeStream) CFRelease(writeStream); [super dealloc]; }

Building the Server

Up till this point, you have built a client on the iPhone that is ready to send some text over to a server. So what about the server? To test this application, I have built a very simple console server using C#. Here is the code for the Program.cs file (see Listing 3).

The server program performs the following:

  • It assumes that the IP address of the server is 192.168.1.102. When testing on your end, replace this IP address with the IP address of your computer running this server application.

  • It sends back to the client whatever data it receives.

  • Once the data is received, the server no longer listens for incoming data. In order for the client to send data to it again, the client needs to reconnect to the server.
Author's Note: Download the accompanying source code for all the examples illustrated in this article.

Figure 4. Change is good: Change the font size of the Text View view.

With the server code explained, you can now test your iPhone application. Type some text into the Text Field view and click the Send button. If the connection is established, you should see the Alert View displaying the data received.

A More Interesting Example

In one of my earlier articles for DevX.com, I wrote about how to build your own instant messenger application (see “Home-brew Your Own Instant Messenger App with Visual Studio .NET"> using .NET. In that article, I wrote about how to write a server and a client application to allow chat messages to be sent between multiple users. Using the code in that article, you could actually modify your iPhone application as a chat client and communicate with other users on other platforms.

Using the same project created earlier, add the following views to the View window (see also Figure 3):

  • Label

  • Text Field

  • Round Rect Button

  • Text View
Select the Text View, press Command-T and change its font size to 9 (see Figure 4).

In the NetworkViewController.h file, add the following statements in bold:

#import @interface NetworkViewController : UIViewController { IBOutlet UITextField *txtMessage; IBOutlet UITextField *txtNickName; IBOutlet UITextView *txtMessages; } @property (nonatomic, retain) UITextField *txtMessage; @property (nonatomic, retain) UITextField *txtNickName; @property (nonatomic, retain) UITextView *txtMessages; -(IBAction) btnSend:(id) sender;

Figure 5. Connections: Verify the connections.
-(IBAction) btnLogin:(id) sender; @end
Perform the following actions:
  • Control-click on the File’s Owner item and drag and drop it over the top Text Field view. Select txtNickName.

  • Control-click on the File’s Owner item and drag and drop it over the top Text View view. Select txtMessages.

  • Control-click on the Round Rect Button view and drag and drop it over the File’s Owner item. Select btnLogin:.
Right-click on the File’s Owner item to verify its connections (see Figure 5). In the NetworkViewController.m file, add the following statements in bold (see Listing 4).

Figure 6. Chat: Start chatting on your iPhone.
That’s all! Press Command-R to test the application. First, enter a nickname for yourself and tap the Login button (see Figure 6). You can now start chatting by typing a message and tapping the Send button.

Author's Note: For your convenience, I have provided the C# version of the chat server in the download package for this article.

Summary

In this article, you have seen how to communicate with another server using TCP/IP. Knowing how to communicate with the outside world allows you to build very interesting applications. In the next article, I will talk about Bluetooth programming on the iPhone.



Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap