Problem in file transfer using sockets


 
Thread Tools Search this Thread
Top Forums Programming Problem in file transfer using sockets
# 1  
Old 09-25-2004
Data 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.
# 2  
Old 03-23-2006
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
# 3  
Old 01-05-2009
try this:
---Transmit Program

while((len = fread(buffer,sizeof(buffer),1, in)) > 0)
{
cout.flush();
send(s,buffer,len,0);
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Problem using scp to transfer a file

I am testing the following command to transfer a file from my server (AIX 5.2) to another server. I was able to generate the keys and sent them the public key. scp -v -P 4030 /home/lawson/.ssh/jimtest.txt someuser@some.ftpsite.net:/Inbound/jimtest.txt > jimtest_out.txt 2>&1 Based on... (3 Replies)
Discussion started by: jyoung
3 Replies

2. Programming

help: problem with sockets write/read

I am trying to make a server and client, the client will choose between some options and the server will react accordingly. After a some reads and writes that work the server needs to read from client an INT i use this: read(newSd,&k,sizeof(int));But even if all the other times there was no... (1 Reply)
Discussion started by: theSling
1 Replies

3. Shell Programming and Scripting

Problem in Weekly file Transfer script

I have made a script which transfers some files of the entire week , but the script fails when the next month is started. For e.g; if i run the script on 5th may , but i need to transfer files of its previous week which is from 24th April to 30th april ,the script fails, i have this loop in the... (2 Replies)
Discussion started by: vee_789
2 Replies

4. UNIX for Dummies Questions & Answers

is there any problem with functions and sockets?

hi I am writing a client-server program and the functions I've written on the server are never executed. even something as easy as typing hello does not. is there something to be taken into account to use functions? I appreciate any information (2 Replies)
Discussion started by: oticeip
2 Replies

5. UNIX for Advanced & Expert Users

problem while doing Large file transfer thru Scp and FTP

Hi , I want to transfer one file having 6GB(after compression) which is in .cpk format from one server to other server. I tried scp command as well as FTP and also split the file then transfer the files thru scp command. At last i am facing the data lost and connection lost issue. Generally it... (2 Replies)
Discussion started by: Sumit sarangi
2 Replies

6. Programming

problem with lots of open sockets

I'm programming in C with lots of sockets, (asynchronous handling), and for a while it goes ok, but after a while it starts to hang, or not get complete connections anymore. checking my router, i see that i get nat tables overflow and even my DNS cacher seems to be having serious issues... ... (4 Replies)
Discussion started by: alien999999999
4 Replies

7. Programming

File Transfer over Sockets

Hello there !!!!!!!!! I got some problems trying to transfer a file through sockets. The Server must be in Java and the Client in C++ I came up with this code for the server : BufferedInputStream input; BufferedOutputStream output; public void send_data() throws IOException { ... (7 Replies)
Discussion started by: mcnikolas
7 Replies

8. UNIX for Dummies Questions & Answers

Have problem transfer large file bigger 1GB

Hi folks, I have a big problem.... and need help from your experience/knowledge. I previously install and use FREEBSD 7.0 release on my storage/backup file server, for some reason, I can not transfer any files that is bigger than 1GB. If I transfer it to Freebsd file server, the system... (2 Replies)
Discussion started by: bsdme2
2 Replies

9. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies

10. UNIX for Advanced & Expert Users

Large file transfer problem

Hello Everyone, I can't transfer a large file (~15GB TAR Archive) from one linux machine to another via FTP. I have tried the following: 1) Normal FTP the whole 15GB. This stops when it gets to about 2GB and doesn't go any further. 2) Split the 15GB file into 500MB pieces using the... (1 Reply)
Discussion started by: VVV
1 Replies
Login or Register to Ask a Question