LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 11-15-2012, 09:37 AM   #1
littlechocho
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
UDP file transfer program c


I write the udp file transfer but there is some problems.

the 123.txt file has not complete transfer.
are there everybody know?
Code:
//////////////////////////////////////////////////////////////////////////////////////
// file_ha.c  (client)  recvfrom()
//////////////////////////////////////////////////////////////////////////////////////

#include <netinet/in.h>    // for sockaddr_in
#include <sys/types.h>    // for socket
#include <sys/socket.h>    // for socket
#include <stdio.h>        // for printf
#include <stdlib.h>        // for exit
#include <string.h>        // for bzero

/*
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
*/

#define RECV_UDP_PORT  5099
#define RECV_HOST_ADDR "127.0.0.1"
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512

#define MAXMESG 2048
void dg_recv(int sockfd, struct sockaddr *psent_addr, int maxclilen)
{
    char buffer[BUFFER_SIZE];
    bzero(buffer,BUFFER_SIZE);	
    
    char file_name[7];
    bzero(file_name, 7);
    file_name[0] = '1';
    file_name[1] = '2';
    file_name[2] = '3';
    file_name[3] = '.';
    file_name[4] = 't';
    file_name[5] = 'x';
    file_name[6] = 't';
	
    FILE * fp = fopen(file_name,"w");
    if(NULL == fp ){
        printf("File:\t%s Can Not Open To Write\n", file_name);
        exit(1);
    }       
    
    int length = 0;
    //recvfrom(socket_descr?ptor, message, sizeof(message), 0, (struct sockaddr *)&sin, &sin_len);
    //while( length = recvfrom(client_socket,buffer,BUFFER_SIZE,0)){
    printf("start wait\n");
    while(1){
	length = recvfrom(sockfd,buffer,BUFFER_SIZE,0,(struct sockaddr *)&psent_addr, &maxclilen);
        printf("sockfd : %d\n", sockfd);
        printf("length : %d\n", length);
        printf("BUFFER_SIZE : %d\n", BUFFER_SIZE);
        
        if(length < 0){
            printf("Recieve Data From Server Failed!\n");
            //break;
        }
//        int write_length = write(fp, buffer,length);
        int write_length = fwrite(buffer,sizeof(char),length,fp);
        if (write_length<length){
            printf("File:\t%s Write Failed\n", file_name);
            //break;
        }
        bzero(buffer,BUFFER_SIZE);    
    }
    printf("Recieve File:\t %s From Server Finished\n",file_name);  
    close(fp);
}


int main(int argc, char **argv)
{
    //set a socket address struct recv_addr,include(client's address, port)
    struct sockaddr_in recv_addr, sent_addr;
    bzero(&recv_addr,sizeof(recv_addr)); //set 0
    recv_addr.sin_family = AF_INET;    //use IPv4TCP/IP
    recv_addr.sin_addr.s_addr = htons(INADDR_ANY);//INADDR_ANY
    //recv_addr.sin_port = htons(0);    //0 is present use a free port
	recv_addr.sin_port = htons(RECV_UDP_PORT);
	
    //set a UDP socket,use "client_socket" present client socket    
    int client_socket = socket(AF_INET,SOCK_DGRAM,0);
    if( client_socket < 0){
        printf("Create Socket Failed!\n");
        exit(1);
    }

    // bind the client's socket and socket address
    if( bind(client_socket,(struct sockaddr*)&recv_addr,sizeof(recv_addr))){
        printf("Client Bind Port Failed!\n"); 
        exit(1);
    }
    
	dg_recv(client_socket, (struct sockaddr*)&sent_addr, sizeof(sent_addr));
	
    //set a socket address struct server_addr, include(server's address, port)  
    struct sockaddr_in server_addr;
    bzero(&server_addr,sizeof(server_addr));
	socklen_t server_addr_length = sizeof(server_addr);
	    
    //close socket
    close(client_socket);
    return 0;

}
Code:
//////////////////////////////////////////////////////////////////////////////////////
// file_user.c  (server)  sendto()
//////////////////////////////////////////////////////////////////////////////////////

