File Exchange

image thumbnail

TCP/UDP/IP Toolbox 2.0.6

version 1.1 (211 KB) by

TCP/IP connections or UDP packets in MATLAB

50 Ratings



View License

Editor's Note: TCP/IP connectivity is also included in The MathWorks Instrument Control Toolbox:

TCP/UDP/IP Toolbox 2.0.6
Example of remote tcp-ip connection:
    pnet(con,'printf','Hello world!\n');

This toolbox can be used to set up TCP/IP connections or
send/receive UDP/IP packets in MATLAB. It can transmit data over the Intranet/Internet between MATLAB processes or other applications. It is possible to act as server and/or client and transmit textstrings, arrays of any datatype, files or MATLAB variables.

NOTE 2015-05-08:
   This toolbox has not rely been updated towards latest versions of MATLAB, I know that the MEX API has some minor changes that may need updates in this pnet tool. Maybe some adjustments i c code and recompile towards latest API (mex.h). I do not have any license of latest matlab versions. Maybe some one can support me with that, and I will upload it as updated package. /Peter Rydesäter

New in 2.0.6 2008-03-06

  -Included a precompiled pnet.mexw32 for support to in matlab R2007 (R2008?)
   No need to compile pnet.c to run at the R2007 platform.

NEW in 2.0.5

More "proper" license specification makes it possible to be useded as (dynamical) linked extention to non-free stand alones of your MATLAB commercial applications.
The precompiled pnet.dll is possible to run in matlab 5.x for Windows.

NEW in 2.0.4
* THE bug when running at Windows platform is removed.
* pnet_remote can call arrays of slaves with just one call.

NEW in 2.0.3
* Support for the single datatype.

NEW in 2.0.2
* Important Bugfix for UDP support.
* Calls to drawnow makes blocking operations breakable also in windows.

NEW in 2.0.1
* Bugfix for windows version, Sleep() delay is div. with 1000 to correspond to unix sleep().

NEW in 2.0:
* UDP packets.
* FAST Datatype support for int*, uint*, double, char.
* FAST INTERNAL byte swapping.
* UDP/IP-demos.
* TCP/IP-demos.
* WEB-SERVER demo.
* New nonblocking remote "eval" of matlab expression on remote host.
Tested to work with MATLAB 5.3/6.x for Linux, Solaris, Windows Compiled mex files for tested platforms included. C-source should compile under any Unix/Windows system. Also basic support for remote evaluation and paralelization of MATLAB is included.

This zip package includes the zip package containing version 1.2.4 with mex files (*.dll) for backward compatibility.

Toolbox by:

Peter Rydesäter

With support from Mario Bergeron & Mike Medeiros

Any donations from commercial use are welcome to sponsor this project.

TCP/IP connectivity is also included in The MathWorks Instrument Control Toolbox:

Comments and Ratings (85)

Darpan Damani

Please follow the method suggested by jienan ding. It works for windows 7.
Just update the path, in my case it was
mex -O pnet.c 'C:\Program Files\MATLAB\R2016b\sys\lcc64\lcc64\lib64\wsock32.lib' -DWIN32

Thanks Jienan, you saved a lot of my time

Richard Crozier

Nice work, but is it possible to use pnet to connect to a local unix file-based socket specified with a path? This would be very useful to me.

Zanyu Chen

I am running this on Mac OD EL Capitan with MATLAB R2015a, and it's AMAZING!!!

This is million times WAY BETTER that the built in MATLAB UDP class. Million thanks for the author.

It solved critical problems for me!


Janez (view profile)

Leo flysh

