Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    Ginso is offline Member
    Join Date
    Feb 2017
    Posts
    12
    Rep Power
    0

    Default socket connection from android to pc via internet

    Hello,
    i'm not sure, whether this is the right category for this topic:
    I wrote an android app and a pc-app that communicate via socket connection(pc is server, android-app is client).

    Server:
    Java Code:
    Socket socket = null;
    ServerSocket server = null;
    int port = 4740;
    try {			
    	server = new ServerSocket(port);
    	System.out.printf("port: %d\n",port);
    	socket = server.accept();
    	// ...
    } catch (BindException e) {
    }
    Client:
    Java Code:
    InputStream input;
    try {
    	socket = new Socket(IPAdress,4740);
    	//...
    My question: Can i somehow achieve a connection like that via Internet(without both devices being in the same local network)?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: socket connection from android to pc via internet

    You can use the Java socket API's to make connections across the internet. I cannot vouch for the above code as I don't do it much. But here are some
    considerations.

    1. You need a route from host A to host B.
    2. If using DNS you need to ensure it is working and provides an address.
    3. And you need to ensure that any firewalls by you, your target, or any ISP in between are not blocking the port or target IP.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: socket connection from android to pc via internet

    If the 'IPAdress' can be reached from the client, you're in business ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Ginso is offline Member
    Join Date
    Feb 2017
    Posts
    12
    Rep Power
    0

    Default Re: socket connection from android to pc via internet

    Quote Originally Posted by JosAH View Post
    If the 'IPAdress' can be reached from the client, you're in business ...
    and how do i get an ip adress that can be reached from anywhere?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: socket connection from android to pc via internet

    ensure that any firewalls by you, your target, or any ISP in between are not blocking
    I'm also having a problem connecting to a server on my PC (LAN address: 192.168.1.69:8080) from an Android app. I can connect to an Android server(192.168.1.116:8080) from my PC.
    I'm on Win 10 and have looked at its Firewall and am unable to figure out how to configure it.
    I tried turning it (public or private???) off (but didn't reboot) without any luck.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: socket connection from android to pc via internet

    Quote Originally Posted by Ginso View Post
    and how do i get an ip adress that can be reached from anywhere?
    Of course the host that implements your server must be hooked up to the internet; if nothing else is done, it can only be reached through its (public) IP address. If it has a registered domain name, it can also be reached through its dns name (e.g. 'google.com'). There is quite a bit of trickery-dickery available to do it in other, more obscure ways ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: socket connection from android to pc via internet

    Also you may need to configure your router to allow outsiders to get to a PC on a LAN.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Ginso is offline Member
    Join Date
    Feb 2017
    Posts
    12
    Rep Power
    0

    Default Re: socket connection from android to pc via internet

    ok i tried to enter the IP-Adress, which https://myexternalip.com/ showed me(which is an IPv6 adress), but socket = new Socket(IPAdress,4740); thows an exception saying "failed to conect to /<IPADRESS> (port 4740)): connect failed ENETUNREACH (Network is unreachable)"
    I tried deactivating the norton firewall, but it didnt help. Can u elaborate, what i have to do with my router?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: socket connection from android to pc via internet

    I've never changed my router to allow in users to connect to a PC inside the local network. I've read that is is possible.
    Ask an internet search engine. Here is one page I found: https://www.att.com/gen/general?pid=23697
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: socket connection from android to pc via internet

    Quote Originally Posted by Ginso View Post
    ok i tried to enter the IP-Adress, which https://myexternalip.com/ showed me(which is an IPv6 adress), but socket = new Socket(IPAdress,4740); thows an exception saying "failed to conect to /<IPADRESS> (port 4740)): connect failed ENETUNREACH (Network is unreachable)"
    I tried deactivating the norton firewall, but it didnt help. Can u elaborate, what i have to do with my router?
    That site requires SSL secure sockcets (https), so you should ask a SSLSSocketFactory for a secure socket, not an ordinary one ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: socket connection from android to pc via internet

    If you just want to do it between your Android and your own PC then you can chose a different port number to handle the https packets. Then you can configure your router to send traffic destined to that port number to a specified host on the LAN. Note that this should only be necessary for traffic originating from the Android device. If the traffic originates from the LAN host then it should behave like any other https connection.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: socket connection from android to pc via internet

    I don't think the OP is looking for https. I think just http passthru is needed.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: socket connection from android to pc via internet

    Then he can might also do a port mapper if passthru isn't supported on his router.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: socket connection from android to pc via internet

    Quote Originally Posted by Norm View Post
    I don't think the OP is looking for https. I think just http passthru is needed.
    My bad; I misread the OP's reply #8 ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: socket connection from android to pc via internet

    This site looks useful: https://portforward.com/
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: socket connection from android to pc via internet

    Yeah, that's what i was talking about. Every router I have ever had supported that capability. And it is pretty easy to set up.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: socket connection from android to pc via internet

    I just got the alpha version of a Simple Web Server running on my Android tablet. I can now use it to send and receive files to/from my tablet.
    My wife and I use our tablets a lot and being able to easily and quickly move files between our tablets and PCs will be useful.
    Plus all the fun I'm having learning Android.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Ginso is offline Member
    Join Date
    Feb 2017
    Posts
    12
    Rep Power
    0

    Default Re: socket connection from android to pc via internet

    i added the port to my routers port filter and port forwarding, but it had no effect. how would u find out the ip adress i have to use?

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: socket connection from android to pc via internet

    Quote Originally Posted by Ginso View Post
    i added the port to my routers port filter and port forwarding, but it had no effect. how would u find out the ip adress i have to use?
    If your machine is on the same LAN as your server host, simply use the local IP address of the server host; only if you are outside (i.e. not on your own LAN) you could use your own public IP address; your router will do the rest ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  20. #20
    Ginso is offline Member
    Join Date
    Feb 2017
    Posts
    12
    Rep Power
    0

    Default Re: socket connection from android to pc via internet

    Thats what this whole thread is about... i know how to do it if the devices are on the same LAN. Where do i find my "own public IP adress"?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2012, 05:20 PM
  2. Replies: 0
    Last Post: 08-01-2011, 04:34 PM
  3. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 02:14 AM
  4. client javadb connection over internet
    By OmerHalit in forum JDBC
    Replies: 15
    Last Post: 05-26-2010, 11:27 AM
  5. pc give internet connection to mobile
    By dimitrist in forum New To Java
    Replies: 0
    Last Post: 12-21-2008, 02:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •