Translate

Showing posts with label Programming World. Show all posts
Showing posts with label Programming World. Show all posts

Thursday, 1 November 2018

Simple Data Structure Implementation


Data Structure Basic Concept and Program using C language

Include the header file 
#include <stdio.h>
#include <stdlib.h>

Define the Node Strucure
struct Node{
 int data;
 struct Node* next;
};

Function to add data on begining
void addBegning(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = *head;
 *head = newNode;
}

Function to add data at last position
void addLast(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* last = *head;
 newNode->data = newData;
 newNode->next = NULL;
 if(*head == NULL){
  *head = newNode;
  return;
 }
 while(last->next != NULL){
  last = last->next;
 }
 last->next = newNode;
 return;
}

Inserting data after some node
void insertAfter(struct Node* head, int newData){ 
 if(head == NULL) return;
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = head->next;
 head->next = newNode;

}

Deleting a element from data structure
void deleteElement(struct Node** head, int element){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* temp = *head, *prev;
 if(temp != NULL && temp->data == element){
  *head = temp->next;
  free(temp);
  return;
 }
 while(temp != NULL && temp->data != element){
  prev = temp;
  temp = temp->next;
 }
 if(temp == NULL){ printf("Element does not exist in list"); return;}
 prev->next = temp->next;
 free(temp);
}

Reverse the Linked List
static void reverseList(struct Node** head){
 struct Node* current = *head;
 struct Node* next = NULL;
 struct Node* prev = NULL;
        while(current != NULL){
              next = current->next;
                current->next = prev;
                prev = current;
                current = next;
        }
 *head = prev;
}

Print the data
void printData(struct Node* node){
 while(node != NULL){
  printf("%d\t", node->data);
  node = node->next;
 }
 printf("\n");
}

Main 
int main(){
 struct Node* head = NULL;
 addBegning(&head, 2);
 addBegning(&head, 50);
 addBegning(&head, 9);
 addLast(&head, 10);
 addLast(&head, 25);
 insertAfter(head->next->next, 30); //head-1, head->next-2, head->next->next-3
 printf("Before Deletion\n");
 printData(head);
 deleteElement(&head, 30);
 printf("After Deletion element 30\n");
 printData(head);
 printf("After Reverse Linked list\n");
 reverseList(&head);
 printData(head);
 return 0;
}

One File Program 
#include <stdio.h>
#include <stdlib.h>

struct Node{
 int data;
 struct Node* next;
};

void addBegning(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = *head;
 *head = newNode;
}
void addLast(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* last = *head;
 newNode->data = newData;
 newNode->next = NULL;
 if(*head == NULL){
  *head = newNode;
  return;
 }
 while(last->next != NULL){
  last = last->next;
 }
 last->next = newNode;
 return;
}
void insertAfter(struct Node* head, int newData){ 
 if(head == NULL) return;
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = head->next;
 head->next = newNode;

}
void deleteElement(struct Node** head, int element){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* temp = *head, *prev;
 if(temp != NULL && temp->data == element){
  *head = temp->next;
  free(temp);
  return;
 }
 while(temp != NULL && temp->data != element){
  prev = temp;
  temp = temp->next;
 }
 if(temp == NULL){ printf("Element does not exist in list"); return;}
 prev->next = temp->next;
 free(temp);
}
static void reverseList(struct Node** head){
 struct Node* current = *head;
 struct Node* next = NULL;
 struct Node* prev = NULL;
        while(current != NULL){
              next = current->next;
                current->next = prev;
                prev = current;
                current = next;
        }
 *head = prev;
}
void printData(struct Node* node){
 while(node != NULL){
  printf("%d\t", node->data);
  node = node->next;
 }
 printf("\n");
}
int main(){
 struct Node* head = NULL;
 addBegning(&head, 2);
 addBegning(&head, 50);
 addBegning(&head, 9);
 addLast(&head, 10);
 addLast(&head, 25);
 insertAfter(head->next->next, 30); //head-1, head->next-2, head->next->next-3
 printf("Before Deletion\n");
 printData(head);
 deleteElement(&head, 30);
 printf("After Deletion element 30\n");
 printData(head);
 printf("After Reverse Linked list\n");
 reverseList(&head);
 printData(head);
 return 0;
}

