LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > 915086731
User Name
Password

Notices


Rate this Entry

Implementation of file transferring by using sockets c on Linux.

Posted 08-01-2014 at 09:10 PM by 915086731

Code for file sending.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <pthread.h>


#define SOCKET_PORT "50000"
#define TRANSFER_SIZE 2048

unsigned long int count = 0;

void error(const char *msg)
{
    perror(msg);
    exit(0);
}


int main(int argc, char** argv)
{
    if( argc != 3 ){
        printf("argument: [target ip address] [file name to be sent]\n");
        return 1;
    }
    /* Making the client */
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    char buffer[TRANSFER_SIZE];
    portno = atoi(SOCKET_PORT);
    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_addr.s_addr = inet_addr(argv[1]);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    /* Time to send the file */
    FILE *pf;
    unsigned long fsize;

    pf = fopen(argv[2], "rb");
    if (pf == NULL) 
    {
        printf("File not found!\n");
        return 1;
    }
    else 
    {
        printf("Found file %s\n", argv[1]);

        fseek(pf, 0, SEEK_END);
        fsize = ftell(pf);
        rewind(pf);

        printf("File contains %ld bytes!\n", fsize);
        printf("Sending the file now\n");
    }

    while (1) 
    {
        // Read data into buffer.  We may not have enough to fill up buffer, so we
        // store how many bytes were actually read in bytes_read.
        int bytes_read = fread(buffer, 1, sizeof(buffer), pf);
        if (bytes_read == 0) // We're done reading from the file
            break;

        if (bytes_read < 0) 
        {
            error("ERROR reading from file"); 
        }

        // You need a loop for the write, because not all of the data may be written
        // in one call; write will return how many bytes were written. p keeps
        // track of where in the buffer we are, while we decrement bytes_read
        // to keep track of how many bytes are left to write.
        void *p = buffer;
        while (bytes_read > 0) 
        {
            int bytes_written = write(sockfd, buffer, bytes_read);
            count += bytes_written;
            printf("%d on stage, sending %d  | Total sended %lu \n",bytes_read, bytes_written, count); //debug
            if (bytes_written <= 0) 
            {
                error("ERROR writing to socket\n");
            }
            bytes_read -= bytes_written;
            p += bytes_written;
        }
    }       

    printf("Done Sending the File!\n");
    printf("Now Closing Connection.\n");

    fclose(pf);
    shutdown(sockfd, SHUT_WR);
    //close(sockfd);
    return 0;
}
Code for file receiver.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <pthread.h>

#define SOCKET_PORT 50000
#define TRANSFER_SIZE 2048

unsigned long int count = 0;

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

void* client_thread_proc(void* arg)
{
    count = 0;
    char buffer[TRANSFER_SIZE];
    char filename[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    FILE *fp;

    int thisfd = (int)arg;
    printf("Server %d: accepted = %d\n", getpid(), thisfd);

    if (thisfd < 0) 
    { 
        printf("Accept error on server\n");
        error("ERROR on accept"); 
        return NULL;
    }

    printf("Connection %d accepted\n", thisfd);
    printf("intput your file name:\n");
    bzero(filename, sizeof(filename));
    scanf("%s", filename);
    fp = fopen(filename, "w+");
    if (fp == NULL) 
    {
        printf("File creat error!\n");
        return NULL;
    }

    /* Time to Receive the File */
    while (1)
    {
        bzero(buffer, sizeof(buffer));
        n = read(thisfd, buffer, sizeof(buffer) );
        count += n;
        printf("%d received | Total received %lu \n", n, count); //debug
        if (n == 0) {
            fclose(fp);
            close(thisfd);
            printf("Reading finished\n");
            return 0;
        }
        if (n > 0) {
            n = fwrite(buffer, 1, n, fp);
        }
        if (n < 0) {
            fclose(fp);
            error("ERROR writing in file");
        }

    } /* end child while loop */
}

void serve_it(int Client)
{
    void* arg = (void*)Client;
    pthread_t new_thread;
    pthread_create( &new_thread, NULL, &client_thread_proc, arg);
}

/* Making Server */
int main()
{
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[TRANSFER_SIZE];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    FILE *fp;

    signal (SIGCHLD, SIG_IGN);

    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_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(SOCKET_PORT);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
          error("ERROR on binding");

    listen(sockfd,5);

    clilen = sizeof(cli_addr);

    while (1)
    {
        printf("Server %d accepting connections\n", getpid());

        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

        serve_it(newsockfd);
    }  // serving loop

    close(sockfd);   
    return 0; 
}
Makefile.
Code:
all:
	gcc myrecv.c  -o myrecv -lpthread
	gcc mysend.c  -o mysend

clean:
	rm mysend myrecv
Posted in Uncategorized
Views 1672 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration