The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
file transfer using Ftp sudhi Shell Programming and Scripting 1 05-25-2008 08:14 PM
Transfer the file manas_ranjan UNIX for Dummies Questions & Answers 2 07-27-2007 06:33 AM
file transfer bkan77 Shell Programming and Scripting 4 07-27-2007 02:55 AM
Sockets and File descriptors gstlouis High Level Programming 3 12-12-2005 04:36 AM
Large file transfer problem VVV UNIX for Advanced & Expert Users 1 10-22-2005 08:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-24-2004
collins's Avatar
Registered User
 

Join Date: Aug 2004
Posts: 43
Unhappy Problem in file transfer using sockets

Hai Friends

I am writing a c program to transfer files from one system to another using TCP/IP socket programming..

My Recieve Program

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

extern int errno;
extern char *sys_erlist[];

void receiveFile(int fd);

#define QLEN 5

int main(int argc, char *argv[])
{
struct sockaddr_in fsin;
char *service = "9110";
int msock, ssock, alen;
struct hostent *phe;
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type;

bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;

if(pse = (struct servent *)getservbyname(service, protocol))
sin.sin_port= (u_short)pse->s_port;
else if((sin.sin_port = htons((u_short)atoi(service))) == 0)
printf("Can't get \"%s\" service entry\n", service);

if(phe = (struct hostent *)gethostbyname(host))
bcopy(phe->h_addr, (char *)&sin.sin_addr, phe->h_length);
else if((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
printf("Can't get \"%s\" protocol entry.\n", protocol);

if((ppe = (struct protoent *)getprotobyname(protocol)) == 0)
printf("Can't get \"%s\" protocol entry.\n", protocol);

if(strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;

s = socket(PF_INET, type, ppe->p_proto);
if(s < 0)
printf("Cant create socket\n");

if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
printf("Can't connect to %s.%s\n", host, service);

ssock = accept(s, (struct sockaddr *)&sin, &alen);
if(ssock < 0)
printf("Accept Failed.\n");
(void)receiveFile(ssock);
(void)close(ssock);
}

void receiveFile(fd)
int fd;
{
char buf[BUFSIZE];
int BUFSIZE=1024;
int cc;
FILE *fp=fopen("sample","w");
do
{
cc = read(fd, buf, sizeof buf);
fputs(fp,buf);
}while(cc > 0);
fclose(fp);
}

My Transmit Program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
int status=0;
int portno=atoi(argv[1]);
// Create a socket. Do a "man socket" to get details. These are
// pretty common defaults for domain, type, and protocol
int s = socket(PF_INET,SOCK_STREAM,0);

if (s <= 0) {
printf("server: Socket creation failed.");
exit(-1);
}

// Connect to server port
sockaddr_in address;
int addressSize = sizeof(sockaddr_in);
address.sin_family=AF_INET; // using INTERNET V4 domain
address.sin_port = htons(portno);
inet_pton(AF_INET,"127.0.0.1",&address.sin_addr); // xlate to internal format

status = connect(s,(struct sockaddr*) &address, addressSize);

if (status != 0) {
printf("client:Connect operation failed, unable to connect to server port.");
exit(-1);
}
int BUFFER_SIZE=1024;
FILE *in = fopen(argv[2],"r");
int len;
char buffer[1024];
while((len = fread(buffer,1,sizeof(buffer),in)) > 0)
{
cout.flush();
send(s,buffer,BUFFER_SIZE,0);
}
fclose(in);
close(s);
return 0;
}

When i run the program my receive program receives twice every data...

Can anyone please figure me out a client/server program to transmit files from one system to another using sockets.
__________________
__|_|__
( ° ° )
+--oOO--------OOo--+
| Binary Friend |
| Collin-a-Harrys |
+-----------------------+
|__|__|
|| ||
ooO Ooo
Reply With Quote
Forum Sponsor
  #2  
Old 03-23-2006
Registered User
 

Join Date: Mar 2006
Posts: 1
hi i am using your code pls help me

hi collins,

I am using your client/server code for transmitting f ile from one system to another system . I am facing the same problem . Data is being copied twice. Have you resolved that problem or any other solutions please lemme know.

Thank you,
Murali
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0