Jump to content

Sending a file through socket programming

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
saviour2009

saviour2009

    Newbie

  • Members
  • Pip
  • 5 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It shouldn't be significantly different.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
Heres simple way of doing it.
C / Pseudo code, should be easy to figure out.

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.
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;

#4
saviour2009

saviour2009

    Newbie

  • Members
  • Pip
  • 5 posts
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:

#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("%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:

#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.

Edited by ZekeDragon, 12 March 2010 - 03:57 PM.
If you see this again, remember to use [code] tags