<?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; Ethernet</title>
	<atom:link href="http://dooba.net/tag/ethernet/feed/" rel="self" type="application/rss+xml" />
	<link>http://dooba.net</link>
	<description>Tech, Science, Insanity</description>
	<lastBuildDate>Mon, 13 Sep 2010 22:17:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Simple UDP</title>
		<link>http://dooba.net/2009/06/23/simple-udp/?utm_source=rss&#038;utm_medium=rss&#038;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>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