Give Something to the world and it will never let you down.  
Onkar Dubey 




Service that allow to run a program on boot (Linux Platforn)



In this tutorial I will show you how you can run a program on boot (startup) using Linux platform this will work on all Linux distros
This service can run all type of executable file.
If you want to run you ".c" file, first compile it and put the compiled file name with the directory as written in Step-3 "ExecStart = "..........." and change the working directory directory in Step-3 WorkingDirectory= " ................" this directory is your executable file directory.

This is tested on Ubuntu 18.

Step 1- Go to system directory

$ cd /etc/systemd/system

Step 2- Open vi Edititor and write the service

$ vim myservice.service
Step 3- Paste the following code as instructed
--------------------------------------------------------------------------------------------------------------------------
[Unit]
# This is the name of service
Description=om service
[Service]
#User is as root, the program will run as root user
User=root
# The configuration file application.properties should be here:
#change this to your workspace
WorkingDirectory=/home/om/workplace
#path to executable. 
#put the executable file
ExecStart=/home/om/workplace/myprog
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
--------------------------------------------------------------------------------------------------------------------------

Step 4- Enable the service

$ sudo systemctl daemon-reload
$ sudo systemctl enable sample.service
$ sudo reboot
If You need any further query please send a message or comment.



Give Something to the world and it will never let you down.  
 
Onkar Dubey 
 




Friday, 7 September 2018

Return array of string and taking refence of a variable in C

#include <stdio.h>
#include <string.h>
#include <malloc.h>

char **logFileSelection(int *len){
        char ** arr = malloc(10 * sizeof(char *));
        char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        int i, j = 0;
        for(i = 0; i < 12; i++){
               arr[j] = malloc(50 * sizeof(char));
               arr[j] = month[i];
               j++;
               *len = *len+1;
        }
        return arr;
}

int main(){
        int len = 0;
        char **arr = logFileSelection(&len);
        printf("length: %d\n", len);
        int i;
        for(i = 0; i < len; i++){
                printf("%s\n", arr[i]);
        }
return 0;
}

Friday, 23 March 2018

Java Program