I have a problem compiling pnet.c, I have Matlab r2014b x64. Using LCC like this:
mex -O pnet.c 'C:\Program Files\MATLAB\R2014b\sys\lcc64\lcc64\lib64\wsock32.lib' -DWIN64
(I changed #ifdef WIN32 to #ifdef WIN64.)

I get this error every time:
fatal error C1083: Cannot open include file: 'winsock2.h': No
such file or directory

It wouldn't work with DWIN32 either.

I have looked into my computer and I have the file. Any suggestions?


Pete (view profile)

Is it possible to use this toolbox to communicate with a Redpitaya board without access to the Instrument Control Toolbox? If so, does anyone have suggestions about this topic?

Having a pretty huge problem this that took a very long time to track down. All incoming UDP data is being truncated at 7 bits. In other words I send bytes of 150 170 from another machine, and pnet gets the packet and calls it 127 127. Looking at pnet.c I seem some double casting (char)(unsigned char) which looks a little suspicious. Has any one else seen this?


did anyone of you manage to run the udp sim daq example on a mac os 10.9 with matlab 2012a? I managed to compile the c-file. but everytime i run the transfer script subsequently to the sim_daq script, the answer is as follows:
Send request...


SIM_DAQ Waits for request...
 SEND respons:
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,END 0.517621 s, 0.74 MBytes/s

Any suggestions? Thanks in advance!

Stuart McGarrity

Stuart McGarrity

Tim Buschman

Works on 64-bit R2015b with MinGW compiler (these instructions are very helpful, with some edits: ). Had to include winsock library (thanks jienan!) otherwise failed.


jienan: what error were you able to fix with this method?

jienan ding

Just get it worked under 64bit win7. Try this command.

mex -O pnet.c 'C:\Program Files\MATLAB\R2014b\sys\lcc64\lcc64\lib64\wsock32.lib' -DWIN32

You might need to change the location of you wsock32.lib.

nelly p

I have had this working well in matlab 2011a. An update to 2014b has caused serious errors...Has anyone had difficulties with running this toolbox in matlab 2014b?

Sanjoy Basak

hi, Could anyone please tell me how to install the tool on ubuntu 14.04LTS? I am not really being able to use it.

Hi Peter. Great work, thank you for maintaining this. As others have noted, in Linux, mex / gcc gets tripped up when compiling pnet.c, because the code uses '//' style comments, which are non ansi-standard. to get around this when compiling, use:

mex -O CFLAGS="\$CFLAGS -std=c99" pnet.c

Hope this helps.


Justin (view profile)

@ Yihui

It doesn't look like there is another option. The lag is roughly 5-45 m, but the vast majority of this lag looks like it comes from the actual writing of the data itself, meaning the read to buffer is relatively insignificant


Yihui (view profile)


  I am trying to sending commands from my console via a UDP port to matlab where my instrument is controlled.

  When I send a command (as package) from my console, on the matlab side I always have to run 'readpacket' and 'read' to get it.

  Is it possible to skip that?

Thanks a lot!

Clayton Chu

Is there a way to select what the source IP should be?


great tool,
how ever i have a couple of issues:
when i do:

i get:

con =

also when the i first issue a pnet command at startup i get :

Warning: In the directory "C:\Documents and Settings\Administrator\My
Documents\Dropbox\matlab_new\tcp_udp_ip", pnet.mexw32 now shadows pnet.dll.
 Please see the MATLAB 7.1 Release Notes.

Can I somehow use this toolbox for matlab 2006a and if so how? and how do I effectively install the toolbox?

If not, does anyone have suggestions on how I can send udp port signals without having to write a code from scratch... CHeers

amjad ali

can any one help me that how to interlink matlab with gnu radio


Abdu (view profile)

I can only say that this toolbox is a great service for the world...


JIE (view profile)

I compiled it successfully on Ubuntu 9.1.
However, it always fail compiling on Ubuntu 10.04.
Anyone tried compiling this on Ubuntu 10.04 or above and succeed?


Casper (view profile)

using the command "mex pnet.c ws2_32.lib" generates an error, and I'm not well-versed enough to find the problem by myself (I'm an intern).

Would it be possible for anyone to upload an x64-version?

I compiled pnet.c with the Matlab 2011a mex compiler using the visual studio 10 c++ compiler on Windows 7 / 64-bit using the command:

"mex pnet.c ws2_32.lib"

Seems to work fine so far. I also changed line 64 from

Edgar Guevara

Alex Zhang

I believe this problem of Windows 7 has been solved by installing the visual c++ and the SDK together.

Alex Zhang

Really need help on how to compile this Toolbox PNET.c for 64 Windows 7? Or does this PNET.c need to be ported for windows 64bit?


Leo (view profile)

I got error message "[recvfrom() or recv()]: WSA error: WSAEMSGSIZE" on UDP receiving. Any suggestions?

Kotya Karapetyan

I wonder if anyone could help me with compiling this at a Windows x64 platform? How do I do it? I am working on Windows 7 and I have never compiled any c-code in my life. Or may be someone just has this pnet compiled for x64?

Kotya Karapetyan

anon donor

I was able to achieve >512mbps (that's 0.5 gigabit/sec) of read or write by modifying pnet.c as follows, and recompiling the mex extention:

---- edit of pnet.c start here----------------
int tcp_connect(const char *hostname,const int port)
+ int optval;
+ int result=0;

return -1;
    con[con_index].fid=socket(AF_INET, SOCK_STREAM, 0);

+ optval=1;
+ result = setsockopt(con[con_index].fid, +IPPROTO_TCP,TCP_NODELAY,(char *) &optval,sizeof(optval));
+ optval = 512*1024;
+ result = setsockopt(con[con_index].fid, SOL_SOCKET,SO_SNDBUF,(char *) &optval,sizeof(optval));
+ result = setsockopt(con[con_index].fid, SOL_SOCKET,SO_RCVBUF,(char *) &optval,sizeof(optval));

---- edit of pnet.c ends here----------------

 with the modified pnet, executing pnet read/write calls using a buffer of 512kbytes gave very high throughput.

Thank you very much peter , for contributing this and sharing
the source, it's been very useful.

Igory iBass

If anybody had such wrong like a 'pnet_sendvar bla bla bla', correct youself pnet_putvar M-file.
 - Write "pnet_putvar" instead of "pnet_sendvar" on LINE 46.

Colin Selleck

Anyone have luck running this under MAC OS 10.6.2 Snow Leopard? It seems to put MATLAB in an infinite busy state.

Chris Volpe

When the latest version loads,the console output claims that it is version 2.0.5, as that's the version that's hard-coded into pnet.c. Is this merely a cosmetic error?

Georg Stillfried

Kostas Grunder

That did the trick. Thank you very much!

Georg Stillfried

To compile pnet.c under Linux (9.04 Ubuntu) in Matlab 2009b you must make sure the comments // are changed into /* ... */


// Comment Line


/* Comment Line */


mex pnet.c

it only gives me a warning

Warning: You are using gcc version "4.3.3-5ubuntu4)". The earliest gcc version supported
         with mex is "4.1". The latest version tested for use with mex is "4.2".
         To download a different version of gcc, visit

A bit strange but it does compile


Dan (view profile)

A great application. Just one question ... in the MATLAB version of tcpip in the instrument control toolbox, there is a parameter called 'OutputBufferSize'. There does not seem to be any kind of analogous parameter for this package. Is the OutputBufferSize dynamically sized or is there a fixed value that I should know about? When responding, please consider me to be a beginner when it comes to TCPIP ... a 'tcpip for dummies' response would be appreciated :)


Dan (view profile)

Georg Stillfried

I did not manage to compile the c-file pnet.c. I am running Matlab 2009a on a Linux machine with a 2.6.27 kernel and a 4.3.2. gcc compiler. Can anyone help me?

P.S. I recieve the following error messages:

>> mex -O pnet.c

Warning: You are using gcc version "4.3.2". The earliest gcc version supported
         with mex is "4.1". The latest version tested for use with mex is "4.2".
         To download a different version of gcc, visit

