Hi, there is the Socket method : Socket.getLocalAddress().toString();

On Mar 20, 2:50 pm, Cadge <[email protected]> wrote:
> Ok but what about if the device is not connected to Wifi, is there an
> easy way to get the IP address and use sockets as above?
>
> Thanks again.
>
> On Mar 20, 1:28 pm, Daniel Janev <[email protected]> wrote:
>
> >         Hi,
>
> > Here is your problem:
>
> > ...
> > socket = new Socket("127.0.0.1", 1234);
> > ...
>
> > Youhave to use the IP address the device has :) "127.0.0.1" means
> > localhost :) Just find the IP address (probably you use WLAN of the
> > device) and use it in your test.
>
> > Cadge wrote:
> > > Basically what I want to achieve is have a server running on an
> > > android device that a P.C/Laptop/whatever can connect to using
> > > sockets.
>
> > > Currently I have this working on an emulator, where I can send a
> > > simple text String, but obviously networking on a real divice is
> > > different.
>
> > > I was just wondering what changes to my code I would have to make to
> > > get this to work a real device?
>
> > > Here is the code which works on the emulator. Thanks in advance
>
> > > <<<Android Server>>>
>
> > > public void run() {
>
> > >            try {
> > >                    ss = new ServerSocket(1234);
> > >            }
> > >            catch(IOException e) {
> > >                    System.out.print(e);
> > >                    System.exit(1);
> > >            }
>
> > >            while (true) {
> > >                    try {
> > >                            socket = ss.accept();
>
> > >                            in = new BufferedReader(
> > >                                               new InputStreamReader(
> > >                                               socket.getInputStream
> > > ()));
>
> > >                            String text = in.readLine();
>
> > >                    }
> > >                    catch (IOException e) {
> > >                            e.printStackTrace();
> > >                    }
> > >            }
> > >    }
>
> > > <<<Development Machine Client>>>
>
> > > public TestClient() {
>
> > >            try {
> > >                    socket = new Socket("127.0.0.1", 1234);
> > >                    out = new PrintWriter(socket.getOutputStream(), true);
> > >                    out.println("TEST");
> > >            }
> > >            catch (UnknownHostException e) {
> > >                    e.printStackTrace();
> > >            }
> > >            catch (IOException e) {
> > >                    e.printStackTrace();
> > >            }
> > > }
>
> > --
>
> > Best Regards,
> >     Daniel
> > ---------------------------------------------------------------
> > Daniel Janev · Department Manager/Core Platform and Smart Home
> > ProSyst Software GmbH
> > 1606 Sofia, Bulgaria · Vladajska Str. 48
> > Tel. +359 (0)2 952 35 81/109 · Fax +359 (0)2 953 26 17
> > Mobile Phone +359 (0)888 678 670http://www.prosyst.com·[email protected]
> > ---------------------------------------------------------------
> > stay in touch with your product.
> > ---------------------------------------------------------------

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to