<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7960357115971384899</id><updated>2012-02-07T09:51:20.362+02:00</updated><category term='linux'/><category term='arduino'/><category term='ngif'/><category term='animator'/><category term='processing'/><category term='gif'/><category term='web based'/><category term='adxl345'/><category term='cli'/><category term='select()'/><category term='java'/><category term='client'/><category term='php'/><category term='programming'/><category term='free'/><category term='tutorial'/><category term='example'/><category term='graphics'/><category term='copying'/><category term='size'/><category term='art'/><category term='i2c'/><category term='socket_select()'/><category term='cs homework'/><category term='gui'/><category term='c#'/><category term='regex'/><category term='homework'/><category term='socket'/><category term='terminal'/><category term='animation'/><category term='chat'/><category term='accelerometer'/><category term='server'/><category term='stream_select()'/><category term='programming language'/><category term='command line'/><category term='code'/><category term='ubuntu'/><category term='image processing'/><category term='default'/><category term='unzip'/><category term='ide'/><category term='zip'/><category term='pixlr.com'/><title type='text'>Live Fast - Code Young</title><subtitle type='html'>A software engineer's blog</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-4097109041638116737</id><published>2011-11-17T16:22:00.003+02:00</published><updated>2011-11-17T16:58:35.302+02:00</updated><title type='text'>I'm an Engineer!</title><content type='html'>Haven't posted for a very very long time, but this one deserves at least a tiny post. This blog's subtitles changes from &lt;span style="font-style:italic;"&gt;A software engineering student's blog&lt;/span&gt; to &lt;span style="font-style:italic;"&gt;A software engineer's blog&lt;/span&gt;. Yepee!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-4097109041638116737?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/4097109041638116737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2011/11/im-engineer.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/4097109041638116737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/4097109041638116737'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2011/11/im-engineer.html' title='I&apos;m an Engineer!'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-6128977791097392850</id><published>2009-12-11T19:03:00.006+02:00</published><updated>2009-12-11T19:33:27.693+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ngif'/><category scheme='http://www.blogger.com/atom/ns#' term='free'/><category scheme='http://www.blogger.com/atom/ns#' term='animation'/><category scheme='http://www.blogger.com/atom/ns#' term='gif'/><category scheme='http://www.blogger.com/atom/ns#' term='gui'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='animator'/><title type='text'>C# rocks or how I made the Shmorgif Animator</title><content type='html'>The other day I was looking for a free tool to make .gif animations. I haven't found anything free worthy of using, so I figured I might as well write my own with the features _I_ would like to see in a simple gif animator.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;You can download it &lt;a href="http://shmorgif.cookietroll.com/" target="_blank"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;A quick search brought me to the &lt;a href="http://www.codeproject.com/KB/GDI-plus/NGif.aspx" target="_blank"&gt;NGif&lt;/a&gt; library page on CodeProject, this is the core code that actually does all the animating, I've simply set up [what I'd like to think is] an intuitive GUI.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://shmorgif.cookietroll.com/ShmorgifScreen.PNG" widht="400" height="307" /&gt;&lt;br /&gt;&lt;br /&gt;Features:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Drag/Drop images on the app window to load new frames.&lt;/li&gt;&lt;li&gt;Change the order of the frames&lt;/li&gt;&lt;li&gt;Remove/add frames&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Preview animation&lt;/li&gt;&lt;li&gt;View thumbs of individual frames&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Pause/Play animation&lt;/li&gt;&lt;li&gt;Change delay between frames&lt;/li&gt;&lt;li&gt;Set custom quality&lt;/li&gt;&lt;li&gt;Set loop on/off&lt;/li&gt;&lt;/ul&gt;Let me know if you want the details&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-6128977791097392850?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/6128977791097392850/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/12/c-rocks-or-how-i-made-shmorgif-animator.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/6128977791097392850'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/6128977791097392850'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/12/c-rocks-or-how-i-made-shmorgif-animator.html' title='C# rocks or how I made the Shmorgif Animator'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-253633976698428288</id><published>2009-11-30T18:03:00.062+02:00</published><updated>2011-11-17T16:16:02.197+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='example'/><category scheme='http://www.blogger.com/atom/ns#' term='arduino'/><category scheme='http://www.blogger.com/atom/ns#' term='i2c'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='code'/><category scheme='http://www.blogger.com/atom/ns#' term='adxl345'/><category scheme='http://www.blogger.com/atom/ns#' term='accelerometer'/><category scheme='http://www.blogger.com/atom/ns#' term='processing'/><title type='text'>ADXL345 accelerometer breakout board + Arduino and Processing</title><content type='html'>Some time ago I've purchased an &lt;a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9156"&gt;ADXL345 accelerometer breakout board&lt;/a&gt; from &lt;a href="http://www.sparkfun.com/"&gt;SparkFun.com&lt;/a&gt;. After some searching and datasheeting (that's an awful term, I know :D), I've finally came up with code, which will allow me to talk to the accelerometer using my Arduino and even pass the data on, to &lt;a href="http://www.processing.org/" target="_blank"&gt;Processing&lt;/a&gt; (it's a fun language, covered in my previous &lt;a href="http://codeyoung.blogspot.com/2009/11/programmer-and-artist-processing-and.html"&gt;post&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;object height="344" width="560"&gt;&lt;param name="movie" value="http://www.youtube.com/v/BJeohcZssBU&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;"&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;embed src="http://www.youtube.com/v/BJeohcZssBU&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="344" width="560"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;In this post I will cover the setup for the communication procedure between the ADXL345 and the Arduino and then, between the Arduino and Processing (ie your PC). All the sources are at the bottom of the post, as usual.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To tell you the truth, I don't know which you should do first, code the Arduino or hook up the ADXL (probably code first), but my fingers were itchy, so I hooked up the ADXL to the Arduino first:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://www.cookietroll.com/blogfiles/adxl/adxl345_arduino_schematics.jpg" alt="Connecting ADXL345 breakout board and Arduino for I2C communication" /&gt;&lt;br /&gt;&lt;br /&gt;Here's how it actually looks in life (click the pics for larger image)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.cookietroll.com/blogfiles/adxl/arduino_and_breadboard.jpg" target="_blank"&gt;&lt;img src="http://www.cookietroll.com/blogfiles/adxl/arduino_and_breadboard_small.jpg" alt="ADXL345 and Arduino" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.cookietroll.com/blogfiles/adxl/breadboard.jpg" target="_blank"&gt;&lt;img src="http://www.cookietroll.com/blogfiles/adxl/breadboard_small.jpg" alt="ADXL345 wiring on breadboard" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.cookietroll.com/blogfiles/adxl/arduino.jpg" target="_blank"&gt;&lt;img src="http://www.cookietroll.com/blogfiles/adxl/arduino_small.jpg" alt="Arduino wiring for ADXL345" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now, after we have everything plugged in nicely, we may start writing the code for getting the acceleration values from the sensor. You might want to have the &lt;a href="http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf" target="_blank"&gt;ADXL345 Datasheet&lt;/a&gt; handy while you code.&lt;br /&gt;&lt;br /&gt;There are two ways to talk to the sensor, SPI and I2C protocols. I've chose the I2C (the schematics above are for I2C communication) because it is easier to wire for, and easier to code - the downside, they say, is that I2C is slower.&lt;br /&gt;&lt;br /&gt;The Arduino &lt;a href="http://www.arduino.cc/en/Reference/Wire" target="_blank"&gt;Wire library&lt;/a&gt; provides a sort-of-easy way to interact with components that implement the I2C communication protocol. I wish I could say it's usage is straightforward, but it is not quite so. There are few catches! Though, if you're aware of them, the library should be easy to use.&lt;br /&gt;&lt;br /&gt;Open page 10 in the datasheet, this page explains how to talk to the device using I2C protocol. When using I2C protocol there are two ways (yes, again two ways) to wire the ADXL345, each way gives it a different device address (so that may prevent address collision). You can see, that the wiring I chose, defines the device address as 0x53, the datasheet also says that this translates to 0xA6 address for write and 0xA7 address for read (because the base device address is 7-bit + 1 read/write bit). &lt;span style="font-weight: bold;font-size:130%;" &gt;Ignore the read and write addresses&lt;/span&gt; as the Wire library takes care of this for us (catch).&lt;br /&gt;Now, at the bottom of page 10 in datasheet, you see a diagram of how reads and writes should be performed. We'll start with the writes because:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;It's what we do first in the code&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Surprisingly, it's easier to do&lt;/li&gt;&lt;/ol&gt;The diagram shows that in order to perform a write to a register on a device, we need to go through the following steps:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Initiate transmission to device (using write address*)&lt;/li&gt;&lt;li&gt;Write the address of the register we want to write&lt;/li&gt;&lt;li&gt;Write the data we want to write to the register&lt;/li&gt;&lt;li&gt;Finish transmission&lt;/li&gt;&lt;/ol&gt;* We only use the base address of the device (0x53) because the read/write bit is handled by the Wire library.&lt;br /&gt;** You will also note that there are 'ack' signals coming from the device, those are too, handled by the library&lt;br /&gt;&lt;br /&gt;Looks pretty logical, and here's a function that will write a value to a certain register on a specified device (you'll need to add  &lt;span style="font-style: italic;"&gt;#include &amp;lt;Wire.h&amp;gt;&lt;/span&gt; to the top of your program to use the Wire library):&lt;br /&gt;&lt;br /&gt;&lt;div id="iframeDiva92f0ab4-0d1f-4460-b6cf-e7ff1e6e4ac5" style="margin: 0px; display: none;"&gt;&lt;iframe id="iframea92f0ab4-0d1f-4460-b6cf-e7ff1e6e4ac5" src="http://www.blogtrog.com/code.aspx?id=a92f0ab4-0d1f-4460-b6cf-e7ff1e6e4ac5" style="border: 1px solid rgb(224, 224, 224); margin: 0px; width:560px;height: 150px;" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id="noIframeDiva92f0ab4-0d1f-4460-b6cf-e7ff1e6e4ac5" style="margin: 0px; display: block;"&gt;&lt;pre&gt;&lt;div&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; writeTo(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; device, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; address, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; val) {&lt;br /&gt;Wire.beginTransmission(device); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;start transmission to device &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   Wire.send(address);        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; send register address&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   Wire.send(val);        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; send value to write&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   Wire.endTransmission(); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;end transmission&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"&gt;&lt;/script&gt;&lt;script language="javascript" type="text/javascript"&gt;bt_code_init('a92f0ab4-0d1f-4460-b6cf-e7ff1e6e4ac5');&lt;/script&gt;So, let's say we want to write the value 8 to register 0x2D on device 0x53 (true story :D), we'll use this function like that:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;writeTo(0x53, 0x2D, 8);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Ok, that was easy. The reading function is a bit longer, but uses similar conecepts, so it's not going to be hard to follow. This time we want to perform a multiple-byte reading (as opposed to single byte writing we did in the writeTo() function). We need to read 6 bytes of data from the sensor (2 bytes for each axis), because, as the datasheet suggests,  we don't want one axis value to change while we're reading another axis' values.&lt;br /&gt;&lt;br /&gt;Lets look at that communication diagram on page 10 of the datasheet again. The steps we are to follow, in order to perform a multi-byte reading, are:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Initiate transmission to device (using write address*)&lt;/li&gt;&lt;li&gt;Write the address of the register we want to &lt;span style="font-weight: bold;"&gt;start** &lt;/span&gt;reading from&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Initiate transmission to device, again! (using read address*)&lt;/li&gt;&lt;li&gt;Read bytes one after another&lt;/li&gt;&lt;li&gt;Finish transmission&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;* Again, we only use the base address of the device (0x53) because the read/write bit is handled by the Wire library.&lt;br /&gt;** When we're doing multiple bytes read (or write) we provide the start address - for example 0x15 - and the number of bytes we want to read - for example 4. That way we'll read values of registers 0x15, 0x16, 0x17, 0x18&lt;br /&gt;&lt;br /&gt;So, let's see the read function:&lt;br /&gt;&lt;div id="iframeDiv822edc51-c108-4ce5-8936-c9d27da26be9" style="margin: 0px; display: none;"&gt;&lt;iframe id="iframe822edc51-c108-4ce5-8936-c9d27da26be9" src="http://www.blogtrog.com/code.aspx?id=822edc51-c108-4ce5-8936-c9d27da26be9" style="border: 1px solid rgb(224, 224, 224); margin: 0px; width: 560px; height: 300px;" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id="noIframeDiv822edc51-c108-4ce5-8936-c9d27da26be9" style="margin: 0px; display: block;"&gt;&lt;pre&gt;&lt;div&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; readFrom(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; device, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; address, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; num, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buff[]) {&lt;br /&gt;Wire.beginTransmission(device); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;start transmission to device &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  Wire.send(address);        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;sends address to read from&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  Wire.endTransmission(); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;end transmission&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;Wire.beginTransmission(device); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;start transmission to device (initiate again)&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  Wire.requestFrom(device, num);    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; request 6 bytes from device&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(Wire.available())    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;device may send less than requested (abnormal)&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  {&lt;br /&gt;buff[i] &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Wire.receive(); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; receive a byte&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    i&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;++&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;Wire.endTransmission(); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;end transmission&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"&gt;&lt;/script&gt;&lt;script language="javascript" type="text/javascript"&gt;bt_code_init('822edc51-c108-4ce5-8936-c9d27da26be9');&lt;/script&gt;The new elements in this function are &lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;Wire.requestFrom(device, num)&lt;/span&gt; - requests &lt;span style="font-style: italic;"&gt;num&lt;/span&gt; number of bytes from &lt;span style="font-style: italic;"&gt;device&lt;/span&gt;. The reading will start from the register passed in the &lt;span style="font-style: italic;"&gt;Wire.send()&lt;/span&gt; call.&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;Wire.available()&lt;span style="font-style: italic;"&gt; -&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span&gt; returns true if there's something to read from the device&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;&lt;span&gt;&lt;span style="font-style: italic;"&gt;Wire.receive()&lt;/span&gt; - reads one byte from the device&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;Essentially, this function performs the reading steps listed above. One thing to note, the result is saved in the &lt;span style="font-style: italic;"&gt;buff&lt;/span&gt; array, and not returned by the function. So you must pass an array to the function and it has to be long enough to hold the data.&lt;br /&gt;&lt;br /&gt;Whew! Ok, we have the read and write functions set. We're ready to write a program that will talk to the sensor (finally).&lt;br /&gt;&lt;br /&gt;&lt;div id="iframeDiv787eefd7-b737-4d9c-a57c-a4e01b93895f" style="margin: 0px; display: none;"&gt;&lt;iframe id="iframe787eefd7-b737-4d9c-a57c-a4e01b93895f" src="http://www.blogtrog.com/code.aspx?id=787eefd7-b737-4d9c-a57c-a4e01b93895f" style="border: 1px solid rgb(224, 224, 224); margin: 0px; width: 560px; height: 340px;" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id="noIframeDiv787eefd7-b737-4d9c-a57c-a4e01b93895f" style="margin: 0px; display: block;"&gt;&lt;pre&gt;&lt;div&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;#include &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Wire.h&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;br /&gt;#define DEVICE (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0x53&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;ADXL345 device address&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;#define TO_READ (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;6&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;num of bytes we are going to read each time (two bytes for each axis)&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buff[TO_READ] ;    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;6 bytes buffer for saving data read from the device&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; str[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;512&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;];                      &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;string buffer to transform data before sending it to the serial port&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; setup()&lt;br /&gt;{&lt;br /&gt;Wire.begin();        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; join i2c bus (address optional for master)&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  Serial.begin(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;9600&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);  &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; start serial for output&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;Turning on the ADXL345&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  writeTo(DEVICE, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0x2D&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);    &lt;br /&gt;writeTo(DEVICE, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0x2D&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;16&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;br /&gt;writeTo(DEVICE, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0x2D&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;8&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"&gt;&lt;/script&gt;&lt;script language="javascript" type="text/javascript"&gt;bt_code_init('787eefd7-b737-4d9c-a57c-a4e01b93895f');&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Most of the code is pretty self-explanatory, so I'll explain only the code after the &lt;span style="font-style: italic;"&gt;//Turning on the ADXL345&lt;/span&gt; comment.&lt;br /&gt;As you can see we are writing three different values to register 0x2D. 0x2D is the Power Control register of the ADXL345 (see datasheet). We first reset the power control register, then put the sensor in standby mode, and last we are putting it in to measure mode. We're doing the writes one after another because that's what the datasheet recommends. We could simply do the last write also. If you don't turn on the fourth bit on (&lt;span style="font-style: italic;"&gt;writeTo(DEVICE, 0x2D, 8)&lt;/span&gt;) the sensor will be in sleep mode, and will give zero readings!!&lt;br /&gt;&lt;br /&gt;And now, finally, the reading loop:&lt;br /&gt;&lt;br /&gt;&lt;div id="iframeDivf5a2eff1-dcf9-4dee-a929-48e07a294b6a" style="margin: 0px; display: none;"&gt;&lt;iframe id="iframef5a2eff1-dcf9-4dee-a929-48e07a294b6a" src="http://www.blogtrog.com/code.aspx?id=f5a2eff1-dcf9-4dee-a929-48e07a294b6a" style="border: 1px solid rgb(224, 224, 224); margin: 0px; width: 560px; height: 380px;" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id="noIframeDivf5a2eff1-dcf9-4dee-a929-48e07a294b6a" style="margin: 0px; display: block;"&gt;&lt;pre&gt;&lt;div&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop()&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; regAddress &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0x32&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;first axis-acceleration-data register on the ADXL345&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; x, y, z;&lt;br /&gt;&lt;br /&gt;readFrom(DEVICE, regAddress, TO_READ, buff); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;read the acceleration data from the ADXL345&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;thus we are converting both bytes in to one int&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (((&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)buff[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;8&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buff[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;];&lt;br /&gt;y &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (((&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)buff[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;])&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;8&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buff[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;];&lt;br /&gt;z &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (((&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)buff[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;8&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buff[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;4&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;];&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;we send the x y z values as a string to the serial port&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  sprintf(str, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;%d %d %d&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, x, y, z);&lt;br /&gt;Serial.print(str);&lt;br /&gt;Serial.print(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, BYTE);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;It appears that delay is needed in order not to clog the port&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  delay(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;15&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"&gt;&lt;/script&gt;&lt;script language="javascript" type="text/javascript"&gt;bt_code_init('f5a2eff1-dcf9-4dee-a929-48e07a294b6a');&lt;/script&gt;&lt;br /&gt;Let's start from the beginning. The &lt;span style="font-style: italic;"&gt;regAddress &lt;/span&gt;variable represents the register we want to start reading from (registers 0x32 - 0x37 are acceleration data registers, page 18 in the datasheet). We're defining the ints (x,y,z) which we'll use later, to send the data through serial port to Processing.&lt;br /&gt;Next we're performing a readFrom function call, we pass our device's address, the register address to start reading from , the number of bytes to read and the buffer in which to save the data.&lt;br /&gt;Now comes the tricky part - convert the data received from 2 bytes in to one int. Each axis' pair of bytes are manipulated, in order to create an int from two bytes using C bitwise operators (see &lt;a href="http://en.wikipedia.org/wiki/Bitwise_operation" target="_blank"&gt;here&lt;/a&gt; and &lt;a href="http://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/bitwise.html" target="_blank"&gt;here&lt;/a&gt; for more info on bitwise operators in C).&lt;br /&gt;Afterward we're using the &lt;a href="http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/"&gt;sprintf()&lt;/a&gt; function to turn the integers we have in to one string. And then we send that string to the serial port, terminating it with end-of-line character. The delay in the end is not strictly necessary, but it gave me better performance that way.&lt;br /&gt;&lt;br /&gt;That's it! Below is a link to the sources - it also contains a matching Processing application to read the data and manipulate a model on the screen accordingly.&lt;br /&gt;&lt;br /&gt;The next step is writing an actual 3D game for this thing, I'll probably do it using Microsoft's XNA studio, which appears to be pretty awesome. Stay tuned. Questions and comments are of course welcomed.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://cookietroll.com/blogfiles/adxl/adxl345_arduino_processing.zip"&gt;Source code files&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-253633976698428288?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/253633976698428288/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html#comment-form' title='134 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/253633976698428288'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/253633976698428288'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html' title='ADXL345 accelerometer breakout board + Arduino and Processing'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>134</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-4087766436849170110</id><published>2009-11-24T12:12:00.019+02:00</published><updated>2010-11-30T22:11:03.359+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='art'/><category scheme='http://www.blogger.com/atom/ns#' term='pixlr.com'/><category scheme='http://www.blogger.com/atom/ns#' term='image processing'/><category scheme='http://www.blogger.com/atom/ns#' term='graphics'/><category scheme='http://www.blogger.com/atom/ns#' term='programming language'/><category scheme='http://www.blogger.com/atom/ns#' term='web based'/><category scheme='http://www.blogger.com/atom/ns#' term='processing'/><title type='text'>Programmer and Artist? (Processing and pixlr.com discovery)</title><content type='html'>Very long time passed since my last post. Since then I have discovered more than a few interesting things but some of them will have to wait as I'm not yet proficient enough to elaborate on those. I'm going to devote this post to something that is close to my heart as an ex-animator and as a programmer - computer generated art (and relevant tools)!&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:130%;" &gt;&lt;span style="font-family:arial;"&gt;Part 1&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;First of all we'll start with a relatively young (2001 born) programming language - &lt;span style="font-weight: bold;"&gt;Processing&lt;/span&gt;. As stated on the &lt;a href="http://www.processing.org/" target="_blank"&gt;processing website&lt;/a&gt;: &lt;span style="font-style: italic;"&gt;Processing is an open source programming language and environment for people who want to program images, animation, and interactions.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The language is designed to be simple, so that artists and hobbyists can use it also. Processing is Java based, which leads to all kinds of cool effects:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;If you're not new to that kind of syntax (C++/Java) you're good to write pretty complex code almost instantly&lt;/li&gt;&lt;li&gt;It's object oriented&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;You can export your sketches (that's how applications are called in Processing) directly to Java applets or applications!&lt;/li&gt;&lt;li&gt;It's system independent&lt;/li&gt;&lt;/ul&gt;I've been messing with it for a few weeks now and I must tell you, it's lots of fun! It has a nice little IDE, which is not too feature-rich (to say the least) but is very light and fast to grasp.&lt;br /&gt;&lt;br /&gt;Here's an &lt;a href="http://www.cookietroll.com/processing/click_the_ball/" target="_blank"&gt;example&lt;/a&gt; of a game I made in a bit more than an hour - including the exporting and uploading to the web.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:130%;" &gt;&lt;span style="font-family:arial;"&gt;Part 2&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Now to my other (not less exciting) finding!&lt;br /&gt;Recently I've read some blogposts about the controversial ChromeOS. One dude was claiming that we need raw horse power on our puters because Picasa's editing tools are fun, but they're not Adobe Photoshop. Regardless of my opinion of this (I think he's actually right about the whole ChromeOS thingie), another guy made a great point by simply saying &lt;a href="http://www.pixlr.com/" target="_blank"&gt;pixlr.com&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;pixlr&lt;/span&gt; is an online image processing tool, and damn, it's a good one! Here's a short list of my pros:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;It's free&lt;/li&gt;&lt;li&gt;It's web based! Simply open your browser and it runs&lt;br /&gt;&lt;/li&gt;&lt;li&gt;It has most of the tools I need in photoshop to do my occasional image editing&lt;/li&gt;&lt;li&gt;It's shortcuts and menus are almost PS identical&lt;/li&gt;&lt;li&gt;It loads FAST&lt;/li&gt;&lt;li&gt;Last but not least, you have an API - I havn't gone in to it too deep, but it looks like cool stuff could be done with it!&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;I've found this app to be extremely useful in the 2-3 days that I actually know it. Give it a run ;)&lt;br /&gt;&lt;br /&gt;Have fun, and let me know if you have any questions!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-4087766436849170110?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/4087766436849170110/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/11/programmer-and-artist-processing-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/4087766436849170110'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/4087766436849170110'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/11/programmer-and-artist-processing-and.html' title='Programmer and Artist? (Processing and pixlr.com discovery)'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-5534124347171708985</id><published>2009-07-23T15:14:00.058+03:00</published><updated>2011-12-17T12:58:10.393+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='server'/><category scheme='http://www.blogger.com/atom/ns#' term='cli'/><category scheme='http://www.blogger.com/atom/ns#' term='socket'/><category scheme='http://www.blogger.com/atom/ns#' term='select()'/><category scheme='http://www.blogger.com/atom/ns#' term='command line'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='terminal'/><category scheme='http://www.blogger.com/atom/ns#' term='stream_select()'/><category scheme='http://www.blogger.com/atom/ns#' term='chat'/><category scheme='http://www.blogger.com/atom/ns#' term='socket_select()'/><category scheme='http://www.blogger.com/atom/ns#' term='client'/><title type='text'>Simple PHP socket-based terminal chat</title><content type='html'>Some time ago I decided it would be interesting to write an extremely simple PHP terminal chat to try out PHP's sockets and the &lt;span style="font-style: italic;"&gt;socket_select()&lt;/span&gt; function usage. If you're not in to reading, feel free to skip to the samples and/or the sources ;)&lt;br /&gt;&lt;br /&gt;What is a &lt;span style="font-style: italic;"&gt;socket_select()&lt;/span&gt; (or &lt;span style="font-style: italic;"&gt;*_select()&lt;/span&gt; for that matter) you ask? &lt;a name='more'&gt;&lt;/a&gt;In general a &lt;span style="font-style: italic;"&gt;select()&lt;/span&gt; function takes a list of resources (files, sockets, etc) and waits for at least one of them to be available for reading (or writing), and when that happens it lets you know which resources from the lists you've passed are ready for action. This is extremely helpful when dealing with sockets, because they - unlike local files for example - aren't as ready to be read or written to because of net lag and such. Why am I telling you this? Because this function is crucial for writing a responsive real time chat in PHP.&lt;br /&gt;&lt;br /&gt;Because of the fact that my first serious encounter with &lt;span style="font-style: italic;"&gt;select()&lt;/span&gt; was in Communication Applications course - we coded in C - I naively believed that the &lt;span style="font-style: italic;"&gt;socket_select() &lt;/span&gt;function in PHP will be glad to take file descriptors and sockets mixed just like the C &lt;span style="font-style: italic;"&gt;select()&lt;/span&gt; function does. I was very wrong.&lt;br /&gt;&lt;br /&gt;PHP actually has different (!!) selects for files and for sockets. A question arose in my head: &lt;span style="font-weight: bold;"&gt;WTF!?&lt;/span&gt; Doesn't that kinda beat the point of select if I can not actually monitor two different resource types?? Well, after some foruming and reading I've found out that you can spawn sockets in a file stream form, using the &lt;span style="font-style: italic;"&gt;stream_socket_server()&lt;/span&gt; and the &lt;span style="font-style: italic;"&gt;stream_socket_client()&lt;/span&gt; functions. Great! Now that we know some techy details, we can look at some code:&lt;br /&gt;&lt;br /&gt;Server side setup&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;div id='iframeDivbf2f4e30-6f9b-463f-b4d2-fac3d9c6a8e9' style='display:none;margin:0px;'&gt;&lt;iframe id='iframebf2f4e30-6f9b-463f-b4d2-fac3d9c6a8e9' src='http://www.blogtrog.com/code.aspx?id=bf2f4e30-6f9b-463f-b4d2-fac3d9c6a8e9' style='width: 560px; height: 280px; border: 1px solid #e0e0e0;margin:0px;' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id='noIframeDivbf2f4e30-6f9b-463f-b4d2-fac3d9c6a8e9' style='display:block;margin:0px;'&gt;&lt;pre&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #800080;"&gt;$PORT&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;20222&lt;/span&gt;&lt;span style="color: #000000;"&gt;;            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;chat port&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$ADDRESS&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;localhost&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;adress&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$ssock&lt;/span&gt;&lt;span style="color: #000000;"&gt;;        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;server socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$csock&lt;/span&gt;&lt;span style="color: #000000;"&gt;;        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;chat socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$uin&lt;/span&gt;&lt;span style="color: #000000;"&gt;;        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;user input file descriptor&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$ssock&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;stream_socket_server&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;tcp://&lt;/span&gt;&lt;span style="color: #800080;"&gt;$ADDRESS&lt;/span&gt;&lt;span style="color: #000000;"&gt;:&lt;/span&gt;&lt;span style="color: #800080;"&gt;$PORT&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;creating the server sock&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;echo&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Waiting for client...\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$csock&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;stream_socket_accept&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$ssock&lt;/span&gt;&lt;span style="color: #000000;"&gt;);            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;waiting for the client to connect&lt;br /&gt;                                        //$csock will be used as the chat socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;echo&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Connection established\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$uin&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;fopen&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;php://stdin&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;r&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;opening a standart input file stream&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language='javascript' src='http://www.blogtrog.com/scripts/bt_code.js' type='text/javascript'&gt;&lt;/script&gt;&lt;script language='javascript' type='text/javascript'&gt;bt_code_init('bf2f4e30-6f9b-463f-b4d2-fac3d9c6a8e9');&lt;/script&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;What did we do?&lt;br /&gt;In the first two lines we define the address and the port for the socket. Then we use the &lt;span style="font-style: italic;"&gt;stream_socket_server()&lt;/span&gt; function to create a TCP server socket in &lt;span style="font-weight: bold;"&gt;stream&lt;/span&gt; form, so it can be used later by &lt;span style="font-style: italic;"&gt;stream_socket_accept()&lt;/span&gt;. The &lt;span style="color: rgb(200, 0, 150);"&gt;$ssock &lt;/span&gt;stream is used only for &lt;span style="font-weight: bold;"&gt;waiting&lt;/span&gt; for an incoming call from a client, it's not actually used for the communication. The &lt;span style="color: rgb(200, 0, 150);"&gt;$csock&lt;/span&gt; is the actual chat socket we will use, it will be open as soon as a client initiates communication with the server. As soon as that happens we give the user a notification that we're connected and put the &lt;span style="font-style: italic;"&gt;standart-input&lt;/span&gt; stream in to &lt;span style="color: rgb(200, 0, 150);"&gt;$uin&lt;/span&gt; so that we can take user input directly from command line.&lt;br /&gt;&lt;br /&gt;Now for the code meat. The communication/select loop!&lt;br /&gt;&lt;br /&gt;Server communication&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;div id='iframeDivf33531bb-596e-4f48-9c1c-69b85579c29f' style='display:none;margin:0px;'&gt;&lt;iframe id='iframef33531bb-596e-4f48-9c1c-69b85579c29f' src='http://www.blogtrog.com/code.aspx?id=f33531bb-596e-4f48-9c1c-69b85579c29f' style='width: 560px; height: 480px; border: 1px solid #e0e0e0;margin:0px;' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id='noIframeDivf33531bb-596e-4f48-9c1c-69b85579c29f' style='display:block;margin:0px;'&gt;&lt;pre&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #800080;"&gt;$conOpen&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;we run the read loop until other side closes connection&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;while&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$conOpen&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the read loop&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$r&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;array&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$csock&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$uin&lt;/span&gt;&lt;span style="color: #000000;"&gt;);        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;file streams to select from&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$w&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;NULL&lt;/span&gt;&lt;span style="color: #000000;"&gt;;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;no streams to write to&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$e&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;NULL&lt;/span&gt;&lt;span style="color: #000000;"&gt;;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;no special stuff handling&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$t&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;NULL&lt;/span&gt;&lt;span style="color: #000000;"&gt;;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;no timeout for waiting&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;stream_select&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$r&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$w&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$e&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$t&lt;/span&gt;&lt;span style="color: #000000;"&gt;)) {    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;if select didn't throw an error&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$r&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;as&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$i&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$fd&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;checking every socket in list to see who's ready&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$fd&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$uin&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the stdin is ready for reading&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;                &lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;fgets&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$uin&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: #008080;"&gt;fwrite&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$csock&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;            }&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;else&lt;/span&gt;&lt;span style="color: #000000;"&gt; {                    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the socket is ready for reading&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;                &lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;fgets&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$csock&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;a 0 length string is read -&amp;gt; connection closed&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;echo&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Connection closed by peer\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$conOpen&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: #008080;"&gt;fclose&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$csock&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;break&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;                }&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;echo&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;[Client says] &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;.&lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language='javascript' src='http://www.blogtrog.com/scripts/bt_code.js' type='text/javascript'&gt;&lt;/script&gt;&lt;script language='javascript' type='text/javascript'&gt;bt_code_init('f33531bb-596e-4f48-9c1c-69b85579c29f');&lt;/script&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;What are we doing? The while loop will run umm.. while connection with the other side is open (if we got this far in the code it is already open).&lt;br /&gt;Now what are the &lt;span style="color: rgb(200, 0, 150);"&gt;$r&lt;/span&gt;, &lt;span style="color: rgb(200, 0, 150);"&gt;$w&lt;/span&gt;, &lt;span style="color: rgb(200, 0, 150);"&gt;$e&lt;/span&gt; and &lt;span style="color: rgb(200, 0, 150);"&gt;$t&lt;/span&gt; variables? Understanding this part is 80% of understanding &lt;span style="font-style: italic;"&gt;select()&lt;/span&gt;.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The &lt;span style="color: rgb(200, 0, 150);"&gt;$r&lt;/span&gt; is an array of streams (sockets, files, etc) we want to read from. As soon as one of them becomes available for reading, &lt;span style="font-style: italic;"&gt;select()&lt;/span&gt; returns.&lt;/li&gt;&lt;li&gt;The &lt;span style="color: rgb(200, 0, 150);"&gt;$w&lt;/span&gt; is the same as the &lt;span style="color: rgb(200, 0, 150);"&gt;$r&lt;/span&gt; only it will wait for streams to become available for writing instead of reading.&lt;/li&gt;&lt;li&gt;The &lt;span style="color: rgb(200, 0, 150);"&gt;$e&lt;/span&gt; is for high-priority in-bound communication (not interesting atm)&lt;/li&gt;&lt;li&gt;The &lt;span style="color: rgb(200, 0, 150);"&gt;$t&lt;/span&gt; variable tells the &lt;span style="font-style: italic;"&gt;stream_select()&lt;/span&gt; function for &lt;span style="font-weight: bold;"&gt;how long&lt;/span&gt; should it wait for a change of status of any of the streams passed to it in the various arrays. We don't want a timeout (ie we don't mind waiting forever), that's why we set it to NULL. Btw, setting $t to 0 will only check once and return immediately.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;A most important thing to know about the &lt;span style="font-style: italic;"&gt;select() &lt;/span&gt;family is that &lt;span style="font-weight: bold;"&gt;it changes the arrays you pass to it&lt;/span&gt;! And leaves in the array &lt;span style="font-weight: bold;"&gt;only&lt;/span&gt; the streams that has actually changed status. What does it mean? Well, two things in general:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Save your streams in other place, so you don't lose them after passing them to &lt;span style="font-style: italic;"&gt;select()&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;Re-initialize your &lt;span style="color: rgb(200, 0, 150);"&gt;$r&lt;/span&gt;, &lt;span style="color: rgb(200, 0, 150);"&gt;$w&lt;/span&gt;, and &lt;span style="color: rgb(200, 0, 150);"&gt;$e&lt;/span&gt; arrays every time you call &lt;span style="font-style: italic;"&gt;select()&lt;/span&gt;.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;Back to our issue, when the &lt;span style="font-style: italic;"&gt;stream_select()&lt;/span&gt; function returns with values greater than 0, we know that we have a stream we can read from! And that's exactly what we are doing in the &lt;span style="color: rgb(90, 90, 250);"&gt;foreach&lt;/span&gt; loop, we iterate over the changed array of streams and read from each one of the available streams. If it's the socket stream, we read the data and put it on the screen, if it's the user input, we send it to the socket.&lt;br /&gt;Another thing to note: when connection on the other side closes, the socket appears to be readable, but, we can only read 0 bytes (empty string). This is how we know the connection was closed.&lt;br /&gt;&lt;br /&gt;Now let's take a look at the client setup&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;div id='iframeDiv89801d75-84e1-4191-b5a6-a2162037503b' style='display:none;margin:0px;'&gt;&lt;iframe id='iframe89801d75-84e1-4191-b5a6-a2162037503b' src='http://www.blogtrog.com/code.aspx?id=89801d75-84e1-4191-b5a6-a2162037503b' style='width: 560px; height: 180px; border: 1px solid #e0e0e0;margin:0px;' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id='noIframeDiv89801d75-84e1-4191-b5a6-a2162037503b' style='display:block;margin:0px;'&gt;&lt;pre&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #800080;"&gt;$PORT&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;20222&lt;/span&gt;&lt;span style="color: #000000;"&gt;;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;chat port&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$ADDRESS&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;localhost&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;adress&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$sock&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;stream_socket_client&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;tcp://&lt;/span&gt;&lt;span style="color: #800080;"&gt;$ADDRESS&lt;/span&gt;&lt;span style="color: #000000;"&gt;:&lt;/span&gt;&lt;span style="color: #800080;"&gt;$PORT&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;); &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;creating the client socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;echo&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;connection established\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$uin&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;fopen&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;php://stdin&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;r&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;); &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;opening a standart input file stream&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language='javascript' src='http://www.blogtrog.com/scripts/bt_code.js' type='text/javascript'&gt;&lt;/script&gt;&lt;script language='javascript' type='text/javascript'&gt;bt_code_init('89801d75-84e1-4191-b5a6-a2162037503b');&lt;/script&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;Looks pretty much like the server setup, except for the fact that we don't need two sockets. We use the same socket for connection and communication. The communication loop of the client is almost identical to that of the server so I'm not going to cover it. You can get both sources at the bottom of the post.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-size:130%;"&gt;&lt;span class="fullpost"&gt;Running the chat&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Open two terminals, one for server and one for client.&lt;br /&gt;First run server.php (I'm on Linux):&lt;br /&gt;&lt;br /&gt;$&gt; php server.php&lt;br /&gt;&lt;br /&gt;The server now waits for an incoming connection&lt;br /&gt;&lt;br /&gt;then go to your other terminal and run client.php:&lt;br /&gt;&lt;br /&gt;$&gt; php client.php&lt;br /&gt;&lt;br /&gt;Both programs should notify you that connection has been established, now type something in any of the terminals to test communication :)&lt;br /&gt;&lt;br /&gt;Server:&lt;br /&gt;&lt;img src="http://www.cookietroll.com/blogfiles/scr_server.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;Client:&lt;br /&gt;&lt;img src="http://www.cookietroll.com/blogfiles/scr_client.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can find the &lt;span style="font-weight: bold;"&gt;sources &lt;/span&gt;&lt;a style="font-weight: bold;" href="http://www.cookietroll.com/blogfiles/simple_php_chat.zip"&gt;here&lt;/a&gt;. Questions, suggestions, criticism and love letters are welcome!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-5534124347171708985?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/5534124347171708985/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/07/simple-php-socket-based-terminal-chat.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/5534124347171708985'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/5534124347171708985'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/07/simple-php-socket-based-terminal-chat.html' title='Simple PHP socket-based terminal chat'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-1988973509608816247</id><published>2009-05-15T10:39:00.017+03:00</published><updated>2009-12-01T12:01:48.681+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ide'/><category scheme='http://www.blogger.com/atom/ns#' term='homework'/><category scheme='http://www.blogger.com/atom/ns#' term='copying'/><category scheme='http://www.blogger.com/atom/ns#' term='cs homework'/><category scheme='http://www.blogger.com/atom/ns#' term='regex'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Copying programming homework</title><content type='html'>Before I start telling you the story which led me to write on this topic, I feel I'm obligated to reveal the main idea of this post:&lt;br /&gt;In order to copy programming homework, &lt;span style="font-weight: bold;"&gt;you mustn't be a dumb fuck&lt;/span&gt;. (And a few ways to avoid being one)&lt;br /&gt;&lt;br /&gt;And now the story. I have a colleague (let's call him John) at work who also checks programming courses homework at my college. Our college is really harsh about students copying programming homework (you can xerox algorithms homework, but programming is a big no-no).&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;One day John gets an email from one of the students claiming that he has submitted the last exercise but in the grades sheet it was marked "Wasn't submitted", so he demanded from John to check the issue. John checked his college email inbox to see if he has overlooked the poor guy's exercise. Surprise! The guy indeed sent him a programming exercise that week. Why wasn't it graded then? Well, when he opened the exercise to check it (as he was sure he overlooked it the first time) he saw that the name and the ID number at the top of the source code files were those of another student, not the one who submitted them and demanded a recheck. The moron copy/pasted the exercise from another student and didn't even change the name on it.&lt;br /&gt;&lt;br /&gt;This is a true, although a bit extreme, example of a dumb fuck copying programming homework. If you want to avoid being caught in such situations, keep reading.&lt;br /&gt;&lt;br /&gt;Two inputs to the beginning homework  copier:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;It's very hard to make copied code completely unrecognizable.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;It is almost always easier (and more productive, and more fun) to write the program from scratch than trying to rewrite it for the pure purpose of making it unrecognizable.&lt;/li&gt;&lt;/ul&gt;If you don't mind the inputs, and you still want to copy someone else's code, you should at least put some minimal effort in camouflaging it, as you don't want to offend the intelligence of your exercise checker. You want to show him that you at least tried :D&lt;br /&gt;&lt;br /&gt;Camouflaging equipment and techniques:&lt;br /&gt;&lt;br /&gt;Equipment:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Get yourself a descent IDE (I recommend &lt;a href="http://www.netbeans.org/" target="_blank"&gt;NetBeans&lt;/a&gt;), as it enables advanced Find/Replace (&lt;a href="http://en.wikipedia.org/wiki/Regular_expression" target="_blank"&gt;regular expressions&lt;/a&gt;, refactoring, etc)&lt;/li&gt;&lt;li&gt;Or at least a descent text editor (&lt;a href="http://notepad-plus.sourceforge.net/uk/site.htm" target="_blank"&gt;Notepad++&lt;/a&gt; for windows. Ubuntu comes with Gedit)&lt;/li&gt;&lt;/ol&gt;Techniques:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Change the name&lt;/span&gt;, the ID number and any other personal information that shows unambiguously that you are not the real owner of this code. Otherwise you're a dumb fuck and the rest of the techniques are not going to help you.&lt;span style="color: rgb(0, 0, 153);font-family:Arial;font-size:11;"  &gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;The basic and most straightforward thing to do is &lt;span style="font-weight: bold;"&gt;rename your classes, functions and some vars&lt;/span&gt;. In the text editor you simply do Find/Replace (usually in the Edit menu). If you're not using an IDE you're gonna have a headache with Java classes as you need to rename all the files which contain those classes. With an IDE you can simply select a name of a class (or a function for that matter) right click &gt; Refactor &gt; Rename. And tada! You have your class/function/variable renamed on &lt;span style="font-weight: bold;"&gt;every&lt;/span&gt; reference to it, no matter where in the project.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Change indentation, change comments&lt;/span&gt;. In an IDE or an advanced text editor (which supports regular expression or at least Find/Replace) It's really easy to do both. Remove all comments: Find regex "&lt;span style="font-style: italic;"&gt;//[.]+\n&lt;/span&gt;" and replace with "&lt;span style="font-style: italic;"&gt;\n&lt;/span&gt;". After all comments removed add yours in random order (remeber to remember what things do in general). Change indentation: Find regex: "&lt;span style="font-style: italic;"&gt;\n[\s]+{&lt;/span&gt;" and replace with "&lt;span style="font-style: italic;"&gt;{&lt;/span&gt;", or vice versa.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Change for to while and while to for&lt;/span&gt;. Anything you can do with a for loop you can do with a while loop, playing a bit with those might make your code look a bit different. &lt;a href="http://www.cookietroll.com/blogfiles/for2while.html" target="_blank"&gt;While to For translation example&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Change temp variables to direct function call&lt;/span&gt; and vice versa. If you see too many redundant temp vars you can cut their count by by putting the calculation they hold directly to where you'd put the variable later. Sounds blurry I know, &lt;a href="http://www.cookietroll.com/blogfiles/tempvsnotemp.html" target="_blank"&gt;here's a code example&lt;/a&gt;.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Use functions instead of in-main&lt;/span&gt; code and vice versa. Take pieces of code that perform some action and put them in a function (that involves some work compared to previous techniques). Or take some function and replace it's every occurrence with it's code (also takes some work to make it work). Make sure it's not an exercise dealing with functions before you remove them - otherwise you're a dumb ass!&lt;/li&gt;&lt;/ol&gt;These were just a few tips on how to make your code keep it's initial functionality, but resemble less to it's original. Please note that the tips provided &lt;span style="font-weight: bold;"&gt;arn't for code efficiency&lt;/span&gt;, but solely for the purpose of making it less recognizable!&lt;br /&gt;&lt;br /&gt;As always questions, comments and slander are most welcome!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-1988973509608816247?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/1988973509608816247/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/05/copying-programming-homework.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/1988973509608816247'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/1988973509608816247'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/05/copying-programming-homework.html' title='Copying programming homework'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-6013869115993314395</id><published>2009-04-28T16:16:00.020+03:00</published><updated>2011-12-17T12:29:06.207+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='server'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='socket'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='client'/><title type='text'>Opening a PHP to Java socket</title><content type='html'>In a project I've been working on recently we needed to integrate a functionality we have in a few Java classes we developed in to a PHP script. If those specific classes could be wrapped in a simple command-line app, it would not have been a problem, as you can &lt;a href="http://codeyoung.blogspot.com/2009/02/accessing-command-line-from-php-code.html"&gt;execute command-line apps from PHP&lt;/a&gt; easily. But! These specific Java classes took very long time to load (around two seconds!) before they could be used. &lt;a name='more'&gt;&lt;/a&gt;So command-line app was out of question, because two seconds delays for every command-line execution are unacceptable when dealing with thousands of calls a day, so the solution was to load the app once and to use the &lt;span style="font-weight: bold;"&gt;already running application&lt;/span&gt; when needed.&lt;br /&gt;&lt;br /&gt;How do you use a running Java application from PHP code? A quick (and dirty?) solution is sockets. In this case the Java app is the server and PHP is the client. Ok, lets take a look at some code (Links to the sources are at the bottom of the page).&lt;br /&gt;&lt;br /&gt;The Java server class looks like this:&lt;br /&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;div id="iframeDiv7d4b4104-abb5-4f6d-a775-603035ead84a" style="display:none;margin:0px;"&gt;&lt;iframe id="iframe7d4b4104-abb5-4f6d-a775-603035ead84a" src="http://www.blogtrog.com/code.aspx?id=7d4b4104-abb5-4f6d-a775-603035ead84a" style="width: 560px; height: 560px; border: 1px solid #e0e0e0;margin:0px;" frameborder="0"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id="noIframeDiv7d4b4104-abb5-4f6d-a775-603035ead84a" style="display:block;margin:0px;"&gt;&lt;pre&gt;&lt;div&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; MyJavaServer {&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; main(String[] args) {&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; port &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;20222&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       ServerSocket listenSock &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;the listening server socket&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;        Socket sock &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;             &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;the socket that will actually be used for communication&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;try&lt;/span&gt;&lt;span style="color:#000000;"&gt; {&lt;br /&gt;&lt;br /&gt;           listenSock &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; ServerSocket(port);&lt;br /&gt;&lt;br /&gt;           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;while&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;we want the server to run till the end of times&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;               sock &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; listenSock.accept();             &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;will block until connection recieved&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;               BufferedReader br &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; BufferedReader(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; InputStreamReader(sock.getInputStream()));&lt;br /&gt;               BufferedWriter bw &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; BufferedWriter(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; OutputStreamWriter(sock.getOutputStream()));&lt;br /&gt;               String line &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;""&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;while&lt;/span&gt;&lt;span style="color:#000000;"&gt; ((line &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; br.readLine()) &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;br /&gt;                   bw.write(&lt;/span&gt;&lt;span style="color:#000000;"&gt;"&lt;/span&gt;&lt;span style="color:#000000;"&gt;PHP said: &lt;/span&gt;&lt;span style="color:#000000;"&gt;"&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; line &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;"&lt;/span&gt;&lt;span style="color:#000000;"&gt;\n&lt;/span&gt;&lt;span style="color:#000000;"&gt;"&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;br /&gt;                   bw.flush();&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;Closing streams and the current socket (not the listening socket!)&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;                bw.close();&lt;br /&gt;               br.close();&lt;br /&gt;               sock.close();&lt;br /&gt;           }&lt;br /&gt;       } &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;catch&lt;/span&gt;&lt;span style="color:#000000;"&gt; (IOException ex) {&lt;br /&gt;           ex.printStackTrace();&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"&gt;&lt;/script&gt;&lt;script language="javascript" type="text/javascript"&gt;bt_code_init('7d4b4104-abb5-4f6d-a775-603035ead84a');&lt;/script&gt;&lt;br /&gt;&lt;hr /&gt;What it does:&lt;br /&gt;Opens a listening (server) socket which waits for incoming connections on port 20222 in an endless loop. As soon as a connection is received the socket's input and output streams are "turned" in to buffered reader and writer (so they can be manipulated in an easier and more effective way).&lt;br /&gt;Afterward we read &lt;span style="font-weight: bold;"&gt;line by line&lt;/span&gt; the incoming message, add a &lt;span style="font-style: italic;"&gt;"PHP said: " &lt;/span&gt;prefix to each line we received and then send it back to the client. After the incoming message is over we close the buffered reader, writer and the socket, and wait for another incoming connection.&lt;br /&gt;&lt;br /&gt;Note:  It's important that you add a end-of-line char ("\n") to the end of the reply when you are writing to the socket, as the PHP client finishes reading when it encounters the end-of-line char.&lt;br /&gt;&lt;br /&gt;The PHP client looks like this:&lt;br /&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;div id='iframeDiva7d5e594-d1f6-4eb8-a06d-45d7cecf36fe' style='display:none;margin:0px;'&gt;&lt;iframe id='iframea7d5e594-d1f6-4eb8-a06d-45d7cecf36fe' src='http://www.blogtrog.com/code.aspx?id=a7d5e594-d1f6-4eb8-a06d-45d7cecf36fe' style='width: 560px; height: 350px; border: 1px solid #e0e0e0;margin:0px;' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id='noIframeDiva7d5e594-d1f6-4eb8-a06d-45d7cecf36fe' style='display:block;margin:0px;'&gt;&lt;pre&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #800080;"&gt;$PORT&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;20222&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the port on which we are connecting to the &amp;quot;remote&amp;quot; machine&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$HOST&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;localhost&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the ip of the remote machine (in this case it's the same machine)&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$sock&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; socket_create(AF_INET&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; SOCK_STREAM&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Creating a TCP socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;        or &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;die&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;error: could not create socket\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$succ&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; socket_connect(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$sock&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$HOST&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$PORT&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Connecting to to server using that socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;        or &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;die&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;error: could not connect to host\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Hello, Java!&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the text we want to send to the server&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;socket_write(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$sock&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;strlen&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$text&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Writing the text to the socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;        or &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;die&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;error: failed to write to socket\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$reply&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; socket_read(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$sock&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;10000&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; PHP_NORMAL_READ) &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Reading the reply from socket&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;        or &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;die&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;error: failed to read from socket\n&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;echo&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$reply&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language='javascript' src='http://www.blogtrog.com/scripts/bt_code.js' type='text/javascript'&gt;&lt;/script&gt;&lt;script language='javascript' type='text/javascript'&gt;bt_code_init('a7d5e594-d1f6-4eb8-a06d-45d7cecf36fe');&lt;/script&gt;&lt;br /&gt;&lt;hr /&gt;What it does:&lt;br /&gt;We define a port (20222 - the port that MyJavaServer listens on) and a host we want to connect to (localhost is our own machine, change to a specific IP to connect remote machines). Create a socket descriptor ($sock), and connect the socket to the "remote" host (the $succ variable is used to check if the connection was successful). Afterward we write the contents $text to the socket, and then read the server's reply. The &lt;span style="font-weight: bold;"&gt;PHP_NORMAL_READ&lt;/span&gt; flag tells the socket_read() function to read until the first end-of-line char.&lt;br /&gt;&lt;br /&gt;Notes:&lt;br /&gt;* Make sure you add an end-of-line ("\n") to the string you send to the server, as the server reads until it encounters the end-of-line char.&lt;br /&gt;* Don't forget to add 1 to the length of the string you are sending so that the added end of line will actually be sent.&lt;br /&gt;&lt;br /&gt;Conclusion:&lt;br /&gt;There's nothing new in this code neither  for "Javers" nor PHPers. The novelty here (for me anywayz) is the interaction between the codes, which on the other makes perfect sense because of the fact that sockets on both utilize the same protocol - in this case &lt;a href="http://en.wikipedia.org/wiki/Internet_Protocol_Suite"&gt;TCP/IP&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;If you still have questions please ask! If you have comments, well, comment :D&lt;br /&gt;&lt;br /&gt;Sources: &lt;a href="http://www.cookietroll.com/blogfiles/php_java_sock.zip"&gt;php_java_sock.zip&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-6013869115993314395?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/6013869115993314395/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/04/opening-php-to-java-socket.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/6013869115993314395'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/6013869115993314395'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/04/opening-php-to-java-socket.html' title='Opening a PHP to Java socket'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-1689475670514322723</id><published>2009-02-07T11:26:00.013+02:00</published><updated>2011-11-17T09:21:12.240+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='unzip'/><category scheme='http://www.blogger.com/atom/ns#' term='cli'/><category scheme='http://www.blogger.com/atom/ns#' term='command line'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='zip'/><title type='text'>Accessing command line from PHP code</title><content type='html'>It is sometimes necessary (or easier) to execute command line commands from within PHP code, in this post I will show an example of such usage. If you only need an über-basic example, here it is:&lt;br /&gt;&lt;br /&gt;&lt;div id='iframeDiv9237fd9e-5413-4173-ba87-15ed27f43120' style='display:none;margin:0px;'&gt;&lt;iframe id='iframe9237fd9e-5413-4173-ba87-15ed27f43120' src='http://www.blogtrog.com/code.aspx?id=9237fd9e-5413-4173-ba87-15ed27f43120' style='width: 520px; height: 130px; border: 1px solid #e0e0e0;margin:0px;' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id='noIframeDiv9237fd9e-5413-4173-ba87-15ed27f43120' style='display:block;margin:0px;'&gt;&lt;pre&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #000000;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #000000;"&gt;php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$thisf&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$_SERVER&lt;/span&gt;&lt;span style="color: #000000;"&gt;[&lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;SCRIPT_NAME&lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;];&lt;br /&gt;`cp &lt;/span&gt;&lt;span style="color: #800080;"&gt;$thisf&lt;/span&gt;&lt;span style="color: #000000;"&gt; newname&lt;/span&gt;&lt;span style="color: #000000;"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;php`;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;?&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language='javascript' src='http://www.blogtrog.com/scripts/bt_code.js' type='text/javascript'&gt;&lt;/script&gt;&lt;script language='javascript' type='text/javascript'&gt;bt_code_init('9237fd9e-5413-4173-ba87-15ed27f43120');&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;This little piece of code will copy the running script to &lt;span style="font-style: italic;"&gt;newfile.php&lt;/span&gt;. If you're not satisfied yet, keep reading.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;The scenario: You have a folder with lots of .zips, and you want to unzip each one in to a relevant folder according to it's name's first letter. For example if the .zip's name is &lt;span style="font-style: italic;"&gt;somefile.zip&lt;/span&gt; we'll unzip it to dir &lt;span style="font-style: italic; font-weight: bold;"&gt;s&lt;/span&gt;, if it's name is &lt;span style="font-style: italic;"&gt;anotherfile.zip&lt;/span&gt; we'll unzip it to dir &lt;span style="font-style: italic; font-weight: bold;"&gt;a&lt;/span&gt;. To keep the code short we assume that all needed directories already exist and that there are only .zips in the &lt;span style="font-style: italic;"&gt;src&lt;/span&gt; dir.&lt;br /&gt;&lt;br /&gt;Before we dive in to PHP lets go over the Linux command line unzip. To unzip &lt;span style="font-style: italic;"&gt;file.zip&lt;/span&gt; residing in dir &lt;span style="font-style: italic;"&gt;src &lt;/span&gt;to dir &lt;span style="font-style: italic;"&gt;trg&lt;/span&gt; you use the following command:&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt; $ unzip -d trg src/file.zip&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Finally, ladies and gentlemen, the code:&lt;br /&gt;&lt;div id='iframeDiv7ed4c602-d8b8-44bb-a748-9dc02ac17175' style='display:none;margin:0px;'&gt;&lt;iframe id='iframe7ed4c602-d8b8-44bb-a748-9dc02ac17175' src='http://www.blogtrog.com/code.aspx?id=7ed4c602-d8b8-44bb-a748-9dc02ac17175' style='width: 520px; height: 310px; border: 1px solid #e0e0e0;margin:0px;' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;div id='noIframeDiv7ed4c602-d8b8-44bb-a748-9dc02ac17175' style='display:block;margin:0px;'&gt;&lt;pre&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #000000;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #000000;"&gt;php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;directory where the zips reside&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$srcdir&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;/home/user/zips/&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the output master directory inside we have all the letters&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$trgdir&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;/home/user/out/&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$dirh&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;opendir&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$srcdir&lt;/span&gt;&lt;span style="color: #000000;"&gt;);            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;the dir handler of the zips&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;while&lt;/span&gt;&lt;span style="color: #000000;"&gt;((&lt;/span&gt;&lt;span style="color: #800080;"&gt;$zip&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #008080;"&gt;readdir&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$dirh&lt;/span&gt;&lt;span style="color: #000000;"&gt;)) &lt;/span&gt;&lt;span style="color: #000000;"&gt;!==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;FALSE&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;reading next file in dir&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$zip&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;||&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$zip&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;..&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;continue&lt;/span&gt;&lt;span style="color: #000000;"&gt;;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;checking that those are actual files&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$firstL&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$zip&lt;/span&gt;&lt;span style="color: #000000;"&gt;[&lt;/span&gt;&lt;span style="color: #000000;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;];                            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;getting the first letter of the file&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    `unzip &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt;d &lt;/span&gt;&lt;span style="color: #800080;"&gt;$trgdir$firstL&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$srcdir$zip&lt;/span&gt;&lt;span style="color: #000000;"&gt;`;        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;running the CLI command&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;?&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script language='javascript' src='http://www.blogtrog.com/scripts/bt_code.js' type='text/javascript'&gt;&lt;/script&gt;&lt;script language='javascript' type='text/javascript'&gt;bt_code_init('7ed4c602-d8b8-44bb-a748-9dc02ac17175');&lt;/script&gt;&lt;br /&gt;Comments and questions are very welcome!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-1689475670514322723?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/1689475670514322723/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/02/accessing-command-line-from-php-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/1689475670514322723'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/1689475670514322723'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/02/accessing-command-line-from-php-code.html' title='Accessing command line from PHP code'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7960357115971384899.post-7184117100727808448</id><published>2009-01-13T14:14:00.009+02:00</published><updated>2009-12-01T12:04:14.797+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='default'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='terminal'/><category scheme='http://www.blogger.com/atom/ns#' term='ubuntu'/><category scheme='http://www.blogger.com/atom/ns#' term='size'/><title type='text'>Setting up the terminal (Ubuntu)</title><content type='html'>My first encounter with Ubuntu happened when I started my current job. In contrast to Fedora (the Linux distribution installed in school) it was easy and stable, and for the first time I really started to appreciate Linux. When I had to pick a distro to install on my laptop for Communication Applications course the choice was pretty obvious.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;Now 2 month deep in to the semester I find myself using Ubuntu in general a lot (yes, I watch movies in Linux) and the terminal specifically (for debugging Comm Apps exercises). There are a few little terminal tips I'd like to share with the Ubuntu terminal beginners:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;Adding terminal launcher to panel&lt;/span&gt;&lt;br /&gt;Instead of going to &lt;span style="font-style: italic;"&gt;Application &gt; Accessories &gt; Terminal&lt;/span&gt; every time, you can add the launcher (shortcut in Windowsian) to the panel for a quick reach like this: go to &lt;span style="font-style: italic;"&gt;Application &gt; Accessories, &lt;/span&gt;but instead of left clicking the &lt;span style="font-style: italic;"&gt;Terminal&lt;/span&gt; right click it and pick &lt;span style="font-style: italic;"&gt;Add this launcher to panel&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_wjORJvW77TA/SWyKEuEMBII/AAAAAAAAAAM/vv71iyVPPAk/s1600-h/add_terminal"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 283px;" src="http://1.bp.blogspot.com/_wjORJvW77TA/SWyKEuEMBII/AAAAAAAAAAM/vv71iyVPPAk/s320/add_terminal" alt="" id="BLOGGER_PHOTO_ID_5290755475943195778" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;Changing the default terminal size&lt;/span&gt;&lt;br /&gt;If you're running the terminal for debugging purposes like me, chances are you are not satisfied with the default size of the terminal - especially if you want to have a few tabs (in Ubuntu you can open a few tabs in one terminal - to open a new tab press &lt;span style="font-style: italic;"&gt;CTRL + SHIFT + T&lt;/span&gt;). Resizing manually every time you open the terminal is an option, but a lame one. So here are two ways to make it happen automatically:&lt;br /&gt;The first way is through the launcher on the panel we've just added: Right click the terminal launcher and pick Properties&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_wjORJvW77TA/SWyM08n9haI/AAAAAAAAAAU/yVhArFVoRWk/s1600-h/terminal_properties"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 151px;" src="http://3.bp.blogspot.com/_wjORJvW77TA/SWyM08n9haI/AAAAAAAAAAU/yVhArFVoRWk/s320/terminal_properties" alt="" id="BLOGGER_PHOTO_ID_5290758503508313506" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;In the properties window edit the Command field, so it looks something like:&lt;br /&gt;&lt;span style="font-style: italic;"&gt;gnome-terminal --geometry=120x26&lt;/span&gt;&lt;br /&gt;Where 120 is the number of chars in a row (width) and 26 is the number of rows (height)&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_wjORJvW77TA/SWyN3XRZJCI/AAAAAAAAAAc/Uquvbchy-RY/s1600-h/in_terminal_props"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 120px;" src="http://1.bp.blogspot.com/_wjORJvW77TA/SWyN3XRZJCI/AAAAAAAAAAc/Uquvbchy-RY/s320/in_terminal_props" alt="" id="BLOGGER_PHOTO_ID_5290759644532778018" border="0" /&gt;&lt;/a&gt;The second way (if you're having troublems with the first one - clashes and stuffs or if you need a lower level control for some reason) is to edit the &lt;span style="font-weight: bold;"&gt;/usr/share/vte/termcap/xterm&lt;/span&gt; file, which holds the terminal profiles. To do this, run the terminal, and type this command:&lt;br /&gt;&lt;span style="font-style: italic;"&gt;sudo gedit &lt;/span&gt;&lt;span style="font-style: italic;"&gt;/usr/share/vte/termcap/xterm&lt;/span&gt;&lt;br /&gt;This will open gedit text editor with the xterm file in it (you type in the &lt;span style="font-style: italic;"&gt;sudo&lt;/span&gt; before the command to open the file in super user mode which gives you access to special system files) look for a line which says something like:&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;:co#80:it#8:li#24:\&lt;/span&gt;&lt;br /&gt;and change it to:&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;:co#120:it#8:li#26:\&lt;/span&gt;&lt;br /&gt;To get the default size of 120x26&lt;br /&gt;&lt;br /&gt;That's it for now, critics and questions and general replies are welcome!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7960357115971384899-7184117100727808448?l=codeyoung.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeyoung.blogspot.com/feeds/7184117100727808448/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeyoung.blogspot.com/2009/01/setting-up-terminal-ubuntu.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/7184117100727808448'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7960357115971384899/posts/default/7184117100727808448'/><link rel='alternate' type='text/html' href='http://codeyoung.blogspot.com/2009/01/setting-up-terminal-ubuntu.html' title='Setting up the terminal (Ubuntu)'/><author><name>Euristic</name><uri>http://www.blogger.com/profile/17387203372761391731</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_wjORJvW77TA/SWyKEuEMBII/AAAAAAAAAAM/vv71iyVPPAk/s72-c/add_terminal' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
