Category:EmbeddedLinux - UDP Server-Client

From Digi Developer

Jump to: navigation, search


Contents

Time Program for EmbeddedLinux

EmbeddedLinux UDP Server-Client (For Digi EmbeddedLinux 5.x modules) This example Demonstrates UDP sockets.


Test Files

This sample program contains three files, Makefile, Client.c and Serv.c.


EmbeddedLinux UDP Server-Client Sample Application

The EmbeddedLinux UDP Server-Client Sample application can be found here: Media:UDP Server-Client.zip


Basic Usage

Compile, load and run application.


Sample of client.c file:

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
 
int main(int argc, char**argv)
{
	if(argc != 3)
	{
		printf("Enter Server IP & UDP port as command line arguments, eg: ./client x.x.x.x 5001\n");
		exit(-1);
	}
	int serv_des;
	struct sockaddr_in serv_addr;
	char buff[20];
	socklen_t sock_len;
 
	printf("UDP Client\n");
	serv_addr.sin_family = AF_INET;
 
	if(inet_aton(argv[1],&(serv_addr.sin_addr)) == 0)
	{
		printf("Wrong IP format\n");
		exit(-1);
	}
 
	serv_addr.sin_port = htons(atoi(argv[2]));
	memset(&(serv_addr.sin_zero),'\0',8);
	sock_len = sizeof(struct sockaddr);
 
	if( (serv_des = socket(PF_INET,SOCK_DGRAM,0) ) < 0){
		perror("Server Descriptor Creation Failed\n");
	}
 
 
	if(sendto(serv_des,"I Am UDP Client\0",16,0,(struct sockaddr*)&serv_addr,sock_len) <0){
		perror("Send To Failed\n");
	}
 
 
	if(recvfrom(serv_des,buff,16,0,(struct sockaddr*)&serv_addr,&sock_len) <0 ){
		perror("Receive From Failed\n");
	}
 
	printf("Server Says : %s\n",buff);
 
 
	return 0;
}

This category currently contains no pages or media.

Personal tools
Wiki Editing