LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-28-2011, 10:41 AM   #1
poeyice
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Rep: Reputation: 0
UDP File transfer Error


I have an assigment to create UDP file transfer with checksum,but currently i have a problem in transferring the data. The problem is:
A.I sent a text file that have approximately 200kb but in the client receive only 100kb... is it fair?
B.For text file,I track the result and i found that its okay for the text file. Write counter show 1024 byte every packet i sent. But when i sent mp3 file or pdf file it doenst show like this. For mp3 the data sent every packet i sent is gradually decreased from 1024 byte 1020byte contiounusly.... For pdf data its seems random. what happen?
thx

Last edited by poeyice; 04-30-2011 at 01:32 AM.
 
Old 04-28-2011, 01:06 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Welcome to LQ!

As for the issues you are having with your application, it would be helpful if 1) you indicate the programming language you are using, and 2) show the code that you have developed.
 
Old 04-30-2011, 01:29 AM   #3
poeyice
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Original Poster
Rep: Reputation: 0
heheh tq for welcoming.. not even a years i tried linux as my OS.... newbie here.. hehehe

i am using C langguange with GNU C compiler standard for linux i think..

here's the code..

client.c
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <netinet/in.h>

#define MAXBUF 1024
int main(int argc, char* argv[])
{
int udpSocket,l;
int returnStatus;
int addrlen;
int counter;
int fd;
l=0;
struct sockaddr_in udpClient, udpServer;
char buf[MAXBUF];
if (argc < 4)
	{
	fprintf(stderr, "Usage: %s <ip address> <port> <filename> [dest filename]\n", argv[0]);
	return(0);
	}
/* create a socket */
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udpSocket == -1)
	{
	fprintf(stderr, "Could not create a socket!\n");
	return(0);
	}
else 	{
	printf("Socket created.\n");
	}
/* client address */
/* use INADDR_ANY to use all local addresses */
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = 0;
returnStatus = bind(udpSocket, (struct sockaddr*)&udpClient,
sizeof(udpClient));
if (returnStatus == 0) 
	{
	fprintf(stderr, "Bind completed!\n");
	}
else	{	
	fprintf(stderr, "Could not bind to address!\n");
	close(udpSocket);
return(0);
}
/* set up the message to be sent to the server */
//strcpy(buf, argv[3]);
/* server address */
/* use the command-line arguments */
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr(argv[1]);
udpServer.sin_port = htons(atoi(argv[2]));
returnStatus = sendto(udpSocket, argv[3], strlen(argv[3])+1, 0,
(struct sockaddr*)&udpServer, sizeof(udpServer));

addrlen = sizeof(udpServer);


if (returnStatus == -1)
{
fprintf(stderr, "Could not send filename to server!\n");
return(0);
}
/* call shutdown to set our socket to read-only */
shutdown(udpSocket, SHUT_WR);
/* open up a handle to our destination file to receive the contents */
/* from the server
*/
fd = open(argv[3], O_WRONLY |  O_APPEND);
if (fd == -1)
{
fprintf(stderr, "Could not open destination file, using stdout.\n");
fd = 1;
}

/* read the file from the socket as long as there is data */
while ((counter = recvfrom(udpSocket, buf, MAXBUF, 0,(struct sockaddr*)&udpServer, &addrlen)) > 0)
{
if(l<300){
fprintf(stderr,"|%d : %x|",l,buf[l]);
l++;}
write(fd, buf, counter);
}
if (counter == -1)
{
fprintf(stderr, "Could not read file from socket!\n");
return(0);
}
close(udpSocket);
return 0;
}
server.c
Code:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#define MAXBUF 1024
int main(int argc, char* argv[])
{
int udpSocket,socket2,k;
int returnStatus = 0;
int addrlen,addrlen2 = 0;
struct sockaddr_in udpServer, udpClient;
char buf[MAXBUF];
/* check for the right number of arguments */
if (argc < 2)
	{
	fprintf(stderr, "Usage: %s <port>\n", argv[0]);
	return(0);
	}
/* create a socket */
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udpSocket == -1)
	{
	fprintf(stderr, "Could not create a socket!\n");
	return(0);
	}
else 	{
	printf("Socket created.\n");
	}
/* set up the server address and port */
/* use INADDR_ANY to bind to all local addresses */
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = htonl(INADDR_ANY);
/* use the port passed as argument */
udpServer.sin_port = htons(atoi(argv[1]));
/* bind to the socket */
returnStatus = bind(udpSocket, (struct sockaddr*)&udpServer,
sizeof(udpServer));
if (returnStatus == 0) 
	{
	fprintf(stderr, "Bind completed!\n");
	}
