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 Mon Feb 06, 2012 6:12 pm

All times are UTC


 Topics   Replies   Views   Last post 
No new posts Rip Blu-ray video to Blackberry Playbook

by alex6699 on Fri Nov 26, 2010 2:56 am in Relaxing Lounge

0

213

Fri Nov 26, 2010 2:56 am

alex6699

No new posts A calls Blocker Software for Windows Mobile

by tingelsky on Tue Nov 16, 2010 7:08 am in Relaxing Lounge

0

137

Tue Nov 16, 2010 7:08 am

tingelsky

No new posts android ksoap2 Caused by: java.lang.RuntimeException: Cannot

by mindus on Thu Sep 30, 2010 6:07 am in Beginner level ANDROID developing issues

0

187

Thu Sep 30, 2010 6:07 am

mindus

No new posts Create a pdf file dynamically

by mindus on Wed Sep 22, 2010 8:47 am in Beginner level ANDROID developing issues

0

104

Wed Sep 22, 2010 8:47 am

mindus

No new posts How can I backup my mobile contacts to my pc?

by weibin.li on Tue Aug 10, 2010 7:28 am in Relaxing Lounge

1

151

Tue Aug 10, 2010 7:28 am

weibin.li




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.