public class testCase1{
public static void main(String[] args) {

/* ***********Pattern Printing*********** */

/*
int k = 1;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%d", k);
k++;
}
System.out.println();
k--;
}}}
1
12
234
4567


*/
public class testCase1{
public static void main(String[] args) {

for (int i = 0; i <= 5; i++) {
for (int j = 1; j <= 5-i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i*2; k++) {
System.out.print("#");
}
System.out.println();
}}}

     #
    ###
   #####
  #######
 #########
###########



/*
public class testCase1{
public static void main(String[] args) {
for (int i = 0; i <= 5; i++){
for(int j = 0; j <= i; j++){
System.out.print(" ");
}
for(int k = 0; k <= 5-i; k++){
System.out.print("#");
}
System.out.println();
}}}
 ######
  #####
   ####
    ###
     ##
      #

*/
/*
public class testCase1{
public static void main(String[] args) {
for(int i = 0; i <= 5; i++){
for(int j = 0; j <= i; j++){
System.out.print(" ");
}
for(int k = 0; k <= 2*(5-i); k++){
System.out.print("#");
}
System.out.println();
}}}
 ###########
  #########
   #######
    #####
     ###
      #

*/
/*
public class testCase1{
public static void main(String[] args) {
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5-i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i*2; k++) {
System.out.print("#");
}
System.out.println();
}
for(int i = 0; i <= 4; i++){
for(int j = 0; j <= i; j++){
System.out.print(" ");
}
for(int k = 0; k <= 2*(4-i); k++){
if( k == 0) System.out.print(" ");
System.out.print("#");
}
System.out.println();
}}}
      #
     ###
    #####
   #######
  #########
 ###########
  #########
   #######
    #####
     ###
      #

*/
/*
public class testCase1{
public static void main(String[] args) {
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= i; j++){
System.out.print(j);
}
System.out.println();
}}}
1
12
123
1234
12345

*/
/*
public class testCase1{
public static void main(String[] args) {
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= i; j++){
System.out.print(" ");
}
for(int k = 1; k <= 5-i+1; k++){
System.out.print(k);
}
System.out.println();
}}}
 12345
  1234
   123
    12
     1

*/
/*
public class testCase1{
public static void main(String[] args) {
int s = 1,temp = 1;
for(int i = 1; i <= 5; i++){
for(int k = 1; k <= 5-i; k++){
System.out.print(" ");
}
for(int k = 1; k <= i; k++){
System.out.print(temp);
temp--;
}
s++;
temp = s;
System.out.println();
}}}
    1
   21
  321
 4321
54321

*/
/*
public class testCase1{
public static void main(String[] args) {
int a = 8;
for(int i = 1; i <= 8; i++){
for(int j = a; j >= 1; j--){
System.out.print(j);
}
a--;
System.out.println();
}}}
87654321
7654321
654321
54321
4321
321
21
1

*/
/*
public class testCase1{
public static void main(String[] args) {
int s = 1, a = 1;
for(int i = 1; i <= 8; i++){
for(int j = 8; j >= i; j--){
System.out.print(" ");
}
for(int k = 1; k <= i; k++){
System.out.print(k);
}
for(int l = 1; l <= i-1; l++){
System.out.print(s);
s--;
}
s = a;
a++;
System.out.println();
}}}
        1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321

*/
/*
public class testCase1{
public static void main(String[] args) {
int x = 7, a = 6;
for(int i = 1; i <= 8; i++){
int s= 1;
for(int j = 1; j <= i; j++){
System.out.print(" ");
}
for(int k = 1; k <= 8-i+1; k++){
System.out.print(s);
s++;
}
for(int l= 1; l <= 8-i; l++){
System.out.print(x);
x--;
}
x = a;
a--;
System.out.println();
}}}
 123456787654321
  1234567654321
   12345654321
    123454321
     1234321
      12321
       121
        1

*/
/*
public class testCase1{
public static void main(String[] args) {
int y = 1, z = 1;
for(int i = 1; i <= 8; i++){
for(int j = 1; j <= i; j++){
System.out.print(j);
}
for(int k = 1; k <= 8-i-1; k++){
System.out.print(" ");
}
for(int l= 1; l <= 8-i; l++){
System.out.print(" ");
}
for(int m = 1; m <= i; m++){
if(y > 0)
System.out.print(y);
y--;
}
if(z < 7)
z++;
y = z;

System.out.println();
}}}
1             1
12           21
123         321
1234       4321
12345     54321
123456   654321
1234567 7654321
123456787654321

*/

/* find max number in array */
/*
import java.util.Scanner;
public class testCase1
{
    public static void main(String[] args)
    {
        int n, max;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter number of elements in the array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter elements of array:");
        for(int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        max = a[0];
        for(int i = 0; i < n; i++)
        {
            if(max < a[i])
            {
                max = a[i];
            }
        }
        System.out.println("Maximum value:"+max);
    }
}
Enter number of elements in the array:10
Enter elements of array:
2
5
7
8
3
5
7
10
15
9
Maximum value:15

*/
/* Selection Sorting */
/*
import java.util.Scanner;
public class testCase1
{
    public static void main(String[] args)
    {
        int n,temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter number of elements in the array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter elements of array:");
        for(int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
   
        for(int i = 0; i < n-1; i++)
        {
for(int j = 0; j < n-i-1; j++){
            if(a[j] <= a[j+1]){
    temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
    }
}
        }
for(int i = 0; i < n; i++){
        System.out.print(a[i]);
}
    }
}
Enter number of elements in the array:8
Enter elements of array:
2
5
9
3
1
0
6
8
98653210om@om:~/Documents$
*/



Give Something to the world and it will never let you down.  
 
Onkar Dubey





Monday, 29 January 2018

Textual description of firstImageUrl

Getting RMS Value from FFMPEG using C Language and setting up the Audio Level to use Train announcement System



Author Name : Onkar Dubey

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{

    int  count = 0, flag = 1;
    FILE *pipein;
    pipein  = popen("ffmpeg -f alsa -i hw:0,0 -af astats=metadata=1:reset=1,ametadata=print:key=lavfi.astats.Overall.RMS_level -f null - 2>&1", "r");

    char line[1024];
    char *s;
    double om_rms;

    while(1)
    {

        fscanf(pipein, "%[^\n]\n", line);

        s = strstr(line, "RMS_level");
        if (s)
        {
            s += 10;

            om_rms = atof(s);

            // Print the RMS value
            //fprintf(stderr, "  RMS_VALUE = %lf\n", om_rms);

                if(om_rms > -15.00000)
                {
                    count ++;
                    if((count == 10) && (flag == 1))
                        {
                        printf("Alert ON\n");
                        fprintf(stderr, "Train announcement is going on....Stop othe process\nRMS_VALUE = %lf dB\n\n", om_rms);
                        flag = 0;
                        }
                }
                else if (om_rms < -18.00000)
                {
                    count --;
                    if((count == -200) && (flag == 0))
                        {
                        printf("Alert OFF\n");
                        fprintf(stderr, "Train announcement have been stoped..... Start othe process\nRMS_VALUE = %lf dB\n\n", om_rms);
                        flag = 1;
                        }
                    else if (count < -200) count = -200;
                }
                else count = 0;
         }

}}




 To connect with socket

 server.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void main(){

    char alert[10] = "Alert ON";
    char alert1[10] = "Alert OFF";
    int count = 0, flag = 1;
    FILE *pipein;
    pipein  = popen("ffmpeg -f alsa -i hw:0,0 -af astats=metadata=1:reset=1,ametadata=print:key=lavfi.astats.Overall.RMS_level -f null - 2>&1", "r");

    char line[1024]; // longer than required - only stores one line of text at a time!
    char *s;
    double om_rms;

        int net_socket;
        net_socket=socket(AF_INET,SOCK_STREAM,0);

        struct sockaddr_in server_address;
        server_address.sin_family=AF_INET;
        server_address.sin_port=htons(2020);
        server_address.sin_addr.s_addr=INADDR_ANY;

        int k = bind(net_socket, (struct sockaddr *) &server_address, sizeof(server_address));
        if(k<0){
                printf("Error in socket binding");
        }
        listen(net_socket,6);

        int client_socket=accept(net_socket,NULL, NULL);





    while(1){
        // Read a line of text from the input pipe
        fscanf(pipein, "%[^\n]\n", line);

        // Find the substring "RMS_level" if it is present in this line
        s = strstr(line, "RMS_level");
        if (s){
            // Substring "RMS_level" was found, so jump to beginning of value
            s += 10;

            // Convert the value from a string (the rest of the line) to a double
            om_rms = atof(s);

            // Print the RMS value
            //fprintf(stderr, "  RMS_VALUE = %lf\n", om_rms);
            if(om_rms > -26.000000)
                {
                    count ++;
                    if((count == 10) && (flag == 1))
                        {
                        send(client_socket,alert,sizeof(alert),0);
                        printf("Alert ON\n");
                        fprintf(stderr, "User is Speaking, Stop othe process  RMS_VALUE = %lf\n\n", om_rms);
                        flag = 0;
                        }
                }
                else if (om_rms < -28.00000)
                {
                    count --;
                    if((count == -200) && (flag == 0))
                        {
                        int client_socket=accept(net_socket,NULL, NULL);
                        send(client_socket,alert1,sizeof(alert1),0);
                        printf("Alert OFF\n");
                        fprintf(stderr, "User is not speaking, Start othe process  RMS_VALUE = %lf\n\n", om_rms);
                        flag = 1;
                        }
                else if (count < -200) count = -200;
                }
                else count = 0;
         }
    }
}


Client.c


#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


int main(){
        int network_socket;
    char recieve_msg[20];

        network_socket=socket(AF_INET,SOCK_STREAM,0);

        struct sockaddr_in server_address;
        server_address.sin_family=AF_INET;
        server_address.sin_port=htons(2020);
        server_address.sin_addr.s_addr=INADDR_ANY;

int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
        if(connection_status<0){
                printf("Error in connection\n");
        close(network_socket);
        }
    else {
    int ret = recv(network_socket,&recieve_msg,sizeof(recieve_msg),0);
while(1)
{
if(ret){
    printf("Recieved massage is:  %s\n: ",recieve_msg);
    }
}
close(network_socket);


return 0;
}
}

to execute the program run as
server side
$ gcc -o server server.c
$ ./server 2020
client side
$ gcc -o Client Client.c
$ ./Client 2020
     

I will make it more reliable to get that inbox me.
To know more detail inbox or comment on the comment box


                                      Give Something to the world and it will never let you down. 
                                                                                           Onkar Dubey 
 
 

Thursday, 28 December 2017

Textual description of firstImageUrl

Temperature Sensor and LCD 16x2 Interfacing with ATmega16


Temperature Sensor and LCD 16x2 Interfacing with ATmega16

Proteus Design

YouTube Video Link




* GccApplication2.c
 *
 * Created: 12/12/2017 6:32:40 AM
 * Author : Om
 */
#define F_CPU 800000
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define enable            5
#define registerselection 6

void lcd(void);
void send_a_command(unsigned char command);
void send_a_character(unsigned char character);
void send_a_string(char *string_of_characters);
int main(void)
{
DDRC = 0xFF;
DDRA = 0x00;
DDRD = 0xFF;
DDRB = 0xFF;
_delay_ms(50);
lcd();
}
void lcd(void){
ADMUX |=(1<<REFS0)|(1<<REFS1);
ADCSRA |=(1<<ADEN)|(1<<ADATE)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);

int16_t COUNTA = 0;
char SHOWA [3];

send_a_command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(50);
send_a_command(0x38);
_delay_ms(50);
send_a_command(0b00001111);
_delay_ms(50);
//while (1)
//{
// ADCSRA |=(1<<ADSC);
//}

while(1)
{
ADCSRA |=(1<<ADSC);
COUNTA = ADC/4;
send_a_string ("Current temp is");
send_a_command(0x80 + 0x40 + 0);
send_a_string ("In (C) = ");
//send_a_command(0x80 + 0x40 + 8);
itoa(COUNTA,SHOWA,10);
send_a_string(SHOWA);
send_a_string ("      ");
send_a_command(0x80 + 0);
send_a_command(0x0C);

}
}
void send_a_command(unsigned char command)
{
PORTC = command;
PORTD &= ~ (1<<registerselection);
PORTD |= 1<<enable;
_delay_ms(20);
PORTD &= ~1<<enable;
PORTC = 0;
}
void send_a_character(unsigned char character)
{
PORTC = character;
PORTD |= 1<<registerselection;
PORTD |= 1<<enable;
_delay_ms(20);
PORTD &= ~1<<enable;
PORTC = 0;
}
void send_a_string(char *string_of_characters)
{
while(*string_of_characters > 0)
{
send_a_character(*string_of_characters++);
}
}


