LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   TCP / UDP File Transfer in C or C++ (https://www.linuxquestions.org/questions/programming-9/tcp-udp-file-transfer-in-c-or-c-840828/)

mending73 10-27-2010 04:05 PM

TCP / UDP File Transfer in C or C++
 
I need to create a program that has both a server and a client and can transmit PDF and text files between the two using UDP as the underlying carrier while behaving as if it is TCP. We have successfully created a UDP program that can simply transmit strings, but we are not really sure on how to transform this code to do files. We know that we need to use a buffer of some sort to transmit the file in pieces. If anyone can provide us with some help to figure out this program it would be greatly appreciated.


Code:

//dg_cli.c
#include "unp.h"
#include "sys/socket.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "arpa/inet.h"
#include "netdb.h"
#include "sys/file.h"
#include "sys/types.h"

void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, so
{
        int n;
        char sendline[MAXLINE], recvline[MAXLINE + 1];

        while (fgets(sendline, MAXLINE, fp) != NULL) {

        sendto(sockfd, sendline, strlen(sendline), 0, pse

        n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL,

        recvline[n] = 0;        /* null terminate */
        fputs(recvline, stdout);
        }
}

Code:

//dg_echo.c
#include "unp.h"
#include "sys/socket.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "arpa/inet.h"
#include "netdb.h"
#include "sys/file.h"

void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
{
        int                    n;
        socklen_t      len;
        char            mesg[MAXLINE];

        for ( ; ; ) {
                len = clilen;
                n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);

                sendto(sockfd, mesg, n, 0, pcliaddr, len);
        }
}

Code:

//udpcli01.c
#include "unp.h"
#include "sys/socket.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "arpa/inet.h"
#include "netdb.h"

int
main(int argc, char **argv)
{
  int sockfd;
  struct sockaddr_in    servaddr;

  if (argc != 2)
  {
    fputs("usage: udpcli <IPaddress>", stderr);
    exit(0);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(SERV_PORT);
  inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));

  exit(0);
}

Code:

//udpserv01.c
#include "unp.h"
#include "sys/socket.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "arpa/inet.h"
#include "netdb.h"

int
main(int argc, char **argv)
{
  int                                  sockfd;
  struct sockaddr_in  servaddr, cliaddr;

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family      = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port        = htons(SERV_PORT);

  bind(sockfd, (SA *) &servaddr, sizeof(servaddr));

  dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
}

Code:

//unp.h
#include "sys/socket.h"
#include <stdio.h>

#define MAXLINE 4096
/* max text line length */

#define SERV_PORT 9877
#define SA struct sockaddr

void dg_cli(FILE *, int, const SA *, socklen_t);
void dg_echo(int, SA *, socklen_t);


JohnGraham 10-27-2010 05:53 PM

Quote:

Originally Posted by mending73 (Post 4141432)
...using UDP as the underlying carrier while behaving as if it is TCP.

We know that we need to use a buffer of some sort to transmit the file in pieces.

Basically, what would turn UDP into a TCP-like protocol would be making sure that all the data gets there, and in the correct order. If some of the data doesn't get there (e.g. someone pulls a network cable) you should know that it's failed. Duplicate packets should be discarded and dropped packets should be sent again (or, in a simpler model, the receiver can simply take that as a failure - at least it knows it's failed).

So, it sounds like the bare minimum you need to do is this:
  1. Establish a session. Before you start sending any actual data, send a packet saying how much data you're going to transmit in total, and any other information the receiving end might need.
  2. For each packet, give some sort of indication of what part of the overall data it contains. At a bare minimum, a fixed-width (e.g. 2 byte) integer at the start of the packet to denote the sequence number of the packet will do - the first has 0, the second has 1 and so on. This way, the receiving end knows if packets are duplicated/missing/out of order.

The above two points are fairly essential. Additionally, you might want to have some way for the receiving end to request retransmission of some/all of the packets (the other option would be to just fail if any of the packets are missing). Also, acknowledgement that the receiving end is prepared to receive the data (by sending an ack to the initial packet and/or each incoming packet) can be given.


All times are GMT -5. The time now is 03:02 PM.