Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seeing a web server over a socket . . .

Tags:

android

tcp

wamp

Currently I support a client with the following architecture, used in an industrial manufacturing process:

  1. They have a Windows program running on a PC that controls the industrial machinery.

  2. They have a proprietary app (which I maintain for them) running on an Android device (basically a phone) which interfaces wirelessly with the PC software over a TCP socket, so it can remotely control those industrial processes.

What the client wants now is a web server running on the PC and a web browser built into the app to control some additional processes not controlled from his Windows program.

I've set up a WAMP server on the PC and a sample web page, which I can see on any browser on the PC as "localhost". I know how to add a web browser View to the Android app via a WebView class.

But I don't know how to make the browser on the phone see the WAMP server on the PC via the TCP connection. How do I hook those two things up?

like image 869
user316117 Avatar asked May 28 '15 15:05

user316117


People also ask

Do web servers use sockets?

An HTTP server is a listening socket that rapidly accepts incoming requests and opens new sockets to handle those requests. The connection socket returns an HTTP response, and closes the connection.

Can a socket be both server and client?

For a server, you usually create a socket, then bind it to a specific port, and accept connections. For the client, you create a socket, and connect to a specified address (an IP address and port pair for a TCP/IP connection). The same device can run a TCP server and client at the same time.

What are sockets in a server?

Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events.

What is WebSockets server?

WebSockets - Server Working. A Web Socket server is a simple program, which has the ability to handle Web Socket events and actions. It usually exposes similar methods to the Web Socket client API and most programming languages provide an implementation.

How do I create a WebSocket connection?

Creating Web Socket connections is really simple. All you have to do is call the WebSocket constructor and pass in the URL of your server. Once the connection has been established, the open event will be fired on your Web Socket instance.

What is web socket and how is it different from http?

What is web socket and how it is different from the HTTP? - GeeksforGeeks What is web socket and how it is different from the HTTP? HTTP and WebSocket both are communication protocols used in client-server communication. HTTP protocol: HTTP is unidirectional where the client sends the request and the server sends the response.

How do I know if a WebSocket is secure?

There is also wss, for secure WebSocket connection the same way https is used for secure HTTP connections. Attaching some event handlers immediately to the connection allows you to know when the connection is opened, received incoming messages, or there is an error. The second argument accepts optional subprotocols.


2 Answers

Some basic information that you should be aware of

When that PC connects to your phone, an underlying network interface must be used, for instance, WiFi or Ethernet. Also note that localhost is lied on loopback interface. It should be noted that loopback interface is only accessible in a device itself (i.e. Other devices cannot communicate with loopback of another device).

In the other side, once an interface is connected, it would be assigned an IP address. I assume that your phone is connected to that PC via WiFi interface, So in this case two interfaces are in use.

  • wlan interface of that PC
  • wlan interface of your phone.

enter image description here

And both have their unique IP addresses. If you want to connect from your phone to that PC you should know IP address of wlan interface of that PC.

If your PC is Linux-based, you could write ifconfig and see that IP address in inet addr field (Under wlan0 section). For Windows machines read this page.


In Android WebView

This View provides a method called loadUrl that is used to fetch HTML content from remote machines. The string you should pass to this method is formated as follows:

http://IP_ADDRESS:PORT_NUMBER

Where

  • IP_ADDRESS : IP address of remote machine. (In your case that one you've found in the previous step)
  • PORT_NUMBER : Each machine can listen on different ports for different purposes (e.g. HTTP, FTP, SSH, ...). Default port for HTTP is 80.

Therefore if we assume that the IP address of that PC is 192.168.0.1, then you should have:

webView.loadUrl("http://192.168.0.1:80");

Or

webView.loadUrl("http://192.168.0.1");
// Because 80 is the default port number for HTTP
like image 92
frogatto Avatar answered Oct 22 '22 08:10

frogatto


Well, how do you connect a webbrowser to a server? On a desktop webbrowser you type the host name or the ip address into the addresss bar of the webbrowser.

A similar processs works for an embedded webbrowser, you just have to call the method loadUrl of your WebView instance.

The more important question what network name your web-server has and prepend http://. If the server gets a static IP address you can use that, too. However you have to make sure the WAMP is not only listening on localhost, otherwise it can not be access from any device (but that is not a question for Stackoverflow).

like image 21
Robert Avatar answered Oct 22 '22 08:10

Robert