Archive

Archive for the ‘C’ Category

Simple UDP

June 23rd, 2009 No comments

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.

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 “server” that spins on the FPGA and receives the incoming messages, and a “client” app that can send a command to the “server”. This is a trvial example and the code is basic, but you can see how easy it would be to expand as needed.

The Client:

#include 
#include 
#include 
#include 
#include 

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 *) &server, sizeof(server)) < 0) {
                   die("failed to send message");
	}

	return 0;
}

And now the Server:

#include 
#include 
#include 
#include 
#include 
#include 

#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
\n", argv[0]);
		exit(1);
	}

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

	memset(&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 *) &echoserver, serverlen) < 0) {
		Die("failed to bind.");
	}

	while(1){

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

	}
}
Categories: C, Code, Embedded, Uncategorized, Xilinx Tags: , , , ,

C++ itoa

June 11th, 2009 No comments

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.

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 && base == 10) buf += '-';

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

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

    return buf;
} 
Categories: C, Code Tags: