• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Cannot get server code to make connection with Android client

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I am new to client-server programming. I am writing a simple client (Android platform/java) - server (pure java - NOT on Android - runs on PC) application. I am running the application on a router wireless network, so using internal IP 192.168.1.2  and trying to send a file image from Android to the server code. This is the basics that is needed to work. The Problem is connection is not fully established so the image file couldn't be sent. The code I guess would be a peice of cake to experts to see where i went wrong. When I googled people where talking in too high level manner and I still didn't know how to fix it.
I will appreciate your help, many thanks in advance.    Here is the entire code (no crash or exception, but just didn't make connections)


Client side (Android java)
first part of the code and the 2 last functions of the code is just to allow user to select any image from the gallery
So only the middle part is socket programming




Server side code (pure Java, not Android platform)
For now file size hard coded. int filesize=550000; but can be adjusted to the size of the image you will select



xml layout



permissions added to Androidmanifest.xml



output didn't get beyond waiting
 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any error messages in the Android logcat?
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried testing with a browser on the Android device to see if it can connect to the server on the PC?
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman Radder wrote:Are there any error messages in the Android logcat?



No, there are no error messages, no crash dumb (no stack trace either, just waiting)

Norman Radder wrote:Have you tried testing with a browser on the Android device to see if it can connect to the server on the PC?



No, not tested with a browser. The code would have to be altered, but i simply wanted to send from Android device to server code on PC

Please I will appreciate if anyone can spot the anomaly with the code, thanks

BTW how do I get to edit button, wanted to edit original post but couldn't.  

Pleas if you can do help with the the code, i'm a starter but i think anyone very familiar socket programming would find it easy to spot whats missing, thanks
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The code would have to be altered


I ran the unmodified FileServer program on my PC and connected to it with a browser using the 127.0.0.1 address.  It connected and the program wrote the HTTP request into the .jpg file:

GET / HTTP/1.1
Host: 127.0.0.1:57908
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8



The browser connecting will demonstrate that the correct IP address is being used.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The client code from lines 41 to 67  work with the FileServer code on my PC to send and save an image file.
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman Radder wrote:
The client code from lines 41 to 67  work with the FileServer code on my PC to send and save an image file.



It only gets to "waiting" on my own computer, you didn't make any modifications, did you?
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[why is there no way of editing post here?]

Please take me through how it worked with yours , I must be doing something very wrong, something so simple but I can't just see it. I'm a starter so I'm probably getting something so simple so wrong
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Many thanks for you tests, Please if you have an Android can you help test the full code, i need to find out which part is failing on my side. I appreciate it, Thanks
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try connecting with a browser to make sure you have the correct IP address?
I just started the FileServer program on my PC and connected to it from a browser on my Android tablet.  This is what was written to the output file on the PC:


GET / HTTP/1.1
Host: 192.168.1.69:57908     <<<<<<<<<<<< NOTE
Accept-Encoding: gzip
User-Agent: Mozilla/5.0 (Linux; U; Android 5.0.1; en-us; Lenovo TAB 2 A10-70F Build/LRX21M) AppleWebKit/537.16 (KHTML, like Gecko) Version/4.0 Safari/537.16 Chrome/33.0.0.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US
Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7

 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you get the IP address of the PC?  I used the Fing app on my tablet.
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman Radder wrote:How do you get the IP address of the PC?  I used the Fing app on my tablet.



I used to return the IP address

 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Like I said in my original post I'm a complete starter in this area (though a bit experienced in other areas), so I don't what you mean by connect to browser(or better said i don't know how to connect with the browser)

What about these 2 lines would it not return the correct computer IP and bind?

 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the Fing app to see what IP address the PC has?

how to connect with the browser


On the tablet, I opened a browser and entered the URL: http://192.168.1.69:57908
I posted what was sent to the PC and saved by the FileServer program in an earlier post.
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman Radder wrote:Did you try the Fing app to see what IP address the PC has?

how to connect with the browser


On the tablet, I opened a browser and entered the URL: http://192.168.1.69:57908
I posted what was sent to the PC and saved by the FileServer program in an earlier post.



Ok, i never had the Fing app, but i will try and download Fing app asap now and check the PC IP

But in the meantime shouldn't the code below work as well in returning the correct IP? (I'm using wireless connection so its internal IP)
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about that code. I'm not that knowledgeable on network stuff.  I've had good luck using the IP address that Fing returns.
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman Radder wrote:I don't know about that code. I'm not that knowledgeable on network stuff.  I've had good luck using the IP address that Fing returns.



The project I'm working on, all these (including retrieving the PC IP) need to be done programmatically. Doing this manually in the context of my project doesn't make any sense. Except if Fing can work programmatically I don't see the point  
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, as far as i can see, Fing app is useless to this cause, This project obviously is a programmatic process, using a manual too like Fing app is a non-event
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you get an IP address from Fing?  Did you try using that address with a browser to see if it could connect to the PC and send something to the PC?
I got my PC's IP address with Fing.  I started your FileServer on my PC.  I entered the PC's IP address in a browser on my tablet.  The FileServer program received the browser's HTTP GET request and wrote it the a file on the PC.
Conclusion:  I had the correct IP address for a program on the tablet to connect to the PC.

Do you know that you have the PC's IP address?  Did you try to verify it the way I did?
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

all these (including retrieving the PC IP) need to be done programmatically.


How will the Android program get the PC's IP address?
 
keno krydenuu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman Radder wrote:

all these (including retrieving the PC IP) need to be done programmatically.


How will the Android program get the PC's IP address?



I have posted the answer to this already , here it is again- it works and its valid :
And if you want to get it in text format :-  
Java documentation  

If I don't want to obtain the IP programmatically i simply go to whatsmyIP.com
But I want it programmatically so i would use
I really don't need Fing app, the issue with my code is definitely not about retrieving IP (I didn't download)
To be honest I don't really know why your Fing app is useful because retrieving an IP address is the first-step most accessible thing.
More so  at the moment I am using wireless router connection so I only need internal IP which is even relatively straightfoward
If you are talking about available port, thats only slightly a little bit less straight forward
Its kinda funny, I've come to this forum seeking expert help, but i've ended up having to lecture someone about the basics of retrieving an IP address programmatically
I wonder where the socket programming experts on this forum are? Help please, with deep appreciations and gratitude in advance
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why you won't do what I suggest to see if you have the correct IP address for the PC so that the code that executes on the Android device can connect to the PC.
The testing I've done with your code has worked so far.  I have not created an Android app yet and won't until you have verified that you are using the correct IP address for the PC.
Once you have the correct IP address, then move to the next step: find a programmatic way to obtain that IP address instead of using the Fing app.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding IP addresses: If I use the ipconfig command in a command prompt, it returns several different IP addresses (these can also be seen on the Task Managers' Performance tab)


//---------------------------------------------
ipconfig

Wireless LAN adapter Wireless Network Connection:

  Connection-specific DNS Suffix  . : gateway.2wire.net
  Link-local IPv6 Address . . . . . : fe80::bdc9:a519:ec96:52b4%22
  IPv4 Address. . . . . . . . . . . : 192.168.1.69  <<<< NOTE: This is the one I used for testing - it worked
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 192.168.1.254

 
Ethernet adapter VirtualBox Host-Only Network:

  Connection-specific DNS Suffix  . :
  Link-local IPv6 Address . . . . . : fe80::6999:b413:2af2:6eab%26
  IPv4 Address. . . . . . . . . . . : 192.168.56.1             <<<<<<<<<<<< NOTE this one is returned by IP.getHostAddress()
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . :


 
straws are for suckers. tiny ads are for attractive people.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic