Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an HTTP server on android

I've two android applications on same LAN provided by WIFI :

  1. App (A) that open a listening socket on port 8033
  2. App (B) that use HttpClient to access (A) on port 8033

How to make it possible that (A) may do POST and GET requests on (B)?
What the URL used by (A) to access (B) looks like ?

Thanks to All.

like image 512
KhanZeeshan Avatar asked Sep 01 '11 09:09

KhanZeeshan


People also ask

What is HTTP server in Android?

Small but powerful multithreaded web server written completely in Java SE and then ported to Android. The server implements most of the HTTP 1.1 specification and provides custom servlet API that can be used to handle dynamic pages. The servlet API is designed after the official javax.

Can I run Apache on Android?

In this article, we will learn how we can set up Apache Web Server On Any Android Device. For doing this we have to install the Termux app available on the play store. Termux is an Android terminal emulator and Linux environment application that works directly with no rooting or setup required.


2 Answers

You may confuse two different level of networking communication.

Level 4 : TCP connection between two sockets.
A logical pipe between to side (may be single(two process?) or two different computers) only connection handling data are handle at this level

Level 7 : Browser / Application Server used particular communication "language" to exchange high level data (file , images, audio ..) and is handled at this level.

Your question is about to open a Listening Socket (level 4) and a client that talk with it with a HTTP protocol (level 7).

So you're miss to fill the gap socket listening side to handle HTTP protocol
May be a java web server implementation may help you.

  • a Java EE way to have HTTP server : "Tiny Java Web Server" @ http://tjws.sourceforge.net/ that may help you for what you're looking for

  • an "Apache" way to have HTTP server http://www.androiddevblog.net/android/a-bare-minimum-web-server-for-android-platform

    see http://en.wikipedia.org/wiki/OSI_model#Layer_3:_Network_Layer to have more complete view of these Communication Layer

like image 59
Emmanuel Devaux Avatar answered Oct 08 '22 06:10

Emmanuel Devaux


Listening on a port and accepting socket connections isn't enough to serve data back to HttpClient. Sockets in effect provide a physical pipe but know nothing about the format of the data that's flowing along that pipe. If you are set on using HttpClient, then you'll need to have your server application understand HTTP protocol (or at least a very basic subset of it).

If all you need is to have two processes communicate in some way, you may be better off having your server app be a service and then your client app interrogate this service for the required data.

like image 39
Aleks G Avatar answered Oct 08 '22 05:10

Aleks G