How to transfer images from client and server using TCP/IP sockets.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • venkat28vk
    New Member
    • Feb 2015
    • 1

    How to transfer images from client and server using TCP/IP sockets.

    Hi,

    I have developed the code and i am putting the code for the reference, kindly skim through the code and say your changes.
    Code:
    server.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <fcntl.h>
    
    void set_socket(struct sockaddr_in *socket, int type, int host_short, int addr_type)
    {
        socket -> sin_family = type;
        socket -> sin_port = htons(host_short);
        socket -> sin_addr.s_addr = htonl(addr_type);
    }
    
    void serialize(char *buffer, int count, char *data)
    {
        int i=0, j=0;
        char temp1[20];
    
        sprintf(temp1, "%d", count);
        while(temp1[i] != '\0')
        {
            buffer[j++] = temp1[i++];
        }
        buffer[j++]=' ';
    
        for(i=0; data[i] != '\0'; i++)
        {
            buffer[j++] = data[i];
        }
        buffer[j] = '\0';
        printf("BUFFER =%ld\n", sizeof(buffer));
    }
    
    int main()
    {
        int sid = 0, bid = 0, fp;
        char *send_data = (char *)malloc(1024*sizeof(char));
        char temp[1024];
        char *receive_data = (char *)malloc(1024*sizeof(char));
        int fd, count, cnt=0;
        struct sockaddr_in server_socket, client_socket;
        int size = sizeof(client_socket);
    
        if((sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        {
            printf("Connection error..\n");
            exit(1);
        }
    
        set_socket(&server_socket, AF_INET, 6000, INADDR_ANY);
    
        if((bid = bind(sid, (struct sockaddr *)&server_socket, sizeof(struct sockaddr))) == -1)
        {
            printf("Binding error..\n");
            exit(1);
        }
    
        printf("I am waiting for client..\n");
    
        recvfrom(sid, receive_data, 1024, 0,(struct sockaddr *)&client_socket, &size);
        printf("received data is : %s\n", receive_data);
        fd = open(receive_data, O_RDONLY);
        printf("size = %ld\n", sizeof(send_data));
    
        while((count=read(fd, temp, 500)) != 0)
        {
            printf("I am inside the loop : %d\n", cnt++);
            serialize(send_data, count, temp);
            printf("Serialized : %s\n", send_data);
            sendto(sid, send_data, 1024, 0, (struct sockaddr *)&client_socket, size);
        }
        printf("I am outside the loop : %d\n", count);
        strcpy(temp, "ENDOFFILE");
        serialize(send_data, sizeof(temp), temp);
        sendto(sid, send_data, 1024, 0, (struct sockaddr *)&client_socket, size);
    
        fcloseall();
        close(sid);
        close(fd);
        return 0;
    }


    And also i am putting the client code

    Code:
    client.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <fcntl.h>
    #include <stddef.h>
    
    
    void set_socket(struct sockaddr_in *socket, int type, int host_short)
    {
        socket -> sin_family = type;
        socket -> sin_port = htons(host_short);
    }
    
    void deserialize(char *buffer, int *size, char *data)
    {
        int i=0, j=0;
        char temp1[20];
    
        while(buffer[i] != ' ')
        {
            temp1[j++] = buffer[i++];
        }
        temp1[j] = '\0';
        printf("\nINT : %s\n", temp1);
        *size = atoi(temp1);
    
        i++;
        j=0;
    
        while(buffer[i] != '\0')
        {
            data[j++] = buffer[i++];
        }
        data[j++] = '\0';
    }
    
    int main()
    {
        int sid = 0, bid = 0, con = 0;
        char *send_data = (char *)malloc(1024*sizeof(char));
        char *receive_data = (char *)malloc(1024*sizeof(char));
        char *temp = (char *)malloc(1024*sizeof(char));
        struct hostent *host;
        struct sockaddr_in server_socket;
        int size = sizeof(server_socket);
    
        if((sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        {
            printf("Connection error at client side..\n");
            exit(1);
        }
    
        set_socket(&server_socket, AF_INET, 6000);
    
        if (inet_aton("127.0.0.1", &server_socket.sin_addr)==0) 
        {
            fprintf(stderr, "inet_aton() failed\n");
            exit(1);
        }
    
        printf("Enter the name of the file you want to see : ");
        scanf("%s", send_data);
        int fd = open("Cameo.mp3", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
        sendto(sid, send_data, 1024, 0, (struct sockaddr *)&server_socket, size);
        printf("================= Contents of the File =====================\n");
        while(1)
        {
            int size;
            recvfrom(sid, temp, 1024, 0, (struct sockaddr *)&server_socket, &size);
            printf("Deserialize it : %s\n",temp);
            deserialize(temp, &size, receive_data);
            if(!strcmp(receive_data, "ENDOFFILE"))
            {
                printf("============================================================\n");
                break;
            }
            else
                write(fd, receive_data, size);
        }
        fcloseall();
        close(sid);
    
        return 0;
    }
Working...