-1

First of all, I'd like to point out that i'm not a good programmer, so please be patient with me. The logic in the program is as follows: the client sends the server a text file, the server saves it under a different name. In short, very similar to this topic: " File transfer server/client using socket " except that I'm using a different protocol. I think I've managed to succesfully send the file, but it seems like the server, afters creating a blank file, stucks in a loop/doesn't write anything in it.

Here's the code for the client:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> /* memset() */
#include <sys/time.h> /* select() */ 
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>  


#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 100
#define PLIK    "/home/aatami/Documents/programowanie/serwer/plik.txt"
#define LENGTH 512
int main(int argc, char *argv[]) {

  int sd, rc, i,fd;
  unsigned int cliLen;
  struct sockaddr_in cliAddr, remoteServAddr;
  struct hostent *h;
  char  buf[LENGTH];
  /* check command line args */
  if(argc<3) {
    printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
    exit(1);
  }

  /* get server IP address (no check if input is IP address or DNS name */
  h = gethostbyname(argv[1]);
  if(h==NULL) {
    printf("%s: unknown host '%s' \n", argv[0], argv[1]);
    exit(1);
  }

  printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
     inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));

  remoteServAddr.sin_family = h->h_addrtype;
  memcpy((char *) &remoteServAddr.sin_addr.s_addr, 
     h->h_addr_list[0], h->h_length);
  remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);

  /* socket creation */
  sd = socket(AF_INET,SOCK_DGRAM,0);  /* port 0 - system gets the first free one */ 
  if(sd<0) {
    printf("%s: cannot open socket \n",argv[0]);
    exit(1);
  }

  /* bind any port */
  cliAddr.sin_family = AF_INET;
  cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  cliAddr.sin_port = htons(0);

  rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
  if(rc<0) {
    printf("%s: cannot bind port\n", argv[0]);
    exit(1);
  }


  /* send data */
  for(i=2;i<argc;i++) {
    rc = sendto(sd, argv[i], strlen(argv[i])+1, 0, 
        (struct sockaddr *) &remoteServAddr, 
        sizeof(remoteServAddr));

    if(rc<0) {
      printf("%s: cannot send data %d \n",argv[0],i-1);
      close(sd);
      exit(1);
    }
    /* send text file */


        char sdbuf[LENGTH]; 
        printf("[Client] Sending %s to the Server... ", PLIK);
        FILE *fs = fopen(PLIK, "r");
        if(fs == NULL)
        {
            printf("ERROR: File %s not found.\n", PLIK);
            exit(1);
        }

        bzero(sdbuf, LENGTH); 
        int fs_block_sz; 
        while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
        {
            if(send(sd, sdbuf, fs_block_sz, 0) < 0)
            {
                fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", PLIK,           errno);
                break;
            }
            bzero(sdbuf, LENGTH);
        }
        printf("Ok File %s from Client was Sent!\n", PLIK);


  }

  return 1;

}

