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.
Sending a file through socket programming
Started by saviour2009, Mar 09 2010 06:17 AM
3 replies to this topic
#1
Posted 09 March 2010 - 06:17 AM
|
|
|
#2
Posted 09 March 2010 - 08:13 AM
It shouldn't be significantly different.
#3
Posted 09 March 2010 - 08:54 AM
Heres simple way of doing it.
C / Pseudo code, should be easy to figure out.
Maybe the above code gives some idea about basic packet layout.
Hope this helps.
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
Posted 10 March 2010 - 03:15 AM
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:
Sever side:
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.
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


Sign In
Create Account

Back to top









