Android Open Handset Developers Community

 
Guest Announcements

:arrow: Welcome: you are not yet registered; you can register here.
:arrow: A new forum on the new HTC Magic is available here.

It is currently Thu Jul 29, 2010 6:01 pm

All times are UTC


 Topics   Replies   Views   Last post 
No new posts For Sale Brand New Apple Tablet iPad 64GB (Wi-Fi + 3G) .$28

by NEVERSAYNEVER on Wed Jul 28, 2010 2:06 pm in News & Announcements

0

5

Wed Jul 28, 2010 2:06 pm

NEVERSAYNEVER

No new posts For Sale Brand New HTC HD2--$280 and Nokia X3@220usd

by NEVERSAYNEVER on Wed Jul 28, 2010 2:05 pm in Users' introductions

0

3

Wed Jul 28, 2010 2:05 pm

NEVERSAYNEVER

No new posts For sale Apple iPhone 4G 32GB $300/3GS 32GB/IPad 64GB $600

by hyperb1 on Wed Jul 28, 2010 10:34 am in News & Announcements

0

4

Wed Jul 28, 2010 10:34 am

hyperb1

No new posts For sell new Nokia n8 $210,Apple iPhone 4G 64gb-$200

by alwaysonline on Tue Jul 27, 2010 12:59 pm in Users' introductions

0

6

Tue Jul 27, 2010 12:59 pm

alwaysonline

No new posts Whosales Cheap Online Brand HTC HD2$280

by SKYPECHART on Sun Jul 25, 2010 12:17 pm in Relaxing Lounge

0

9

Sun Jul 25, 2010 12:17 pm

SKYPECHART




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Problem: read a file, connect to a server
PostPosted: Mon Jul 27, 2009 10:07 pm 
Offline

Joined: Mon Jul 27, 2009 2:29 pm
Posts: 1
Hi guys, I've some problems:

1)first I'm not able to read a file:(
2)second seems that my code that have to simulate a Client can't
connect to my server.
maybe I'm wrong something, Ip address(I think it is right)? I have set
the virtual device on port 5554 (the default)..I need to redirect the
host port?
this is my code:

;
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import java.io.*;
import java.net.*;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class C extends Activity {
    /** Called when the activity is first created. */
        private PrintWriter savedpoint;
        private Context context = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try{

        savedpoint = new PrintWriter(openFileOutput
("Data.dat",MODE_APPEND));

        for(int i=0;i<20;i++)//scrive coordinate
        {
                savedpoint.println(-78.443221+"i"+" "+120.344423+"i");

        }

        F thread=new F(getFilesDir(),this);
        thread.start();

        }catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
    }

}

//thread that have to acess to the server
class F extends Thread{
        File directory;
        Context context;

        private final static String host = "10.0.2.2";
        private final static int port = 2120;

        F(File directory,Context context)
{this.directory=directory;this.context=context;}
        public void run()
        {

                try{

                Socket socket = null;
                PrintWriter out = null;
                InetAddress ip = InetAddress.getByName(host);
                socket = new Socket(ip,port);       ----->SEEMS THERE IS NO CONNECTIONS, BECAUSE THE SERVER DOESN'T SIGN ANYTHING
                                                       
                out = new PrintWriter( new BufferedWriter( new OutputStreamWriter
(socket.getOutputStream())),true);
                Scanner s=new Scanner(directory+"Data.dat");     ---> IS IT THE RIGHT WAY TO READ A FILE?

                try {
                        while (true) {

                                String l = s.nextLine();    ---->ERROR
                                out.println(l);
                        }
                } catch (NoSuchElementException e) {s.close();
                out.println("*");
                }catch(FileNotFoundException e){}
                } catch (UnknownHostException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();}

}
}


the manifest:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="prova.client"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".C"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

thanks in advance for any help..


Top
 Profile  
 
 Post subject: Re: Problem: read a file, connect to a server
PostPosted: Mon Aug 03, 2009 4:17 pm 
Offline

Joined: Thu Dec 27, 2007 2:51 pm
Posts: 41
loteodor wrote:
Hi guys, I've some problems:

1)first I'm not able to read a file:(
2)second seems that my code that have to simulate a Client can't
connect to my server.
maybe I'm wrong something, Ip address(I think it is right)? I have set
the virtual device on port 5554 (the default)..I need to redirect the
host port?
thanks in advance for any help..


Concerning the client/server communication, I think you are missing the read/write steps after opening the socket; you should use the socket's methods getInputStream / getOutputStream in order to create input / output streams:

Code:
     Socket socket= new Socket (ip,port);
     InputStream in = socket.getInputStream ();
     OutputStream out = socket.getOutputStream ();
     BufferedReader con = new BufferedReader (new InputStreamReader (System.in));

After the code above, you can read/write from the socket with in.read() and out.write()

I also strongly sugget to try to catch exceptions when creating the socket to check if something is going wrong:

Code:
try {
            Socket socket= new Socket (ip,port);
} catch (UnknownHostException e) {
            errorMessage("Unknown host" + ipAddress);

} catch (IOException e) {
            errorMessage("Couldn't get I/O for the connection to: " + ip);

}


Last but not least, you definitely have to give internet access to your application in your AndroidManifest.xml file:
Code:
<uses-permission android:name="android.permission.INTERNET" />

Hope this helps!


Top
 Profile  
 
 Post subject: Re: Problem: read a file, connect to a server
PostPosted: Tue Mar 16, 2010 1:38 pm 
Offline

Joined: Tue Mar 16, 2010 1:24 pm
Posts: 2
Hi,

can you send me the entire project of yours to my mail id hpushpa18@gmail.com and pushpa.h@meomyo.com
I also need to do the same so i want your code please
Thanks and Regards,
Pushpa


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
phpBB SEO

This site is not affiliated with nor endorsed by the Open Handset Alliance.
All trademarks and logos used in this site are of properties of their respective owners.