                                      Give Something to the world and it will never let you down. 
                                                                                           Onkar Dubey 
 
 

Saturday, 9 December 2017

Port Programming

Port Programming 

Serial Port Opening and sending and accessing data using "AT" Commands

Instructions:

This code is verified in Linux Ubuntu 16.04.3 LTS

 First check the USB Port using following given command

echo Hello > /dev/pts/1
echo xyz > /dev/pts/2
echo ..... > /dev/pts/...
and so on...  put something on the position of ... when You get the response that means port can be use
exp:
echo Welcome > /dev/pts/6    hit enter
Welcome
if you get "Welcome" that means You can use that port.     
Change the path of the port in code and compile using
gcc -o serial_port serial_port.c
and run it using 
./serial_port

serial_port.c

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>

int main(){
    int fd;
    //char path[10];
    struct termios options;

    /* ################# Open the port #################*/

    printf("Enter com_port path\n sorry not now\n");
//    scanf("%c",&path);
    fd = open("/dev/pts/1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
   {                                              /* Could not open the port */
     fprintf(stderr, "open_port: Unable to open /dev/pts/6 - %s\n",strerror(errno));
   }else{
printf("port opened\n");
    }

    fcntl(fd, F_SETFL, 0);

    /* ################ Get the current options ###############*/

    tcgetattr(fd, &options);

    /*############## set raw input, 1 second timeout */

    options.c_cflag     |= (CLOCAL | CREAD);
    options.c_lflag     &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_oflag     &= ~OPOST;
    options.c_cc[VMIN]  = 0;
    options.c_cc[VTIME] = 10;

    /*################### set the options ##################*/

    tcsetattr(fd, TCSANOW, &options);

char buffer[400];  /* Input buffer */
      char *bufptr;      /* Current char in buffer */
      int  nbytes;       /* Number of bytes read */
      int  tries;        /* Number of tries so far */

      for (tries = 0; tries < 1; tries ++)
      {
       /* send an AT command*/
if (write(fd, "AT+CMGL=\"ALL\"\n", strlen("AT+CMGL=\"ALL\"\n")) < 3){
printf("command sent\n");
  continue;
if(write(fd, "AT+CMGL=\"ALL\"\r", strlen("AT+CMGL=\"ALL\"\r"))=="clc")
goto loop;

}



       /*################## Read characters into our string buffer ################*/

bufptr = buffer;

nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1);
printf("%s\n",bufptr);

    char *p;

    p = strstr(buffer, "tin");
    printf("%s",p);

p = strstr(buffer, "server");
if(p == NULL) printf("not from server\n");
  *bufptr = '\0';
loop: close(fd);
 }

close(fd);
return 0;
}



