Friday, 19 February 2016

Socket programming in C using Threads

    /*
    
        C socket server example, handles multiple clients using threads
    
        Compile
    
        gcc server.c -lpthread -o server
    
    */
    
     
    
    #include <stdio.h>
    #include <string.h>    //strlen
    #include <stdlib.h>    //strlen
    #include <sys/socket.h>
    #include <arpa/inet.h> //inet_addr
    #include <unistd.h>    //write
    #include <pthread.h> //for threading , link with lpthread
    
    //the thread function
    void *connection_handler(void *);
    
    int main(int argc , char *argv[])
    {
        int socket_desc , client_sock , c;
        struct sockaddr_in server , client;
    
        //Create socket
        socket_desc = socket(AF_INET , SOCK_STREAM , 0);
        if (socket_desc == -1)
        {
            printf("Could not create socket");
        }
    
        puts("Socket created");
    
        //Prepare the sockaddr_in structure
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr("127.0.0.1");
        server.sin_port = htons( 8888 );
    
        //Bind
        if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
        {
            //print the error message
            perror("bind failed. Error");
            return 1;
        }
        puts("bind done");
    
        //Listen
        listen(socket_desc , 3);
         
        //Accept and incoming connection
        puts("Waiting for incoming connections...");
        c = sizeof(struct sockaddr_in);
    
        //Accept and incoming connection
        puts("Waiting for incoming connections...");
        c = sizeof(struct sockaddr_in);
        pthread_t thread_id;
    
        
        while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
        {
            puts("Connection accepted");
    
            if( pthread_create( &thread_id , NULL ,  connection_handler , (void*) &client_sock) < 0)
            {
                perror("could not create thread");
                return 1;
            }
    
            //Now join the thread , so that we dont terminate before the thread
            //pthread_join( thread_id , NULL);
            puts("Handler assigned");
        }
        if (client_sock < 0)
        {
            perror("accept failed");
            return 1;
        }
        return 0;
    }
    
     
    
    /*
    
     * This will handle connection for each client
    
     * */
    
    void *connection_handler(void *socket_desc)
    {
        //Get the socket descriptor
        int sock = *(int*)socket_desc;
        int read_size;
        char *message , client_message[2000];
    
        //Send some messages to the client
        message = "Greetings! I am your connection handler\n";
        write(sock , message , strlen(message));
        message = "Now type something and i shall repeat what you type \n";
        write(sock , message , strlen(message));
    
        //Receive a message from client
        while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
        {
            //end of string marker
            client_message[read_size] = '\0';
    
            //Send the message back to client
            write(sock , client_message , strlen(client_message));
    
            //clear the message buffer
            memset(client_message, 0, 2000);
        }
    
        if(read_size == 0)
        {
            puts("Client disconnected");
            fflush(stdout);
        }
        else if(read_size == -1)
        {
            perror("recv failed");
        }
        return 0;
    } 
    
    
    /*
    
        C socket client example, handles multiple clients using threads
        Compile
        gcc -o client client.c
        run using -->  ./client 
    
    */
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n,p,count=0;
        struct sockaddr_in serv_addr;
        char buff[256],client[100],temp_buff[100];
        FILE *id;
        id = popen("id -n -u","r");
        fgets(client,100,id);
        strtok(client,"\n");
        portno = 8888;
    
        bzero((char *) &serv_addr, sizeof(serv_addr));
    
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket"); 
        bzero((char *) &serv_addr, sizeof(serv_addr));
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(portno);
        serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
        //getnameinfo((struct sockaddr *)&serv_addr, sizeof serv_addr, host, sizeof host, service, sizeof service, 0);
        if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");
        while(1)
        {
          if (count == 0)
          {
            p = write(sockfd,client,strlen(client));
            if (p < 0)
                error("ERROR writing to socket");
            bzero(temp_buff,sizeof(temp_buff));
            n = read(sockfd,temp_buff,99);
            if (n < 0)
                error("ERROR reading from socket");
            count++;
          }
          else
          {
            printf("%s: ",client);
            bzero(buff,256);
            fgets(buff,255,stdin);
            n = write(sockfd,buff,strlen(buff));
            if (n < 0)
                error("ERROR writing to socket");
            if (buff[0] == 'q')
            {
                close(sockfd);
                break;
            }
    
            n = read(sockfd,buff,255);
            if (n < 0)
                error("ERROR reading from socket");
            printf("%s:%s",temp_buff,buff);
        }
           }    
        return 0;
    }

Wednesday, 17 February 2016

ChatBox using socket programming in c


SERVER CODE

Save It With server.c


/*
*   Usage :  ./server <SERVER IP ID> <PORT NU.> 
*       Use port number between 5000 to 65535
*/

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