else 	{
	fprintf(stderr, "Could not bind to address!\n");
	close(udpSocket);
	return(0);
	}
	while (1)
	{int fd;
	int i,readCounter,r, writeCounter;
	char* bufptr;
	char buf[MAXBUF];
	char filename[MAXBUF];
	r=0;
	/* wait for an incoming connection */
	addrlen = sizeof(udpClient);
	/* use accept() to handle incoming connection requests */
	/* and free up the original socket for other requests */
	socket2 = recvfrom(udpSocket,filename,MAXBUF,0, (struct sockaddr*)&udpClient, &addrlen);
	if (socket2 == -1)
		{
		fprintf(stderr, "Could not accept connection!\n");
		return(0);
		}
	else
		{
		printf("filename: %s\n",filename);	
		}
	/* get the filename from the client over the socket */
i = 0;
	/* open the file for reading */
	fd = open(filename, O_RDONLY);
	if (fd == -1)
		{
		fprintf(stderr, "Could not open file for reading!\n");
		close(socket2);
		continue;
		}
	else
	{fprintf(stderr, "opening!\n");
	}
	/* reset the read counter */
	readCounter = 0;
	/* read the file, and send it to the client in chunks of size MAXBUF */
addrlen2=(sizeof(udpServer));	
//check buffer awal
while((readCounter = read(fd,buf,MAXBUF)) > 0)
	{
		writeCounter = 0;
		bufptr = buf;
		while (writeCounter < readCounter)
		{
		if(r<300){ 
		fprintf(stderr, "|%d : %x|<%d %d>",r,buf[r],writeCounter,readCounter);
		r++;}
			readCounter -= writeCounter;
			bufptr += writeCounter;
			writeCounter = sendto(udpSocket, bufptr, strlen(bufptr)+1, 0,(struct sockaddr*)	&udpClient,sizeof(udpClient));
			if (writeCounter == -1)
			{
			fprintf(stderr, "Could not write file to client!\n");
			close(socket2);
			continue;
			}
		}
	}
	fprintf(stderr, "first file sent!\n");
	close(fd);
	close(socket2);
	}
	
close (udpSocket);
return 0;
}
write counter and read counter i am using in question B is the sama variable in the code. Plus i am thinking making a checksum function for it. Anyone can give me an advice how to do it? i don't mind if you give me the code
Attached Files
File Type: txt client.txt (2.2 KB, 2 views)
File Type: txt server.txt (2.7 KB, 4 views)

Last edited by poeyice; 04-30-2011 at 01:34 AM.
 
Old 04-30-2011, 02:14 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
As far as I can see you're using strlen on binary data in case of mp3/pdf. But I might be wrong; didn't look too thoroughly.

Code:
		while (writeCounter < readCounter)
		{
		if(r<300){ 
		fprintf(stderr, "|%d : %x|<%d %d>",r,buf[r],writeCounter,readCounter);
		r++;}
			readCounter -= writeCounter;
			bufptr += writeCounter;
			writeCounter = sendto(udpSocket, bufptr, strlen(bufptr)+1, 0,(struct sockaddr*)	&udpClient,sizeof(udpClient));
			if (writeCounter == -1)
			{
			fprintf(stderr, "Could not write file to client!\n");
			close(socket2);
			continue;
			}
		}
 
Old 05-11-2011, 02:11 AM   #5
poeyice
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Original Poster
Rep: Reputation: 0
wooo... then anyone know how to write other than binary file? sorry to bother you... but anyone can suggest a book for networking programming especially for linux... i am using appress book :linux networking programming... but its so hard to find specific problem like this.. TQ ALL
 
Old 05-11-2011, 05:59 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by poeyice View Post
wooo... then anyone know how to write other than binary file? sorry to bother you... but anyone can suggest a book for networking programming especially for linux... i am using appress book :linux networking programming... but its so hard to find specific problem like this.. TQ ALL
As typical, a lot programmers who wish to learn about socket programming get stuck on unrelated issues. Opening a file for reading, such that you can create a new file with the exact same contents, has nothing to do with socket programming. Try to separate the two notions, and you might get further along with your project.

As for sending the contents of a file over a socket, the most basic of examples is the following (which I have not tested, much less compiled):
Code:
...

FILE*         inFile = fopen("somefile", "r");
unsigned char buf[1024];
size_t        bytesRead = 0;

while ((bytesRead = fread(buf, 1, sizeof(buf), inFile)) > 0)
{
   size_t bytesSent = 0;

   while (bytesSent != bytesRead)
   {
      ssize_t ret = sendto(sockfd, buf + bytesSent, bytesRead - bytesSent, (struct sockaddr*) &dest_addr, sizeof(dest_addr));

      if (ret > 0)
      {
         bytesSent += ret;
      }
      else if (ret < 0)
      {
         perror("Failed to send data through socket!");
         exit(1);
      }
   }
}
If you want to copy to another file on the same system, you would open that other file using fopen(), and then in lieu of using sendto(), you would use fwrite(). Conceptually, the process would remain the same.

Last edited by dwhitney67; 05-11-2011 at 06:03 AM.
 
Old 05-13-2011, 12:51 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
This is worth a read; very popular to learn socket programming in C http://beej.us/guide/bgnet/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TCP / UDP File Transfer in C or C++ mending73 Programming 1 10-27-2010 05:53 PM
How to Log TCP and UDP errors during data transfer? luisk Linux - Networking 4 08-20-2009 07:11 AM
How to optimise file transfer through UDP sockets? montylee Programming 12 07-03-2008 01:31 AM
Rsync error cannot do ssh and transfer file linuxbee1 Linux - Newbie 13 06-23-2008 04:41 PM
1 Mb data transfer using udp protocol swamy suresh Programming 1 05-12-2008 08:40 AM


All times are GMT -5. The time now is 03:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration