LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   UDP file transfer program c (http://www.linuxquestions.org/questions/programming-9/udp-file-transfer-program-c-4175437277/)

littlechocho 11-15-2012 09:37 AM

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

dwhitney67 11-16-2012 05:21 AM

Quote:

Originally Posted by littlechocho (Post 4830082)
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;
}



All times are GMT -5. The time now is 07:40 AM.