LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 10-27-2010, 04:05 PM   #1
mending73
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Rep: Reputation: 0
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);

Last edited by mending73; 10-27-2010 at 04:55 PM. Reason: easier to read
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-27-2010, 05:53 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by mending73 View Post
...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.
 
2 members found this post helpful.
  


Reply

Tags
socket, tcp, udp


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About TCP / UDP shipon_97 Linux - Newbie 5 06-24-2012 06:22 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
Problem to transfer binary file over TCP socket yhus Linux - Networking 3 02-28-2006 08:41 AM
tcp/udp and c++ Kroenecker Programming 1 05-10-2005 11:56 AM


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

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