And the code for the server:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* close() */
#include <string.h> /* memset() */
#include <errno.h>
#define LENGTH 512
#define LOCAL_SERVER_PORT 1500
#define MAX_MSG 100
#define PLIKSERV    "/home/aatami/Documents/programowanie/serwer/plikserv.txt"
int main(int argc, char *argv[]) { /* licznik argumentow, tablica argumentow */

  int sd, rc,nsockfd;
  unsigned int n,cliLen;
  struct sockaddr_in cliAddr, servAddr;
  char msg[MAX_MSG];
   char  buf[512],sbuf[LENGTH];
  /* socket creation */
  sd=socket(AF_INET, SOCK_DGRAM, 0);
  if(sd<0) {
    printf("%s: cannot open socket \n",argv[0]);
    exit(1);
  }

  /* bind local server port */
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(LOCAL_SERVER_PORT);
  rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
  if(rc<0) {
    printf("%s: cannot bind port number %d \n", 
       argv[0], LOCAL_SERVER_PORT);
    exit(1);
  }

  printf("%s: waiting for data on port UDP %u\n", 
       argv[0],LOCAL_SERVER_PORT);



  /* server infinite loop */
  while(1) {

    /* init buffer */
    memset(msg,0x0,MAX_MSG);


    /* receive message */
    cliLen = sizeof(cliAddr);
    n = recvfrom(sd, msg, MAX_MSG, 0, 
         (struct sockaddr *) &cliAddr, &cliLen);

    if(n<0) {
      printf("%s: cannot receive data \n",argv[0]);
      continue;
    }

    /* print received message */
    printf("%s: from %s:UDP%u : %s \n", 
       argv[0],inet_ntoa(cliAddr.sin_addr),
       ntohs(cliAddr.sin_port),msg);

         unsigned int sin_size = sizeof(struct sockaddr_in);




        FILE *fr = fopen(PLIKSERV, "a");
        if(fr == NULL)
            printf("File %s Cannot be opened file on server.\n", PLIKSERV);
        else
        {
            bzero(sbuf, LENGTH); 
            int fr_block_sz = 0;
            while((fr_block_sz = recvfrom(sd, buf, LENGTH, 0,(struct sockaddr *) &cliAddr, &cliLen)) > 0) 
            {
                int write_sz = fwrite(buf, sizeof(char), fr_block_sz, fr);
                if(write_sz < fr_block_sz)
                {
                    perror("File write failed on server.\n");
                }
                bzero(buf, LENGTH);
                if (fr_block_sz == 0 || fr_block_sz != 512) 
                {
                    break;
                }
            }
            if(fr_block_sz < 0)
            {
                if (errno == EAGAIN)
                {
                    printf("recv() timed out.\n");
                }
                else
                {
                    fprintf(stderr, "recv() failed due to errno = %d\n", errno);
                    exit(1);
                    }
                }
            printf("Ok received from client!\n");
            fclose(fr); 
        }

  }/* end of server infinite loop */

return 0;

}

Any help greatly appreciated!

Community
  • 1
  • 1
Cy83rr
  • 11
  • 1
  • 5
  • One thing I see is that in `recvfrom` you are reading into `sbuf` but in `fwrite` you are writing from `buf`. – Duck May 07 '13 at 02:41
  • I've changed it to buf in all cases, but the result is still the same. – Cy83rr May 07 '13 at 05:06
  • This code makes no sense whatsoever. You are writing UDP code as though it was TCP code. For example your server read loop terminates on a zero length packet which is never sent by the client. You are also testing for EAGAIN, which can never occur in blocking mode, which is what you are using. You are making no provision for UDP datagram loss or repetition or out of sequence arrival. Your client is sending `argc-2` packets but your server is receiving exactly one. Your client is using `send()` on an unconnected UDP Socket, which will produce an ENOTCONN error. Throw this away and start again. – user207421 Feb 23 '19 at 19:32

1 Answers1

1

After seen your code I'd suggest try to send a "Hello world\n" from client to the server, see if you get it there. Once you make the connection between client and server then try to expand the code to do a file transfer.

But keep this in mind about UDP:

"UDP uses a simple transmission model with a minimum of protocol mechanism. It has no handshaking dialogues, and thus exposes any unreliability of the underlying network protocol to the user's program. As this is normally IP over unreliable media, there is no guarantee of delivery, ordering or duplicate protection. If error correction facilities are needed at the network interface level, an application may use the Transmission Control Protocol (TCP) which is designed for this purpose."

As you start the client, it sends all packets as fast as possible, could it be that the packets get lost or disordered or even duplicates at the server site. I also suggest waiting for a packet to acknowledge the correct reception. I mean, when then server receives a packet it sends a packet to acknowledge it to the client. This way, a conversation (dialog) could be held and the transfer accomplished satisfactory.

ja_mesa
  • 1,971
  • 1
  • 11
  • 7
  • Actually, i've already rewritten it using TCP, and now i'm just being stubborn to write it with UDP. There is a method here to check if something works implemented already - the server receives a message from the client, which is printed out in the terminal, and it works just fine. If I try sending again and again (the message and the file) the final text file becomes corrupted - instead of being empty, it has some 1s and 0s and /. Is it because of the duplicating/disordering/etc packages? – Cy83rr May 07 '13 at 18:53
  • I'm afraid it could be. There may be 10 cases where the transfer is fine and sometimes not, this is because the duplicates/disordering/missing packets; UDP was not designed for this purpose. Either way you go, UDP or TCP, you need to create a protocol something that indicates you what you're going to receive. – ja_mesa May 07 '13 at 19:01
  • How do I do that for UDP? Is this something feasible for a novice programmer, or would I be better off leaving the transfer with TCP? Many thanks for your advice! – Cy83rr May 07 '13 at 21:21