                                      Give Something to the world and it will never let you down. 
                                                                                           Onkar Dubey 

Thursday, 16 November 2017

Textual description of firstImageUrl

UDP Implementation






       


Here's a simple UDP Server Client Implementation in C for Unix/Linux.
As UDP is a connection-less protocol, it is not reliable or we can say that we don't send acknowledgements for the packets sent.

Here is a concurrent UDP server which can accept packets from multiple clients simultaneously.
The port mentioned here can be changed to any value between 1024 and 65535 (since upto 1024 are known ports).
//UDPServer.c
/*
 *  gcc -o server UDPServer.c
 *  ./server
 */
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 512
#define PORT 9930
void err(char *str)
{
    perror(str);
    exit(1);
}
int main(void)
{

    
struct sockaddr_in my_addr, cli_addr;
    int sockfd, i;
    socklen_t slen=sizeof(cli_addr);
    char buf[BUFLEN];


/* ############### Socket Opening ############# */


    if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      err("socket");
    else
      printf("Server : Socket() successful\n");
    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);


/* ############### Socket Binding ############## */    


    if (bind(sockfd, (struct sockaddr* ) &my_addr, sizeof(my_addr))==-1)
      err("binding Error");
    else
      printf("Server : bind() successful\n");


/* ################ Data Receiving ############### */


    while(1)
    {
        if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&cli_addr, &slen)==-1)
            err("recvfrom()");
        printf("Received packet from %s:%d\nData: %s\n\n",
               inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), buf);
    }
    close(sockfd);
    return 0;
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//UDPClient.c
/*
 * gcc -o client UDPClient.c
 * ./client <server-ip>
 */
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 512
#define PORT 9930
void err(char *s)
{
    perror(s);
    exit(1);
}
int main(int argc, char** argv)
{
    struct sockaddr_in serv_addr;
    int sockfd, i, slen=sizeof(serv_addr);
    char buf[BUFLEN];
    if(argc != 2)
    {
      printf("Usage : %s <Server-IP>\n",argv[0]);
      exit(0);
    }



/* ############### Socket Opening ############# */


    if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
        err("socket");
    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    if (inet_aton(argv[1], &serv_addr.sin_addr)==0)
    {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }



/* ############### Data Sending ############## */    

   
 while(1)
    {
        printf("\nEnter data to send(Type exit and press enter to exit) : ");
        scanf("%[^\n]",buf);
        getchar();
        if(strcmp(buf,"exit") == 0)
          exit(0);
        if (sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&serv_addr, slen)==-1)
            err("sendto()");
    }
    close(sockfd);
    return 0;
}