#include <netinet/in.h>    // for sockaddr_in
#include <sys/types.h>    // for socket
#include <sys/socket.h>    // for socket
#include <stdio.h>        // for printf
#include <stdlib.h>        // for exit
#include <string.h>        // for bzero

/*
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
*/

#define RECV_UDP_PORT 5099 
#define RECV_HOST_ADDR "127.0.0.1"
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512

int dg_sent(FILE *fp, int sockfd, struct sockaddr *precv_addr, int recvlen)
{
    char buffer[BUFFER_SIZE];
    bzero(buffer, BUFFER_SIZE);
    int file_block_length = 0;
    int input;
    while((file_block_length = fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0){
        printf("file_block_length = %d\n",file_block_length);        		
        if(sendto(sockfd, buffer, file_block_length, 0, precv_addr, recvlen)<0){
            printf("Send File Failed\n");
			return 0;
        }
        bzero(buffer, BUFFER_SIZE);
        
    }//the code is a loop to read the text and to sent to the client. and this is TCP most receieve is 1024byte 

    //close(fp);
    fclose(fp);    
	return 1;
}

int main(int argc, char **argv)
{
    FILE *infp, *outfp, *fopen();
    //set a socket address struct recv_addr, include(server's address, port)  
    struct sockaddr_in recv_addr;
    bzero(&recv_addr,sizeof(recv_addr)); //set 0
    recv_addr.sin_family = AF_INET;//use IPv4TCP/IP
    recv_addr.sin_addr.s_addr = inet_addr(RECV_HOST_ADDR);
    recv_addr.sin_port = htons(RECV_UDP_PORT);

    //set a UDP socket, server_socket is present server's socket   
    int server_socket = socket(PF_INET,SOCK_DGRAM,0);
    if( server_socket < 0){
        printf("Create Socket Failed!");
        exit(1);
    }   
    
    printf("choose sentifile_name\n");    
	char file_name[7];
    bzero(file_name, 7);
	file_name[0] = '1';
	file_name[1] = '2';
	file_name[2] = '3';
	file_name[3] = '.';
	file_name[4] = 't';
	file_name[5] = 'x';
	file_name[6] = 't';
	infp = fopen(file_name,"r");
	if(NULL == infp ){
            printf("File:\t%s Not Found\n", file_name);
    }
	else{
	    printf("your File is  %s\n", file_name);
		if(dg_sent(infp, server_socket, (struct sockaddr*)&recv_addr, sizeof(recv_addr)) == 1){
		    printf("File:\t%s Transfer Finished\n",file_name);
		}
		close(server_socket);
	}	
	exit(0);
	
    return 0;

}
//////////////////////////////////////////////////////////////////////////////////////
// 123.txt
//////////////////////////////////////////////////////////////////////////////////////
AZ
BBB
CCCC
DDDDD
EEEEEE


...
.
.
.
.
.
.
.





FFFFFF
GGGGG
HHHH
III
JJ
K
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Steve................
A
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv wxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz

Last edited by littlechocho; 11-18-2012 at 02:31 AM.
 
Old 11-16-2012, 05:21 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by littlechocho View Post
I write the udp file transfer but there is some problems.
Please edit your post so that your code appears in CODE tags; this helps preserve any formatting you may have in your code, and more importantly, it makes it easier to read your code.

It would also be helpful if you removed from your code any statements that are commented out.

Lastly, please state exactly what the problem is. If the program is not working as expected, indicate what results you were expecting.

P.S. You can post formatted code using the # symbol that appears on the right-hand-size of the editing tool set above the edit-box. For example:
Code:
#include <stdio.h>

int main(int argc, char** argv)
{
    int foo = 5;

    printf("foo is %d\n", foo);

    return 0;
}
 
  


Reply



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
UDP File transfer Error poeyice Programming 6 05-13-2011 12:51 AM
TCP / UDP File Transfer in C or C++ mending73 Programming 1 10-27-2010 05:53 PM
How to optimise file transfer through UDP sockets? montylee Programming 12 07-03-2008 01:31 AM
Program to transfer file 'backwards' over SSH jantman Linux - Networking 6 10-17-2006 12:10 PM
File transfer program!! vishamr2000 Programming 11 04-27-2006 04:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration