Closed Thread
Results 1 to 4 of 4

Thread: Sending a file through socket programming

  1. #1
    saviour2009 is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Sending a file through socket programming

    Hello Friends,

    I am a newbie to socket prog. I was able to send some text using socket programming from a clent to a server. Now I wanted to know about sending a file to the server. So please help me and how should I proceed. It would be very helpful if I get a pseudocode or some sample code based on that.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Sending a file through socket programming

    It shouldn't be significantly different.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    julmuri's Avatar
    julmuri is offline Programmer
    Join Date
    Dec 2008
    Location
    127.0.0.1
    Posts
    139
    Rep Power
    12

    Re: Sending a file through socket programming

    Heres simple way of doing it.
    C / Pseudo code, should be easy to figure out.

    Code:
    struct Packet
    {
    public:
        typedef unsigned short    Id;
        typedef unsigned long    Size;
    
    public:
        Id        id;
        Size    size;
        void*    data;
    
    }; // struct Packet
    
    
    enum PacketId
    {
        PacketIdFile = 0
        // ...
    }; // enum PacketId
    
    
    int getPacketHeaderSize()
    {
        return sizeof( Packet::Id )  +
               sizeof( Packet::Size );
    }
    
    int getPacketSize( Packet* packet )
    {
        assert( 0 != packet );
        return sizeof( packet->id )  +
               sizeof( packet->size )+
               packet->size;
    }
    int main( int argc, char* argv[] )
    {
        /*    file sender
    
            you need to figure what file api you are going to use
        */
        Packet packet = {0};
    
        packet.id   = PacketIdFile;
        packet.size = getFileSize( file );
        packet.data = malloc( packet.size );
        if ( 0 == packet.data )
            return 0;
    
        readFile( file, packet.data, packet.size );
        // error check
        // ...
    
        send( socket, &packet, getPacketSize( &packet ), 0 );
        // error check
        // ...
    
    
        /*    file receiver
        */
    
        Packet packet = {0};
    
        recv( socket, &packet, getPacketHeaderSize(), 0 );
        // error check
        // ...
    
        if ( 0 < packet.size )
        {
            packet.data = malloc( packet.size );
            if ( 0 == packet.data )
                return 0;
    
            recv( socket, packet.data, packet.size, 0 );
            // error check
            // ...
        } // if
    
        switch ( packet->id )
        {
        case PacketIdFile:
        {
            // file packet received
            // ...
        } // case
    
        default:
        {
            // unknown packet
            // ...
        } // default
        } // switch
    
        return 0;
    }
    Like said, its pretty much the same as sending any data over socket.
    Maybe the above code gives some idea about basic packet layout.

    Hope this helps.
    Code:
    std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

  5. #4
    saviour2009 is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Re: Sending a file through socket programming

    thanx for the reply.........But my problem is that i am working on windows platform on visual studio 2008 and its a bit different from doing socket programming in linux. I tried simple data transfer programs that are as given below:

    Client side:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<winsock.h>
    #define PORT 6003
    int main()
    {
    WSADATA WsaDat;
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("WSA Initialization failed.");
               }
    
    
    int sfd,res;
    char buf1[20],buf2[20];
    struct sockaddr_in server;
    while(1)
    {
    sfd=socket(AF_INET,SOCK_STREAM,0);
    if(sfd==-1)
    {
            printf("Socket creation error");
            exit(0);
    }
    
            server.sin_family=AF_INET;
            server.sin_addr.s_addr=inet_addr("127.0.0.1");
            server.sin_port=htons(PORT);
     res=connect(sfd,(struct sockaddr*)&server,sizeof(server));
     if(res==-1)
     {
             printf("connection error");
             closesocket(sfd);
             exit(0);
     }
     printf("\nEntr msg:");
     scanf("&#37;s",&buf1);
     if(strcmp("quit",buf1)==0)
     {
     send(sfd,buf1,20,0);
     closesocket(sfd);
     exit(0);
     }
     send(sfd,buf1,20,0);
     recv(sfd,buf2,20,0);
     printf("\n%s",buf2);
    }
     closesocket(sfd);
     return 0;
    }

    Sever side:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<winsock.h>
    int main()
    {
    WSADATA WsaDat;
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("WSA Initialization failed.");
               }
    
    int sfd,res,k;
    char buf1[20],buf2[20];
    struct sockaddr_in server;
    sfd=socket(AF_INET,SOCK_STREAM,0);
    if(sfd==-1)
    {
            printf("Socket creation error");
            exit(0);
    }
    while(1)
    {
            server.sin_family=AF_INET;
            server.sin_addr.s_addr=inet_addr("127.0.0.1");
            server.sin_port=htons(6003);
            bind(sfd,(struct sockaddr*)&server,sizeof(server));
                int len=sizeof(server);
            listen(sfd,5);
            int ak=accept(sfd,(struct sockaddr*)&server,&len);
            recv(ak,buf1,20,0);
            printf("%s\n",buf1);
            scanf("%s",&buf2);
            if(strcmp("quit",buf1)==0)
            {
                    closesocket(sfd);
                    exit(0);
            }
            send(ak,buf2,20,0);
    }
    closesocket(sfd);
    return 0;
    }

    If we can make some changes in these programs to work for file transfer, then it would be easy for me to understand as I wish to follow the same pattern in this case. Hope u may help.
    Last edited by ZekeDragon; 03-12-2010 at 03:57 PM. Reason: If you see this again, remember to use [code] tags

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. socket programming
    By kenna in forum C and C++
    Replies: 2
    Last Post: 07-12-2010, 06:57 PM
  2. Socket Programming
    By Hunter100 in forum C and C++
    Replies: 10
    Last Post: 06-27-2010, 10:01 PM
  3. help with socket programming in c
    By kedah160 in forum C and C++
    Replies: 16
    Last Post: 03-23-2010, 04:51 PM
  4. Socket Programming in C
    By saviour2009 in forum C and C++
    Replies: 4
    Last Post: 03-08-2010, 02:37 AM
  5. Socket Programming
    By NeedHelp in forum Managed C++
    Replies: 2
    Last Post: 06-19-2006, 12:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts