Connection issues with android client and java pc server

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

49 Replies - 5853 Views - Last Post: 26 February 2013 - 01:17 AM

#1 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 125
  • Joined: 10-February 13

Connection issues with android client and java pc server

Posted 10 February 2013 - 01:22 PM

Hi,

I'm really hoping someone here can help me out. I'm working on an android project in college, I haven't done android programming, nor have I done any socket programming in college. My first step in this project is to have a client server communication. I have, based on notes and tutorials etc I have found online created a simple client and server, where a string is sent to the server when a button is clicked on the client side. This string is then mean to print to console...

My end game is to send gps co ordinates to my server where another client will take them from the server. I'm doing this as the first step, and build on it.

I've included my code below, unfortunately, the string doesn't print to the console, it looks correct to me. Could someone please have a quick look over this and see where I might be going wrong?

I'm pulling my hair out at this stage!!

SERVER


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Additional_Server {

    public static void main(String[] args) throws Exception {
        

            Socket s;
            ServerSocket ss = new ServerSocket(2012);
             
        System.out.println("Server started. Listening to the port 2012");
            
        while (true) {

             try { 
                    System.out.println("Server: waiting for connection .."); 
                   s = ss.accept(); 
                   InputStream in = s.getInputStream(); 
                   Scanner r = new Scanner(in); 
         
                   String inputLine; 
                    inputLine = r.nextLine(); 
                    System.out.println("Hello " + inputLine + " from Gary");                  

                } catch (IOException ex) { 

                    System.out.println("Problem in message reading"); 
                } 
            } 
     

        } 
    }



CLIENT


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

 
public class Client extends Activity {

