File Exchange

image thumbnail

Web Server

version 1.4 (104 KB) by

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

39 Downloads

Updated

View License

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/

Comments and Ratings (33)

Hey I am very new to MATLAB. I found this useful piece of code. Please tell me what should the variable 'config' be declared?

I try to make the webserver run a M file, but webserver(4000, 'file.m') doesn't make it ...
Did I misunderstand something ?

I don't know if I really understand the purpose of this webserver, but I have my own website and would like to allow people (my website's visitors) to run little funny Matlab scripts or commands directly on my website. µ
Can I use such webserver to do that ?

Hi. Thanks for this nice application. I have a problem in photo uploading. I select an image named 1.jpg. Whatever I write in box "Title of your Image", I get the following error after sending:

Error using fwrite
Invalid file identifier. Use fopen to generate a valid file identifier.

I went through the code and it seems that it doesn't save the image in any folder. Then, it creates a random number and assigns it to the image's name. But , finally, it can't find the image. (see file multipart2struct.m, lines36 to 41).

How can I solve this problem?

avcsw pk

Aaron Barker

This webserver is fantastic. Using it to provide some MATLAB analysis functions to co-workers.

Does this support serving css or am I missing something? I don't seem to be able to use it.

Edgard Salas

Hi, A favour, when I write in portuguese language some letters (for example: ç,õ,í,á, ) does not appear when the text arrives to the matlab. How can I resolve it?
I thank you since now for your help.

Ron

Ron (view profile)

Wish MATLAB could be used for web application like other framework e.g., web2py. This submission shows its possible to use MATLAB as a front end for the framework. Wondering if any plans to move fwd to something bigger, like ORM for django?

Ron

Ron (view profile)

Jan Balaguer

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*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.

CdC

CdC (view profile)

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.

CdC

CdC (view profile)

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.

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?

Rahim

Rahim (view profile)

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?

Yves

Yves (view profile)

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.”

Kirill

Kirill (view profile)

Thank you

Omar Shenawy

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

Suresh Deoda

Great work, thanks a lot.

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*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

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!

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

Changshun Deng

Does this need appach or IIS?

Excelent piece of code, very useful !
Thanks

Ernest

Ernest (view profile)

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.

today

today (view profile)

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

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

I can confirm it works well.

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

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

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*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.

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

1.4

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

1.3

Update for ice-browser

1.2

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

1.1

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

MATLAB Release
MATLAB 7.10 (R2010a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video