Im am converting a program from Basic4PPC to Basic4Android.
That program uses FTP to read a small file from a server. The original program works very well. Now with Basic4Android reading a file results in success = false. FTPConnection closed without indication. I have to specify a port number. The sample code shows 21. Is that port the only one which should work? How to find the correct port number?
Harry
The FTP protocol is defined in RFC 959 and specifies port 21 for connections, with port 20 used for data transmissions as necessary. Whilst you don't have to use port 21 to establish an FTP connection, you should do so unless you have a very good reason to diverge from the standard.
Related
I am trying to add an http server to my custom ROM, which is based on 5.1. I managed to place lighttpd binary in system/xbin via PRODUCT_COPY_FILES in device.mk, and able to run it successfully in device in eng build. Server in listening to port 80.
But in user build, server is not able to bind to 80. When checked, file owner/group is root/shell. Is this the reason port 80 is not allowed ? How to change to root/root while placing the binary ? Is there any alternative method to bind to 80 ?
EDIT-1
Another option I tried is to use iptables.. but again , permission denied.
Is there any way to add an iptable rule in android source and build ?
Also read about 'updating linux configuration' to mark port 80 as a non-privileged port.. but could not figure out how to so this.
Thanks in advance.
You should not leave it running as root on port 80. this may end up in a security breach.
Either run on a higher than 1024 port, or read this answer , especially the part that explains how to use su
in my current project it is a requirement to send a file from a windows computer to an android device over bluetooth without anything on the phone other than it's standard state and of course a paired bluetooth connection. i've looked over pybluez and it seemed simple enough to send files between a client and server architecture (and in fact got it sending between my laptop and desktop rather quickly) but I cannot for the life of me find any way to get python to send a file from the computer to android once the connection is established; my attempts have been grabbing the bluetooth mac address like thing from the device like so
nearby_devices = bluetooth.discover_devices(
duration=8, lookup_names=True, flush_cache=True, lookup_class=False)
and then later trying to send the files like so
port = 1
for addr, name in nearby_devices:
bd_addr = addr
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("download-app")
sock.close()
Of course with the example script given by the pybluez documentation I can seamlessly send files between a client and a server but I am still stuck without a way to send a file to the selected android device (even if I specify it's address and know it's within range)
You're most of the way there...
As you know, you need something to talk to at the other end of your Bluetooth connection. You just need to replace your custom server with a well-known service (generally one of these options).
In my case, my phone supports the "OBEX Object Push" service, so I just need to connect to that and use a suitable client to talk the right protocol. Fortunately, the combination of PyOBEX and PyBluez does the trick here!
The following code (quickly patched together from PyOBEX and PyBluez samples) runs on my Windows 10, Python 2.7 installation and creates a simple text file on the phone.
from bluetooth import *
from PyOBEX.client import Client
import sys
addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)
service_matches = find_service(name=b'OBEX Object Push\x00', address = addr )
if len(service_matches) == 0:
print("Couldn't find the service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()
Looks like PyOBEX is a pretty minimal package, though, and isn't Python 3 compatible, so you may have a little porting to do if that's a requirement.
I haven't personally explored it but check out this blog -
http://recolog.blogspot.com/2013/07/transferring-files-via-bluetooth-using.html
The author uses the lightblue package as an API for the Obex protocol and send files over the connection. Now the lightblue package appears to be unmaintained. There are other packages like PyObex (which I could not import for whatever reason) which you could also explore as alternatives but lightblue seems to be the way to go.
I have made a Python 3 port of PyOBEX based on the PyOBEX code on bitbucket. I've tested so far only the client functionalities, but I expect the server to be working fine as well, since most of the compatibility issues with Python 3 were due to struct.pack/struct.unpack binary blobs appended to strings that should have all been tackled.
Hi Guy's I'm new to programming Android device's I do have python, java, C#, C, C++, PHP, Bash and Visual Basic Experience but I'm new to this block programming, and I haven't done much work with forms. I'm trying to get make an application that posts data to an external IP. I have successsfully wrote a server and a windows based client, clicking buttons in my windows client posts data to the ip 192.168.1.9 port 9999. This is just in the testing phase to remote control a bunch of beaglebone gpio's. So far I've had great success with the windows side. In app inventor for android, However, I've created a series of buttons and tabs, different buttons post text or post and poll for response. The problem I have is that I can directly attach the web connector to 192.168.1.9 but when I add in the port 9999 it tells me the address is incorrect. The method I'm using is
when Screen1.initialize
do set Web1.Url to "http://192.168.1.9:9999"
when Button1.Click
do call Web1.PostText
text > 0
Again, if I type in just the IP of the beaglebone I see its ethernet port go crazy when I click button1. It does nothing when I add in the port. Of course my server is running on 9999 since port 80 is reserved for the internet. Any suggestions?
I would like to suggest you a two-step solution.
Step 1:
Problem>>Develop an android app which is capable to communicate via TCP-IP.
Solution>> I hope you are familiar with MIT-APP Inventor-2. Import an extension called ClientSocket extension V0.4.3 available here to app. Thanks to the developer of the extension.
Step 2:
Problem>>A server responding client request.
Solution>> I have written a Python code.
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 9000 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
print (host)
s.listen(5)
while True:
c, addr = s.accept() # Establish connection with client.
data=(str(c.recv(1024)))
print data
conn.commit()
c.close()
cur.close()
Hope this will help.
I tried at making a socket with an Android NDK wrapper, passing the file descriptor to Java to be used with a recv wrapper. The target user should not require Single User. The recv call does not receive any data. Why?
TCP sockets can not be read without a connection, because they require sequentiality. UDP packets were not being received either. RAW sockets were, of course, not being successfully made. My code had more bugs than a restaurant's dumpster, I was calling shutdown instead of close... not sure why. It's April fools, and usually I delete questions once I realize I'm being an idiot and wasting people's time; but today I have a bounty on this question, so I think I'll revisit this idea for a bit and post better code. Check back later if you're still interested.
Android doesn't support reading from unconnected sockets.
TCP doesn't support reading from unconnected sockets, or writing to them either. Your socket is of type SOCK_STREAM, and you are calling shutdown() on it, so it must be a TCP socket.
NB:
shutdown() doesn't close the FD. You must also call close().
recv() returning -1 doesn't necessarily mean 'socket closed'. You need to look at the value of errno to decide what it does mean.
Your title 'Java Native Socket Not Persistent' has nothing to do with your question.
I need to get my app to play a video file located on my network. I know the url of the file is:
http://something.local/abc.mp4
Now, when I manually substitute "something.local" with its true ip address, the MediaPlayer has no problem playing it. Nonetheless, when I have the above address, the MediaPlayer errors out with error (1, -1007).
So I'm assuming this is because Android doesn't understand "something.local" as being correct.
My question is: How can I "translate" something.local into an ip myself, so that I can then pass it into MediaPlayer?
A small caveat: I believe that MediaPlayer does not work with IPv6 addresses, so please keep that in mind...
Just a side note, in case it makes my situation clearer: When I run ping something.local -4 in the Windows command prompt, it returns:
Pinging something.local [192.168.1.126] with 32 bytes of data:
Reply from 192.168.1.126: bytes=32 time=145ms TTL=64
Reply from 192.168.1.126: bytes=32 time=112ms TTL=64
Reply from 192.168.1.126: bytes=32 time=32ms TTL=64
Reply from 192.168.1.126: bytes=32 time=169ms TTL=64
That translation where windows went from something.local -> 192.168.1.126 is what I want to do in my Android app.
Firstly, you need read document about Bonjour (iOS term) or Zero Config (Linux term).
To understand what's something.local:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/about.html#//apple_ref/doc/uid/TP40002458-SW1
For example, if a user types steve.local. into a Web browser, this
tells the system to multicast the request for steve on the local
network instead of sending it to the conventional DNS server. If a
Bonjour-enabled computer named steve is on the local network, the
user’s browser is sent the correct IP address for it. This allows
users to access local hosts and services without a conventional DNS
server.
For how to resolve it:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/NetServicesArchitecture.html#//apple_ref/doc/uid/20001074-SW1
For java library, previous answers provided good enough example.
You should try this snippet with jmDNS library api.. may need some changes.
JmDNS jmdns = JmDNS.create();
DNSEntry addressEntry = jmdns.getCache().getDNSEntry(name, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
if (addressEntry instanceof DNSRecord) {
ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(true);
if (cachedAddressInfo != null) {
for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
// use the `address`
}
}
You have access to java,net APIS on android and can use them to resolve adresses.
http://docs.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html
However, success will depend on network proper configuration. Your device receives DNS server setup via DHCP - so you are at mercy of network provider