    private Socket s;
    private PrintWriter p;
    TextView display;
    Button test;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test = (Button) findViewById(R.id.test);
        display = (TextView) findViewById(R.id.Sdisplay);
        test.setonclickListener(new View.onclickListener() {
 
            public void onclick(View v) {

                // TODO Auto-generated method stub
 
                try {
                    s = new Socket("192.168.1.3", 2012); // connect to server
                    OutputStream o = s.getOutputStream();
                   p = new PrintWriter(o);
                    InputStream in = s.getInputStream();
                    Scanner r = new Scanner(in);
                    p.println("Gary");
                    p.flush();                     

                } catch (UnknownHostException e) {

                    e.printStackTrace();

                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
        });

    }
}



I'd really appreciate any pointers or help, as I've said, I'm very new to android programming so all help appreciated!!


I have created a java client and server with this same code (obvious edits) and it works on the same machine so I'm not sure what the issue is here.
Regards,
Gary

Is This A Good Question/Topic? 0
  • +

Replies To: Connection issues with android client and java pc server

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Connection issues with android client and java pc server

Posted 10 February 2013 - 02:31 PM

This 192.168.1.3 is a local IP address. You would really need a server and something like a web service for this type of architecture.
Was This Post Helpful? 1
  • +
  • -

#3 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 125
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 10 February 2013 - 02:38 PM

View Postmacosxnerd101, on 10 February 2013 - 02:31 PM, said:

This 192.168.1.3 is a local IP address. You would really need a server and something like a web service for this type of architecture.


Hi, thanks for your reply, that ip is the ip of my device, I'm unsure what you are talking about when you refer to a web service, i'm very new to android and there doesn't seem to be anything concrete online to learn from :( So if you wouldn't mind explaining -.-

Do you think the way I'm going about it is incorrect or unsuitable? I want to send the co ordinates of one device to a server so another client can use them...

thanks again
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Connection issues with android client and java pc server

Posted 10 February 2013 - 02:41 PM

Right, but 192.168.*.* usually refers to the local IP of the device on the local network. Your Android device probably isn't connected to the local network. On top of that, it won't be connected to the local network elsewhere.

You really need a server. More reading on what a Web Service is.
Was This Post Helpful? 0
  • +
  • -

#5 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,097
  • Joined: 13-March 09

Re: Connection issues with android client and java pc server

Posted 14 February 2013 - 09:18 AM

Yes, i agree with macosxnerd101
But there are other ways to do this then using web service.

Your Ip tell's me that you are using a router (or a un-bridged DSL modem)
On neither case, you can use port forwarding to open the port that the server-application uses.
Then you can just turn of you wifi on your device and try connecting over 3G-network.
That will ensure that your device has an external ip-address, not that it's needed if you try to connect to the server via your router ip (should be the external one, to be sure, visit whatsmyip.org). But, will i would use the 3G-network. This is because some routers uses a redirect function that works like this: If packages are sent from within the local network, and they are addressed to the router and to a forwarded port, then it will not send the package out. Instead it will just redirect the package to the local ip that is bounded to the open port. The router dose this to lower the latency and to put less stress on the bandwidth for the outgoing network. So, avoid making call within the local network if you can. It gave me some gray hair before i figured out.

Hope this makes some sense to you.

Cheers
Was This Post Helpful? 2
  • +
  • -

#6 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 125
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 18 February 2013 - 06:02 AM

View PostEndLessMind, on 14 February 2013 - 09:18 AM, said:

Yes, i agree with macosxnerd101
But there are other ways to do this then using web service.

Your Ip tell's me that you are using a router (or a un-bridged DSL modem)
On neither case, you can use port forwarding to open the port that the server-application uses.
Then you can just turn of you wifi on your device and try connecting over 3G-network.
That will ensure that your device has an external ip-address, not that it's needed if you try to connect to the server via your router ip (should be the external one, to be sure, visit whatsmyip.org). But, will i would use the 3G-network. This is because some routers uses a redirect function that works like this: If packages are sent from within the local network, and they are addressed to the router and to a forwarded port, then it will not send the package out. Instead it will just redirect the package to the local ip that is bounded to the open port. The router dose this to lower the latency and to put less stress on the bandwidth for the outgoing network. So, avoid making call within the local network if you can. It gave me some gray hair before i figured out.

Hope this makes some sense to you.

Cheers


Hi EndLessMind..

Thank you very much for your input, this was the method of communication I had in mind. I am set up in another building from my main modem, I had used an old dsl modem to give me internet out here. This caused problems and since my post I have gotten a router and bridged it to the network. This is now giving my ip addresses like 192.168.3.101. I'm just wondering if I port forward with the router, do I forward the ports that I have programmed into the code? I'm new to this so you'll have to excuse my ignorance but I'm eager to learn. I'm just unsure when you say about visiting whatsmyip.org to see if it's an external ip etc..

If you could help me out on this one I'd really appreciate it.

Thanks in advance,
Gary
Was This Post Helpful? 0
  • +
  • -

#7 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,097
  • Joined: 13-March 09

Re: Connection issues with android client and java pc server

Posted 18 February 2013 - 06:38 AM

Hi. Okey.
Yes, you want to forward the port the you have programmed your server to use.
whatsmyip.org will show you what ip your router/modem has (your external ip).
If you use port forwarding, then you would like to connect to the external-ip.
The only time you use local-ip is when you are sure that you always will be connected to the local network when you are trying to connect to the server, and this happens very rarely. So i would use my external-ip.
Also, the reason i'm recommending you to use whatsmyip.org, is that sometimes it can be hard to find the external-ip by logging in to the router/modem and sometimes (depending on your connection type like fiber, cable or dls) the internet-supplier uses proxys (you can check that by taking the ip you get from whatsmyip.org and run a trace in cmd: like tracert 255.255.255.0 ). If they use proxy, when you would like to connect to the last proxy-ip, and that's the one you'll find on whatsmyip.org.

Hope this isn't to much to take in.

Cheers
Was This Post Helpful? 2
  • +
  • -

#8 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 125
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 19 February 2013 - 09:30 AM

View PostEndLessMind, on 18 February 2013 - 06:38 AM, said:

Hi. Okey.
Yes, you want to forward the port the you have programmed your server to use.
whatsmyip.org will show you what ip your router/modem has (your external ip).
If you use port forwarding, then you would like to connect to the external-ip.
The only time you use local-ip is when you are sure that you always will be connected to the local network when you are trying to connect to the server, and this happens very rarely. So i would use my external-ip.
Also, the reason i'm recommending you to use whatsmyip.org, is that sometimes it can be hard to find the external-ip by logging in to the router/modem and sometimes (depending on your connection type like fiber, cable or dls) the internet-supplier uses proxys (you can check that by taking the ip you get from whatsmyip.org and run a trace in cmd: like tracert 255.255.255.0 ). If they use proxy, when you would like to connect to the last proxy-ip, and that's the one you'll find on whatsmyip.org.

Hope this isn't to much to take in.

Cheers


EndLessMind that's great information. Can I ask so, if I am sure I'm going to connected to the local network (for demo purposes), what would be the procedure? I will forward the relevant ports on my router in this case 2012, then I would put in the ip of my device (the phone)?

Again, thanks so much for your help, it's very beneficial!
Was This Post Helpful? 0
  • +
  • -

#9 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,097
  • Joined: 13-March 09

Re: Connection issues with android client and java pc server

Posted 19 February 2013 - 10:48 AM

If you are sure, then i can use the local IP, then you also don't have to do anything.
Was This Post Helpful? 1
  • +
  • -

#10 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 125
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 19 February 2013 - 10:58 AM

View PostEndLessMind, on 19 February 2013 - 10:48 AM, said:

If you are sure, then i can use the local IP, then you also don't have to do anything.


EndLessMind...thanks so much.

So I'll just forward my ports, use my local IP, can I ask, my code I posted above, does that look correct to you?

Regards,
Gary
Was This Post Helpful? 0
  • +
  • -

#11 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,097
  • Joined: 13-March 09

Re: Connection issues with android client and java pc server

Posted 19 February 2013 - 11:46 AM

If you're going to connect via the local ip, then you don't need to forward the port.

The code looks fine, but i would use a DataOutputStream instead if a PrintWriter:
DataOutputStream _OutPut = new DataOutputStream(s.getOutputStream());
_OutPut.writeBytes("message");
_OutPut.flush();


If the message is not receive by the server directly, try force-closing the app (without closing down the emulator/ powering of the device)
If the message is then received, then you have to change some stuff on the server.
It's a long time since i did this type of project, so i can remember everything, and i can find my old code :/
But we will solve this :)
Was This Post Helpful? 0
  • +
  • -

#12 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 125
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 21 February 2013 - 10:54 AM

View PostEndLessMind, on 19 February 2013 - 11:46 AM, said:

If you're going to connect via the local ip, then you don't need to forward the port.

The code looks fine, but i would use a DataOutputStream instead if a PrintWriter:
DataOutputStream _OutPut = new DataOutputStream(s.getOutputStream());
_OutPut.writeBytes("message");
_OutPut.flush();


If the message is not receive by the server directly, try force-closing the app (without closing down the emulator/ powering of the device)
If the message is then received, then you have to change some stuff on the server.
It's a long time since i did this type of project, so i can remember everything, and i can find my old code ://>/>
But we will solve this :)/>/>


EndLessMind

Thanks, it's great to have someone willing to help me solve my problem. Right, well I've modified the code with the DataOutputStream, my message still isn't being printed to the console window, I have force closed the application, and I ran it again but still no success. I just don't know what to do?! :stupid:/>

After ensuring that my code is correct, I'm not sure where to turn next :blink:/>
Was This Post Helpful? 0
  • +
  • -

#13 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,097
  • Joined: 13-March 09

Re: Connection issues with android client and java pc server

Posted 21 February 2013 - 11:09 AM

Is the connection accepted by the server?

If so try sending "Hello server" + "\n"
:)
Was This Post Helpful? 0
  • +
  • -

#14 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 125
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 21 February 2013 - 11:37 AM

View PostEndLessMind, on 21 February 2013 - 11:09 AM, said:

Is the connection accepted by the server?

If so try sending "Hello server" + "\n"
:)/>/>



After this line on server

s = ss.accept();


I put in this

 System.out.println("Connection Accepted");


To try to figure out if it is accepted, my assumption is, if accepted, it will then print out "Connection Accepted"??

It doesn't print though, does this mean that the server socket isn't accepted?
Was This Post Helpful? 0
  • +
  • -

#15 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,097
  • Joined: 13-March 09

Re: Connection issues with android client and java pc server

Posted 21 February 2013 - 12:06 PM

Yeah, basically, it blocks the thread until a connection has been accepted:
http://developer.and...t.html#accept()

You can also check so that the socked returned by ServerSocket.accept() is not null.

You can do like this:
if (s != null) {
DataInputStream in = new DataInputStream(s.getImputStream());
while (s.isConnected()) {
System.out.println("" + new BufferedReader(in).readLine()); 
}

}


What code should work if you add + "\n" to each message you send to the server :)
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »