Basic Socket Problem

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Basic Socket Problem

Postby scottk9876 » Tue Jul 03, 2012 12:17 am

[The pic at the bottom shows the result on the emulator for running the app]

I am developing a network application that exchanges xml data from an android app to a remote server. The server receives xml requests from the apps, processes them, and returns an xml result.

My goal is to overcome the technical hurdles by establishing a simple proof-of-concept server and app pair that can talk to each other through a TCP socket connection.

I have Windows 7 operating with IIS that has a server on port 23 that receives an xml request, displays the request for me to see (on a memo field), and responds with a string back to the client. I know this server works (written with Delphi) because I have a delphi client that sends an xml request that gets displayed, and receives the output xml from the server that it, in turn, displays.

I wish to create an andoid app using Eclipse that duplicates the client functionality - namely, it opens a socket connection with the server, sends pre-set xml (just a string) and receives the resulting xml from the server. I can’t even get the connection to work. I am new to android development so please bear with me. I will provide you with whatever you need to help.

Here is the android manifest:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="com.example.client"
  4.    android:versionCode="1"
  5.    android:versionName="1.0" >
  6.  
  7.     <uses-sdk android:minSdkVersion="15" />
  8.     <uses-permission android:name="android.permission.INTERNET" />
  9.  
  10.     <application
  11.        android:icon="@drawable/ic_launcher"
  12.        android:label="@string/app_name" >
  13.         <activity
  14.            android:name=".ClientTestActivity"
  15.            android:label="@string/app_name" >
  16.             <intent-filter>
  17.                 <action android:name="android.intent.action.MAIN" />
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.     </application>
  22. </manifest>
  23.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



Here is the Java Code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.client;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.net.Socket;
  6. import java.net.SocketAddress;
  7.  
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11.  
  12. public class ClientTestActivity extends Activity {
  13.        
  14.         // Define our variables
  15.         SocketAddress sockaddr; // Our socket address
  16.         Socket my_socket;                       // Our Socket to send xml data
  17.         TextView display_txt;   // The display view we use to communicate results
  18.        
  19.     /** Called when the activity is first created. */
  20. [syntax="java"][/syntax]    @Override
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.  
  23.         /** Default constructors */
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.main);
  26.         display_txt = (TextView)findViewById(R.id.textView1);
  27.        
  28.         /**  Protect against exceptions */
  29.         try {
  30.                
  31.                 /**  define the socket address. 10.0.2.2 should map to "Localhost" */
  32.                         SocketAddress sockaddr = new InetSocketAddress("10.0.2.2", 23);
  33.                        
  34.                         /**  Instantiate and connect the socket */
  35.             my_socket = new Socket();
  36.                         my_socket.connect( sockaddr );
  37.                        
  38.                        
  39.                 } catch (IOException e) {
  40.                        
  41.                                 /**  Report an exception encountered */
  42.                                 display_txt.setText("Nasty Error");
  43.                                
  44.                 } finally {
  45.                        
  46.                         /** Catch any exception thrown from closing the socket */
  47.                         try {
  48.                                
  49.                                 /** Close our socket and report results */
  50.                                 my_socket.close();
  51.                                 display_txt.setText("Connected Okay!");
  52.                                
  53.                         } catch (IOException e) {
  54.                                
  55.                                 /** Display the error trying to close */
  56.                                 display_txt.setText("Could not close socket.");
  57.                                
  58.                         }
  59.                 }
  60.        
  61.     }
  62. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


When I run the app, I don't get the expected error messages or "Connected Okay!". I simply get the emulator stating "Unfortunately, Client_Socket has stopped".

Thank you VERY MUCH, in advance, for your time and help.

Scott
Image
scottk9876
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jul 02, 2012 12:38 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google [Bot] and 5 guests