How to implement an HTTP server on android

前端 未结 3 1922
时光说笑
时光说笑 2020-12-31 17:10

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
相关标签:
3条回答
  • 2020-12-31 17:33

    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

    0 讨论(0)
  • 2020-12-31 17:48

    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.

    0 讨论(0)
  • 2020-12-31 17:57

    I was looking for the same solution and found NanoHTTPD - an extremely simple HTTP request handling server class. You can find the source and very easy to understand samples at https://github.com/NanoHttpd

    0 讨论(0)
提交回复
热议问题