5.0

5.0 | 11 ratings Rate this file 63 Downloads (last 30 days) File Size: 104 KB File ID: #29027
image thumbnail

Web Server

by

 

14 Oct 2010 (Updated )

HTTP Matlab Web Server, allows m-files to be executed in a browser

| Watch this File

File Information
Description

This function WEBSERVER, is a HTTP webserver for HTML code and images and also allows execution of Matlab code through the web.

   webserver(port)

 Supports
   - HTML and images
   - Sub-Folders
   - Upload files
   - M file execution

 Example,
  webserver(4000);
  % Use firefox or internet explorer and visit http://localhost:4000/
 

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (23)
01 Apr 2014 Dirk-Jan Kroon

*CdC
This software is only a small demo I wrote for fun, and never tested with more than one user.

Probably it is also not really suitable for multiple users, because Matlab is in basis single threaded.

A solution is using a real HTTP server like Apache, in combination with PHP (see easyphp.org).
Then compile your matlab-code to a executable and simply parse the user inputs/outputs to that executable through PHP.

01 Apr 2014 CdC

Thank you again for creating this fantastic tool. It will have a very big impact on our progress.

Question: Is there a way to decrease response latency from the server? We have the server up and running, but we are getting latencies to serve a simple page of about 1 second at no load, and up to 10 seconds at larger numbers of users (still pretty modest numbers in the tens of concurrent users). We would like to use it with a capacity to handle about 100 concurrent users with latencies <1second. Is this possible? Will running on a faster machine, compiling, or other approaches lead to this outcome? Thank you very much for your help.

It turns out that the post below from 30 Mar 2014 saying that the webserver was not working results from the fact that the webserver doesn't work with Matlab2007b. On newer Matlab versions webserver works fine (2010, 2014).

Thanks again.

30 Mar 2014 CdC

Thanks for contributing, as this looks like it could be an important application for us.
Unfortunately, so far the code is not working for me. Help would be greatly appreciated.

I downloaded and unzipped the files and subfolders into a directory, went to that directory in matlab, and typed the sample commands, eg webserver(4000) or webserver(4000,config) after setting up config variable. The webserver seemed to have started, and created a figure window. I then went to chrome and firefox and typed http://localhost:4000, but I got the error that the connection timed out / no data received. I tried again with port 4001, same error. This is on a windows pc. Note this is with Matlab 2007b. Thanks for your help.

09 Mar 2014 MatlabNewbie

How can i get the Multipart POST to work for binary data that i'm sending? I'm not sending a file but a 8 bit grayscale PNG image byte array. The code is failing completely in text2header. Is this even supported?

24 Feb 2014 Rahim

Where would I go about placing my own m file for execution within the code, as in which .m file would I place the call under?

08 Feb 2014 Yves

Very useful. Thank you.

Please note I had to split:
DataOutputStream(TCP.outputStream).write(data,0,length(data));

into 2 lines:
tmp=DataOutputStream(TCP.outputStream);
tmp.write(data,0,length(data));

To avoid the error:

Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.”

11 Oct 2013 Kirill  
14 Aug 2013 taoufik wassar

Thank you

23 Jun 2013 Omar Shenawy

This is just perfect in every manner. Awesome piece of code that is well designed and works perfectly :-)

19 Jun 2013 Suresh Deoda

Great work, thanks a lot.

11 Oct 2012 Dirk-Jan Kroon

*Filipe S.
A Matlab GUI/Figure is user-interface thus client-side, this is a webserver thus server-side.
The only way to do client-side things like showing a GUI, is manually coding the GUI in javascript, html and css

10 Oct 2012 Filipe S.

hi!
i'm trying to use this to put a matlab gui on a web page. However, i'm stubling on how to call the .m file to call the gui. Can you help me on how to do this?

thanks!

02 Aug 2012 Michele Parmigiani

Excellent work with many useful applications.

