#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#define TP_SIZE 188
#define TP_PER_DATAGRAM 7
#define DATAGRAM_SIZE (TP_SIZE * TP_PER_DATAGRAM)
#ifdef COUNTER
#define OFFSET 4
#else
#define OFFSET 0
#endif
void usage ()
{
printf ("usage: server \"ip address\" port bitrate filename\n");
printf (" bitrate is expressed in bit/sec (must be > 12000)\n");
}
struct timeval addtime (struct timeval time, int microseconds)
{
if (time.tv_usec+microseconds > 999999)
{
time.tv_sec++;
}
time.tv_usec = (time.tv_usec+microseconds) % 1000000;
return time;
}
unsigned int difftime1 (struct timeval time1, struct timeval time2)
{
return ((time1.tv_sec - time2.tv_sec) * 1000000 +
(time1.tv_usec - time2.tv_usec));
}
/* Tells if time1 is before time2 */
int after (struct timeval time1, struct timeval time2)
{
if (time1.tv_sec> time2.tv_sec)
{
return 0;
}
if ((time1.tv_sec == time2.tv_sec) &&
(time1.tv_usec >= time2.tv_usec))
{
return 0;
}
return 1;
}
int main(int argc, char * argv[])
{
int Sock, result, nBytes, tAddr_len, i;
char destination[128];
char filename[1024];
char tp_buffer[DATAGRAM_SIZE
#ifdef COUNTER
+ 4
#endif
];
struct sockaddr_in tAddr;
struct hostent *server_host;
int typeShutDown, ShutDown = 0, shutsock;
FILE *fp;
int bitrate;
int delay;
int port;
struct timeval timeval, time_now, time_start, time_end;
struct timeval abstime_start;
int alive = 1;
int loop = 0, data_sent = 0, absdata_sent = 0;
#ifdef COUNTER
unsigned int counter = 0;
#endif
int loop1 = 0;
if (argc != 5)
{
usage ();
exit (-1);
}
strcpy (destination, argv[1]);
port = atoi(argv[2]);
bitrate = atoi(argv[3]);
strcpy (filename, argv[4]);
if (bitrate < 12000)
{
usage ();
exit (-1);
}
delay = (int) ((double) ((double) DATAGRAM_SIZE * (double) 8000000) / (double) bitrate);
printf ("bitrate is %d, delay is %d usec\n",
bitrate,
delay);
fp = fopen (filename, "r");
if (fp == NULL)
{
fprintf (stderr, "Error unable to open file %s", filename);
perror ("");
exit (-1);
}
if((Sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("Error during creation of socket");
exit(-1);
}
memset(&tAddr, 0, sizeof(tAddr));
tAddr.sin_family = AF_INET;
tAddr.sin_addr.s_addr = inet_addr(destination);
tAddr.sin_port = htons(port);
tAddr_len = sizeof(tAddr);
result = connect(Sock, (void *)&tAddr, sizeof(tAddr));
if (result == -1)
{
printf("Error during connection of socket: %d\n", result);
exit(-1);
}
nBytes = fread (&tp_buffer[OFFSET], 1, DATAGRAM_SIZE, fp);
if (nBytes < DATAGRAM_SIZE)
{
/* End of file reached: rewind */
rewind (fp);
fprintf (stderr, "file too short\n");
}
#ifdef COUNTER
memcpy (&tp_buffer[0], counter);
counter++;
#endif
gettimeofday (&timeval, NULL);
time_start = timeval;
abstime_start =timeval;
if ((result = send(Sock, tp_buffer, nBytes, 0)) == -1)
{
printf("Error to send\n");
exit (-1);
}
data_sent += DATAGRAM_SIZE
#ifdef COUNTER
+ 4;
#else
;
#endif
absdata_sent += data_sent;
while (alive)
{
loop1++;
nBytes = fread (&tp_buffer[OFFSET], 1, DATAGRAM_SIZE, fp);
if (nBytes < DATAGRAM_SIZE)
{
/* End of file reached: rewind */
rewind (fp);
gettimeofday(&time_now, NULL);
printf ("Loop %d: bandwidth %f (%f)\n",
loop,
(double) (data_sent * (double) 8000000) /
(double) difftime1 (time_now, time_start),
(double) (absdata_sent * (double) 8000000) /
(double) difftime1 (time_now, abstime_start));
time_start = time_now;
loop++;
data_sent = 0;
continue;
}
if ((loop1 % 1000) == 0)
{
int retval;
struct timeval tv;
fd_set rfds;
tv.tv_sec = 0;
tv.tv_usec = 1;
gettimeofday(&time_now, NULL);
printf ("Loop %d: bandwidth %f (%f)\n",
loop1,
(double) (data_sent * (double) 8000000) /
(double) difftime1 (time_now, time_start),
(double) (absdata_sent * (double) 8000000) /
(double) difftime1 (time_now, abstime_start));
time_start = time_now;
data_sent = 0;
#if 1
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
retval = select(1, &rfds, NULL, NULL, &tv);
if (retval)
{
alive = 0;
printf ("Break!!\n");
break;
}
#endif
}
#ifdef COUNTER
memcpy (&tp_buffer[0], counter);
counter++;
#endif
timeval = addtime(timeval, delay);
do
{
gettimeofday(&time_now, NULL);
} while (!after(timeval, time_now));
if ((result = send(Sock, tp_buffer, nBytes, 0)) == -1)
{
printf("Error to send\n");
exit (-1);
}
data_sent += DATAGRAM_SIZE
#ifdef COUNTER
+ 4;
#else
;
#endif
absdata_sent += DATAGRAM_SIZE;
}
close(Sock);
return 0;
}