<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dooba.net &#187; C</title>
	<atom:link href="http://dooba.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://dooba.net</link>
	<description>Tech, Science, Insanity</description>
	<lastBuildDate>Thu, 09 Sep 2010 03:09:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>LINQPad beyond Linq</title>
		<link>http://dooba.net/2010/09/09/linqpad-beyond-linq/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=linqpad-beyond-linq</link>
		<comments>http://dooba.net/2010/09/09/linqpad-beyond-linq/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 02:35:25 +0000</pubDate>
		<dc:creator>Markis</dc:creator>
				<category><![CDATA[Linq]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Linqpad]]></category>

		<guid isPermaLink="false">http://dooba.net/2010/09/09/linqpad-beyond-linq/</guid>
		<description><![CDATA[LINQPad is a great way to learn Linq, but there is plenty of information on their site about that: http://linqpad.net/.  And I don&#8217;t need to rehash all the information on their site.  Instead, I would like to talk about the ways LINQPad can be used beyond that.  I am also going to assume you know [...]]]></description>
			<content:encoded><![CDATA[<p>LINQPad is a great way to learn Linq, but there is plenty of information on their site about that: <a href="http://linqpad.net/">http://linqpad.net/</a>.  And I don&#8217;t need to rehash all the information on their site.  Instead, I would like to talk about the ways LINQPad can be used beyond that.  I am also going to assume you know about the Dump function native to LINQPad and general understanding of LINQPad.</p>
<h3>Using LINQPad to Test simple .NET Functions</h3>
<p>LINQPad can be used to test any .NET function.  Simply type in the .NET<br />
<a href="http://dooba.net/wp-content/uploads/2010/09/image.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb.png" border="0" alt="image" width="536" height="367" /></a></p>
<h3>Using LINQPad to test ASMX Web Services</h3>
<p>Generate the SOAP client &#8211; open “Visual Studio Command Prompt” and run “WSDL &lt;url&gt;”</p>
<p><a href="http://dooba.net/wp-content/uploads/2010/09/image1.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb1.png" border="0" alt="image" width="535" height="272" /></a></p>
<p>Compile the SOAP client &#8211; run “csc /t:library &lt;filename generated in previous step&gt;”</p>
<p><a href="http://dooba.net/wp-content/uploads/2010/09/image2.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb2.png" border="0" alt="image" width="536" height="273" /></a></p>
<p>Add the correct references in LINQPad, one to the library we just created and one to System.Web.Services.dll</p>
<p><a href="http://dooba.net/wp-content/uploads/2010/09/image3.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb3.png" border="0" alt="image" width="536" height="365" /></a></p>
<p>Type the necessary code to call the web service, just as you would have normally in Visual Studio</p>
<p><a href="http://dooba.net/wp-content/uploads/2010/09/image4.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb4.png" border="0" alt="image" width="535" height="367" /></a></p>
<h3>Using LINQPad to call a WCF Service</h3>
<p>Generate the client &#8211; open “Visual Studio Command Prompt” and run “SVCUTIL /config:&lt;path to LINQPad’s config file&gt; /mergeConfig &lt;url&gt;”<br />
Note that this will generate the client and will add the necessary settings in LINQPad’s config file</p>
<p><a href="http://dooba.net/wp-content/uploads/2010/09/image5.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb5.png" border="0" alt="image" width="532" height="270" /></a></p>
<p>Compile the client – run “csc /t:library &lt;code file generated in previous step&gt;”</p>
<p><a href="http://dooba.net/wp-content/uploads/2010/09/image6.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb6.png" border="0" alt="image" width="535" height="272" /></a></p>
<p>Add the correct references in LINQPad, one to the library we just created and one to System.ServiceModel.dll</p>
<p><a href="http://dooba.net/wp-content/uploads/2010/09/image7.png"><img style="display: inline; border-width: 0px;" title="image" src="http://dooba.net/wp-content/uploads/2010/09/image_thumb7.png" border="0" alt="image" width="533" height="364" /></a></p>
<p>Add the follow code and make adjustments as necessary</p>
<blockquote><p>var endpoint = new System.ServiceModel.EndpointAddress(&#8220;<a href="http://www.ecubicle.net/gsearch_rss.asmx&quot;);">http://www.ecubicle.net/gsearch_rss.asmx&#8221;);</a><br />
var binding = new System.ServiceModel.BasicHttpBinding();<br />
var oProxy = System.ServiceModel.ChannelFactory&lt;gsearch_rssSoapChannel&gt;.CreateChannel(binding, endpoint);</p>
<p>/* Code specific to service */<br />
var request = new GetSearchResultsRequest() { Body = new GetSearchResultsRequestBody() };<br />
request.Body.searchPage = &#8220;&#8221;;<br />
request.Body.gQuery = &#8220;Test&#8221;;<br />
request.Body.numOfResults = &#8220;50&#8243;;</p>
<p>bool successfulCall = false;<br />
try<br />
{<br />
/* Service Call */<br />
oProxy.GetSearchResults(request).Dump();<br />
oProxy.Close();<br />
successfulCall = true;<br />
}<br />
finally<br />
{<br />
if (!successfulCall)<br />
{<br />
oProxy.Abort();<br />
}<br />
}</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://dooba.net/2010/09/09/linqpad-beyond-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple UDP</title>
		<link>http://dooba.net/2009/06/23/simple-udp/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=simple-udp</link>
		<comments>http://dooba.net/2009/06/23/simple-udp/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 16:00:34 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Embedded]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Xilinx]]></category>
		<category><![CDATA[Ethernet]]></category>
		<category><![CDATA[FPGA]]></category>

		<guid isPermaLink="false">http://dooba.net/?p=152</guid>
		<description><![CDATA[There are many reasons why you might need to send data or messages between devices on your network. Maybe you want to control a remote box, or you have a small linux setup running somewhere remotely and you want to sent and receive data. There are obviously several ways to do this, and one is [...]]]></description>
			<content:encoded><![CDATA[<p>There are many reasons why you might need to send data or messages between devices on your network. Maybe you want to control a remote box, or you have a small linux setup running somewhere remotely and you want to sent and receive data. There are obviously several ways to do this, and one is using sockets and UDP.</p>
<p>This code came about when I needed to talk to an embeded CPU in a Xilinx FPGA. I have ethernet on the card as well, and the CPU is setup to handle it. I wanted to send control commands to the CPU from my Linux workstation to verify some functionality. To do this, I have two pieces of code: (1) a &#8220;server&#8221; that spins on the FPGA and receives the incoming messages, and a &#8220;client&#8221; app that can send a command to the &#8220;server&#8221;. This is a trvial example and the code is basic, but you can see how easy it would be to expand as needed.</p>
<p>The Client:</p>
<pre name="code" class="c">
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>

void die(char* msg) { perror(msg); exit(1); }

int main(int argc, char *argv[]) {
	int sock;
	struct sockaddr_in server;
	unsigned short int serverPort = 9999;

	sock= socket(AF_INET, SOCK_DGRAM, 0);
	if(sock < 0 ) die("failed to create socket");

	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr("127.0.0.1");
	server.sin_port = serverPort; .

	char msg[] = "Hello World. ";
	printf("We will send the message: \"%s\" to the server. \n", msg);

	if (sendto(socketDescriptor, msg, strlen(msg), 0, (struct sockaddr *) &#038;server, sizeof(server)) < 0) {
                   die("failed to send message");
	}

	return 0;
}
</pre>
<p>And now the Server:</p>
<pre name="code" class="c">
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>

#define BUFFSIZE 255
void Die(char* msg) { perror(msg); exit(1); }

int main(int argc, char* argv[]){
	int sock;
	struct sockaddr_in echoserver;
	struct sockaddr_in echoclient;

	char buffer[BUFFSIZE];
	unsigned int echolen, serverlen, clientlen;
	int rec = 0;

	if(argc != 2){
		fprintf(stderr, "USAGE: %s
<port>\n", argv[0]);
		exit(1);
	}

	/* create UDP socket */
	if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
		Die("failed to create socket");
	}

	memset(&#038;echoserver, 0, sizeof(echoserver));
	echoserver.sin_family = AF_INET;
	echoserver.sin_addr.s_addr = htonl(INADDR_ANY);
	echoserver.sin_port = htons(atoi(argv[1]));

	serverlen = sizeof(echoserver);
	if(bind(sock, (struct sockaddr *) &#038;echoserver, serverlen) < 0) {
		Die("failed to bind.");
	}

	while(1){

		clientlen = sizeof(echoclient);
		if((rec = recvfrom(sock, buffer, BUFFSIZE, 0, (struct sockaddr*)&#038;echoclient, &#038;clientlen)) < 0){
			Die("failed to rec mesg");
		}
		printf("client connected: %s\nData: %s\n\n", inet_ntoa(echoclient.sin_addr),buffer);

	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dooba.net/2009/06/23/simple-udp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ itoa</title>
		<link>http://dooba.net/2009/06/11/c-itoa/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=c-itoa</link>
		<comments>http://dooba.net/2009/06/11/c-itoa/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 22:34:43 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://dooba.net/?p=54</guid>
		<description><![CDATA[It’s super annoying that c/c++ has an atoi function but not an itoa. I’ve had to implement this countless times in my code, and I have a nice snipet that has worked well for me. This function supports output in an base from 2 to 16, and has a “pad” option that add a leading [...]]]></description>
			<content:encoded><![CDATA[<p>It’s super annoying that c/c++ has an atoi function but not an itoa. I’ve had to implement this countless times in my code, and I have a nice snipet that has worked well for me. This function supports output in an base from 2 to 16, and has a “pad” option that add a leading “0″ if the number of chars in the output string are not a multiple of two. This is nice for hex formatting.</p>
<pre name="code" class="cpp">
string itoa(int value, int base, bool pad){
    enum { kMaxDigits = 35 };
    string buf;
    buf.reserve( kMaxDigits);

    //check that the base is valid
    if(base < 2 || base > 16) return buf;
    int quotient = value;

    //translate number to string with base
    do{
        buf += “0123456789abcdef”[std::abs( quotient % base )];
        quotient /= base;
    } while( quotient );

    //append negative sign for base 10
    if(value <0 &#038;&#038; base == 10) buf += '-';

    reverse( buf.begin(), buf.end() );

    if(pad &#038;&#038; base != 10) if(buf.length()%2 == 1) buf = "0" + buf;

    return buf;
} </pre>
]]></content:encoded>
			<wfw:commentRss>http://dooba.net/2009/06/11/c-itoa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple bootloader&#8230;</title>
		<link>http://dooba.net/2009/06/11/a-simple-bootloader/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-simple-bootloader</link>
		<comments>http://dooba.net/2009/06/11/a-simple-bootloader/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 06:21:19 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Embedded]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Xilinx]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://dooba.net/?p=5</guid>
		<description><![CDATA[If you have ever used a Linux distro on an embedded system you know that a bootloader is a necessity. I&#8217;ve been working with PetaLinux for the Xilinx MicroBlaze and we have been using FS-boot to launch U-boot and then bring up Peta. However, recently I switched to BlueCat linux to take advantage of the [...]]]></description>
			<content:encoded><![CDATA[<p>If you have ever used a Linux distro on an embedded system you know that a bootloader is a necessity. I&#8217;ve been working with <a href="http://www.petalogix.com/">PetaLinux</a> for the Xilinx MicroBlaze and we have been using FS-boot to launch U-boot and then bring up Peta. However, recently I switched to BlueCat linux to take advantage of the MicroBlaze&#8217;s MMU. However, <a href="http://www.lynuxworks.com">BlueCat</a> doesn&#8217;t document a clear way to boot from flash. Below is some simple code to boot BlueCat. Note: the concept was taken from a Xilinx app note.</p>
<pre name="code" class="c">#include &lt;xparams.h&gt;
#define KDI_FLASH_LOC 0xXXXXXXXX //location of the linux image in flash
#define KDI_DDR_LOC 0xXXXXXXXX //location in DDR that you want your image to end up
#define KDI_LEN 0xXXXXXXXX //length of your image in bytes

main(){

uint8_t* kdi_flash_ptr = (uint8_t*)KDI_FLASH_LOC;
uint8_t* kdi_ddr_ptr = (uint8_t*)KDI_DDR_LOC;

void* laddr;

memcpy(kdi_flash_ptr, kdi_ddr_ptr, KDI_LEN);

laddr = (void*)KDI_DDR_LOC;
(*laddr)();

}</pre>
<p>You can use XPS to load your KDI file into flash, and then compile the bootloader into your bitstream, load the FPGA, and you are off!</p>
]]></content:encoded>
			<wfw:commentRss>http://dooba.net/2009/06/11/a-simple-bootloader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