Anyway I experienced one problem and I hope you can help me to understand how to solve it.
Web server application runs very well on my laptop (Matlab7.11.1.866 (R2010b) SP1), but when I try to run in my virtual machine (Matlab7.1.0.246 (R14) SP3) the server starts, but when I try to connect the brower start to load the page but it stops and the display message is the following:

Webserver Available on http://localhost:4000/
GET / HTTP/1.1

Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/xaml+xml, */*

Accept-Language: en-gb

User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET4.0C)

Accept-Encoding: gzip, deflate

Host: localhost:4000

Connection: Keep-Alive

Get: [1x1 struct]
Content: [1x1 struct]

HTTP/1.0 200 OK
Date: Tue, 12 Oct 2010 09:19:05 GMT
Server: Matlab Webserver
X-Powered-By: Matlab7.1.0.246 (R14) Service Pack 3
Set-Cookie: SESSID=5322082bf473207961031e3df1f45a22; path=/
Expires: Thu, 19 Nov 1980 08:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Connection: close
Content-Length: 336
Content-Type: text/html; charset=UTF-8

I tried to locate the source of the problem and it seems that after the first while(true) the script enter in:
TCP=JavaTcpServer('accept',TCP,[],config);
and it get stuck into:
while(true),
try socket = TCP.serverSocket.accept; break; catch, end
drawnow
if(~ishandle(TCP.stopwindow)), TCP.socket=-1; return, end
end

Any suggestion is very welcomed!

Best Regards,

Michele Parmigiani

09 Feb 2012 Changshun Deng

Does this need appach or IIS?

18 Oct 2011 Mathias Bustamante

Excelent piece of code, very useful !
Thanks

28 Sep 2011 Ernest

Hi.. how do I automatically reset the webserver whenever I encounter this error:

Java exception occurred:
java.net.SocketException: Software caused connection abort: socket write error

That is, I dont need to go to matlab and re-run webserver(80) manula in the command window.

Thank you.

30 Aug 2011 Sung  
24 Mar 2011 Adam Leadbetter

Very nice piece of code. It works very well for me.

04 Feb 2011 Aleksandar Pavic

Very interesting, I will test this, I've made php-console bridge on linux...

16 Dec 2010 Nicolas Billard

I can confirm it works well.

25 Nov 2010 Dirk-Jan Kroon

*Nicolas Billard
Today I uploaded the version which solves your problems (will probably be online tomorrow) ...

27 Oct 2010 Dirk-Jan Kroon

*Nicolas Billard

Thank you for your comments.

The Java-error is due to earlier usage of the port without correctly closing/disconnecting the port due to a code-crash or other problem. In the future I will probably try to solve that by storing the port in a Matlab global, making it possible to close the port after a code crash.

I didn't experience the refresh problems my self. But the code is very experimental, and still more like a prove of concept. Hopefully i will find time to improve the code in the near future, or maybe somebody else will create his own project from this with more stable code.

18 Oct 2010 Nicolas Billard

It's interesting project apart some stability problems.

The following message is often displayed (when i launch webserver first time, it's ok. If i stop it and run it again, it's rain of messages) :

??? Java exception occurred:
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
Error in ==> JavaTcpServer at 10
serverSocket = ServerSocket(data);
Error in ==> webserver at 33
TCP=JavaTcpServer('initialize',[],port);

Other problem, i must refresh sometimes until almost 10 times before the server executes my url.

If you have any suggestion to improve the stability, I'm very interested.

Updates
25 Nov 2010

Fixed Java-Port error due to code crash, and fixed refresh bug by waiting longer for the data. Problems where reported by "Nicolas Billard"

25 Nov 2010

Now it also works with the "ICE-browser" in Matlab. Also added error information and config options.

29 Nov 2010

Update for ice-browser

18 Oct 2011

Solved:
- Chinese character-set bug (reported by Ernest)
- Try and catch around java connection error caused by IE
- added config variable to function-header

Contact us