testsf_c.c revision 72aa2aa4760b02ec1c22e58a4cd7443721311552
1
2/*
3 * Client for the send_file test program
4 * Syntax: testsf_c <server IP addr> <client_filename> <filename> <file length>
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/file.h>
11#include <netinet/in.h>
12#include <sys/socket.h>
13#include <errno.h>
14#include "test.h"
15#include "usctest.h"
16
17
18#ifndef PATH_MAX
19#define PATH_MAX 4096
20#endif
21
22char *TCID="sendfile";
23int TST_TOTAL=1;
24extern int Tst_count;
25
26main(argc, argv)
27int argc;
28char *argv[];
29
30{
31  struct sockaddr_in sa;
32  int s, fd;
33  char *lp, *sp;
34  int i;
35  int nbyte;
36  char *clnt_fname;
37  char rbuf[PATH_MAX];
38  int flen, nlen;
39  int port;
40
41  /* open socket to server */
42  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
43	tst_resm(TBROK, "socket error = %d\n", errno);
44	exit(1);
45  }
46
47  clnt_fname = argv[3]; /* filename to create/
48
49  /* prepare to copy file from server to local machine */
50  if ((fd = open(clnt_fname, O_CREAT | O_TRUNC | O_WRONLY)) < 0) {
51	tst_resm(TBROK, "file open error = %d\n", errno);
52	close(s);
53	exit(1);
54  }
55
56  lp = argv[5]; /* get file size */
57  flen = strtol(lp,(char **)NULL,10);
58
59
60  /* initialize request info: */
61  rbuf[0] = '\0';
62  sp = &rbuf[0];
63  sp = strcat(sp, argv[5]); /* file size */
64  sp = strcat(sp, "=");
65  sp = strcat(sp, argv[4]); /* requested file */
66
67  tst_resm(TBROK, "sp=%s\n",sp);
68  /* initialize server info to make the connection */
69  sa.sin_family = AF_INET;
70  sa.sin_addr.s_addr = inet_addr(argv[1]);
71  port=atoi(argv[2]);
72  sa.sin_port = htons(port);
73
74  if ( connect(s, (struct sockaddr*) &sa, sizeof(sa) ) < 0 ) {
75        tst_resm(TBROK, "connect error = %d\n", errno);
76	close(s);
77	exit(1);
78  }
79
80  /* send request info to server */
81  if ((nbyte = write(s, rbuf, strlen(rbuf))) <= 0) {
82        tst_resm(TBROK, "socket write  error = %d\n", errno);
83	close(s);
84	exit(1);
85  }
86
87tst_resm(TBROK, "client write %d bytes to server with contents %s\n", nbyte, rbuf);
88
89  nlen = 0; /* init size of info received */
90  rbuf[0] = '\0';
91  while ((nbyte = read(s, rbuf, PATH_MAX)) >0) { /* receive info until EOF */
92    nlen += nbyte;
93    if (write(fd, rbuf, nbyte) != nbyte) {
94      tst_resm(TBROK, "Error writing to file %s on client\n",clnt_fname);
95      exit(1);
96    }
97  }
98
99
100  if (nlen != flen) { /* compare expected size with current size */
101    tst_resm(TBROK, "WRONG!!! nlen = %d, should be %d\n", nlen, flen);
102    exit (1);
103  }
104  else
105    tst_resm(TINFO, "File %s received\n", argv[4]);
106
107  close(s);
108  close(fd);
109  exit(0);
110}
111