pnet.c:104: error: expected identifier or ‘(’ before ‘/’ token
pnet.c:170: error: expected identifier or ‘(’ before ‘/’ token
pnet.c: In function ‘newbuffsize’:

[... many more error messages...]

pnet.c:1158: error: expected expression before ‘/’ token
pnet.c:1174: error: expected expression before ‘/’ token

    mex: compile of ' "pnet.c"' failed.


Goola (view profile)

I want to implement 802.11 MAC layer in matlab
is there anyone who knows any useful and related toolbox or m.file?

Kevin Bartlett

I have created a simple program for sending and receiving UDP packets. It isn't as feature-rich or flexible as the TCP/UDP/IP Toolbox, not having support for TCP packets or able to handle large volumes of data, but it may be sufficient for some applications.

Search the File Exchange for "py_udp".

can some one please help me in using this tool box...


Edward (view profile)

I can't get the program to run. When executing the examples, I get the error message

symbol mxCreateDoubleMatrix, version libmx.INTERNAL not defined in file with link time reference.

An email to the author two weeks ago has not been answered.


Nice work. Thank you.

Kevin Bartlett

Got errors trying to compile under RedHat:
pnet.c:104: error: expected identifier or ?(? before ?/? token
and so on. Fixed by editing pnet.c to change all "//" comments to the "/* */" comment format (for example, the comment "// Grow...." becomes "/* Grow....*/"). Now udp_send_demo works on Linux to send data to another Linux machine running udp_plotter_demo. Great!

HOWEVER, still can't get my Windows machine to send data to the Linux machine. udp_send_demo creates a variable "udp" with a value of 1 (i.e., a valid socket, I think); then


returns a value of 328 (presumably the number of bytes sent?), but nothing shows up at the Linux machine running udp_plotter_demo. Has anyone else had a similar experience?


Imanol Cruz

Imanol Cruz

Waiting for a manual

Nikola Toljic

Excellent. Thanks.

Francesco Riganti

Excellent functionality, very useful.

Felipe Braga

Excelent package. I have a really simple doubt that maybe you can help me. I can send messages via this socket, I can establish the connection between my client application and the socket but I cannot read the messages sent from my client application to my MATLAB socket. What do I have to do to read and store this information?

Thanks for the help

Stephen Morris

This is an excellent package and covers the basics very well. However, I found it didn't quite cover my needs because I couldn't find a way of generating events (i.e. calling a callback function whenever data becomes available at a port). If there is in fact a way of doing this then the author can correct my comment, but in the meantime I have successfully used a third-party ActiveX control (SocketWrench from Catalyst Software) that provides this functionality for <$100.

Ulrik -

Why pay for something you can get for free :-)

Steven Kolak

MATLAB now directly supports these protocols through the Instrument Control Toolbox. Visit the TCP/IP and UDP web pages for more information and a downloadable MATLAB code example:

Jim Tonti

Excellent work! Ran tcpipmex.dll (tcpip -1.2.4 and associated open, read, write, status) without re-compiling under MATLAB V7 under WinXP SP2. This was a real time saver.

CB Williams

Does the trick...

Daniel Hrach

Andrew Meyer

Tried to compile a similar toolbox on top of winsock sockets with no success..this was exactly what i was looking for!

Arne Solstad

Very nice tool. Found it necessary to recompile for use in Windows XP/Matlab 7.3 and 7.4, but that was very straightforward - just follow instructions in header of pnet.c (one single mex command).

Rousseau Jean-Michel

Very flexible an independant tool.

Danish Hashmi

nice tool

Ravi Paike

Excellent Tool. Thanks

hui lei


furkan turkmen

good job..

chow wai theng

Can I use it to connect to IIS on Windows 2000 and exchange variables or parameters (in string or text) to my ASP page?
Can it functions like a Web Server connection?

marc frown

there is no sendvar file in the zip file i downloaded
any help?

Frans Cornelissen

This toolbox will compile fine and work perfectly on mac os X as is. Yeah! Tested with matlab 7.0.1. on 10.3.2. Just use the mex command with the main pnet.c file and it will generate a pnet.mexmac.

Jesus Lopez

I was looking for a way to do TCP/IP communication over MATLAB and this toolbox gave me the solution I needed.
Maybe it's necessary to improve the documentation and to add some examples.

nick werle

hackers cryptic unusable code is worth what you pay for it.

SW engineering is not doing 1 line of APL code that noone can maintain

ren wei

it is a remarkable work and just meet my needs.But there is something wrong when i tried to 'close' a connection, for the server showed 'busy' even if a command "pnet_remoet(con,'close')" was done in the workspace of a client . waiting for the answers! thank you !

alex keuch

Exactly what i was looking for. Thank's a lot.

Merrill McKee

Wonderful. Thanks.

Arne Solstad

Excellent functionality, and easy to use. Nice work!

Jeff Shafer

Very nice package with good documentation. Many thanks!

Oki Gun

Great - Just What I'm looking for

Mejía C. José Alfonso

All articles are importants & necessary for mi, the problem its on englihs, thanks you. I´m studing Biomedical engineer on México, D.F., Sorry for my bad english, by.

Doug Harriman

Excellent functionality, very useful.

Sacit M. Cetiner

Very sound design; fills a great gap that MathWorks neglects for years.

Can be easily used for both TCP and UDP with a tiny change in command line.

Veit-Stefan Kafka

A very useful tool for interprocess communication. Sorringly there is no windows-dll included in the actual version (1.2), and the .c-file doesn't compile for me with lcc, so I have to use the linux-version ;-)



Added a not about compatibility toward newer MATLAB version.

More "proper" license specification makes it possible to be useded as (dynamical) linked extention to non-free stand alones of your MATLAB applications. The precompiled pnet.dll is possible to run in matlab 5.x for Windows.

major bug fix for Windows platform. THE bug is found. Data is now also transmitted in binary mode at windows platform.

UDP support more stable (random) parameter is now initialized.
Regular Calls to drawnow makes blocking operations breakable in windows.

1.2->2.0, new udp support, much faster, datatype support.
2.0->2.0.1 Bugfix Windows: Sleep() changed.

Major improvemet of implementation.
i.e. UDP support.


MATLAB Release
MATLAB 7.4 (R2007a)

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

» Watch video