main(int argc,char **argv)
{

    int sfd, nfd, portno;
    char a[25];
    char reply[100];
    struct sockaddr_in v,v1;
    int  n;
    int size=0;
    FILE *in=NULL;
    char server[256],client[256];

    in=popen("id -n -u", "r");
    fgets(server,100, in);
    strtok(server, "\n");
    //printf("%s",server);

    //socklen_t n;

    // First call to socket() function /
    sfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sfd<0)
    {
        perror("socket");
    }

    v.sin_family = AF_INET;
    v.sin_addr.s_addr =inet_addr(atoi(argv[1]));   /////server ip add
    v.sin_port = htons(atoi(argv[2])); ///// port num

    bind(sfd, (struct sockaddr *) &v, sizeof(v));
    perror("bind");   

    listen(sfd,1);
    perror("listen");   

    n=sizeof(v1);
    printf("n=%d\n",n);
    int cnt = 0;
    nfd = accept(sfd, (struct sockaddr *) &v1, &n);
    perror("accept");   

    while(1)
    {
        if(cnt==0)      //if condition transfer user name on client side.
        {   
            read(nfd,a,sizeof(a));
            strcpy(client,a);
            bzero(a,sizeof(a));
            cnt++;
            send(nfd,server,strlen(server)+1,0);
        }
        else            // else condition use for normal cheting.
        {
            bzero(a,sizeof(a));
            recv(nfd,a,sizeof(a),0);
            if(a[0]=='q')
            {
                close(nfd);
                break;
            }

        
            printf("%s-->%s",client,a);
                sleep(1);
            printf("%s:",server);
            bzero(reply,sizeof(reply));
            fgets(reply,sizeof(reply),stdin);
            send(nfd,reply,strlen(reply)+1,0);
        }
        
    }
}



CLIENT CODE

Save It With client.c

/*
*   Usage :  ./client <SERVER IP ID> <PORT NU.> 
*       Use port number between 5000 to 65535
*/


#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
main(int argc,char **argv)
{

    int sfd, nfd, portno, clilen;
    char a[25],b[25];
    struct sockaddr_in v;
    int  n;
    int size=0;
    FILE *in=NULL;
    char client[256],server[256];

    in=popen("id -n -u", "r");
    fgets(client,100, in);
    strtok(client, "\n");

    // First call to socket() function /
    sfd = socket(AF_INET, SOCK_STREAM, 0);

    v.sin_family = AF_INET;
    v.sin_addr.s_addr =inet_addr(atoi(argv[1]));
    v.sin_port = htons(atoi(argv[2]));
    bind(sfd, (struct sockaddr *) &v,  sizeof(v));

    n=sizeof(v);
    printf("n=%d\n",n);
    int cnt =0;
    connect (sfd, (struct sockaddr *)&v , n);
    perror("connect");

    while(1)
    {   
        if(cnt == 0)            //if condition transfer user name on client side.
        {   
            write (sfd,client,strlen(client)+1);
            bzero(b,sizeof(b));
            cnt++;
            sleep(1);
            recv(sfd,b,sizeof(b),0);
            strcpy(server,b);
        }
        else                // else condition use for normal cheting.
        {
            printf("%s>>",client);
            fgets(a,sizeof(a),stdin);

            if(a[0]=='q')
            {
                close(sfd);
                break;
            }
        
            write (sfd,a,sizeof(a));
            sleep(1);
            bzero(b,sizeof(b));
            read(sfd,b,sizeof(b));
            printf("%s:%s",server,b);
        }
    }
    
}



download Programming Embedded Systems: With C and GNU Development Tools, 2nd Edition pdf


If you have programming experience and a familiarity with C--the dominant language in embedded systems--Programming Embedded Systems, Second Edition is exactly what you need to get started with embedded software. This software is ubiquitous, hidden away inside our watches, DVD players, mobile phones, anti-lock brakes, and even a few toasters. The military uses embedded software to guide missiles, detect enemy aircraft, and pilot UAVs. Communication satellites, deep-space probes, and many medical instruments would have been nearly impossible to create without embedded software.

Whether you are new to embedded systems or have done embedded work before, you'll benefit from the topics in this book, which include:
  • How building and loading programs differ from desktop or servercomputers
  • Basic debugging techniques--a critical skill when working withminimally endowed embedded systems
  • Handling different types of memory
  • Interrupts, and the monitoring and control of on-chip and externalperipherals
  • Determining whether you have real-time requirements, and whetheryour operating system and application can meet those requirements
  • Task synchronization with real-time operating systems and embeddedLinux
  • Optimizing embedded software for size, speed, and power consumption
  • Working examples for eCos and embedded Linux





Press Download Button ----->     Download Now


Embedded Systems Building Blocks 2nd Edition pdf







Press Download Button ----->     Download Now