#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/signal.h>
#include <stdio.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "sockbib.h"
#define ROUND 5
#define SERVICE "chargen"
#define MAXTMPLEN 10240 /* max length of tmp array declared below */
#define VERSION "\nSpeed - TCP/UDP Version 1.0.6 - by Ethy Brito 1999/09/14\n"
//char *wheel="/-\\|";
char *wheel=" .o0O0o.";
long charread, arr[ROUND];
int cti;
void catcher()
{
int j;
arr[cti++%ROUND]=charread;
// for(charread=0,j=ROUND; j--;)
// charread+= arr[j];
// charread/=ROUND;
printf("%c %ld.%ld %cbyte(s)/s (%ld Kbits/s) \n\e[A",
wheel[cti%strlen(wheel)],
(charread<1024?charread:charread/1024),
(charread<1024?0:(10*charread)/1024-10*(charread/1024)),
charread<1024?' ':'K',
8*charread/1024);
charread=0;
(void)signal(SIGALRM, catcher);
(void)alarm((u_int)1);
}
$
void main(argc, argv)
char *argv[];
{
CNCSOCK * skt;
char *cmdname,
tmp[MAXTMPLEN],
proto[8];
long i;
int udp=0;
printf("%s\n", VERSION);
/* save the command name (usually called "speed") */
cmdname = argv[0];
/* punt if syntax is wrong */
if (argc == 1) {
fprintf(stderr, "usage: %s [-u] hostname|hostaddress\n", cmdname);
exit(1);
}
if (!strcmp(argv[1], "-u"))
{
strcpy(proto, "udp");
argv++;
udp=1;
}
else
strcpy(proto, "tcp");
printf("%s measuring...\n", proto);
if((skt=InicSocket(argv[1], SERVICE, proto))==NULL)
{
fprintf(stderr, "Error opening socket!\n");
exit(1);
}
putc('\n',stdout);
memset(arr, 0, sizeof(arr));
catcher();
if(udp)
{
// fcntl(skt->sock, F_SETFL, O_NONBLOCK);
send ( skt->sock, "", 0, 0 ); //wake up server!
}
for(; ; )
{
if((i=recv(skt->sock, tmp, MAXTMPLEN, 0))>0)
charread+=i+(udp*33);
if(udp)
send ( skt->sock, "", 0, 0 );
// printf("\n<<<< %ld - %ld>>>>%.40s", charread, i,tmp);
}
return;
}
=============Aqui a sockbib.c==================
sockbib.c
static char rcs_id[]="$Id: sockbib.c,v 1.4 1997/09/09 13:45:24 penna Exp $";
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include "sockbib.h"
/************
As funcoes de interface com o mundo externo sao:
InicSocket() - usada pelos clientes para abrir comunicacao com o server
FimSocket() - usada pelos clientes para fechar comunicacao com o server
MsgToServer() - usada pelos clientes para mandar mensagem para o server
MsgFromServer() - usada pelos clientes para receber mensagem do server
MsgFromClient() - usada pelos servidores, apos seu start pelo inetd
MsgToClient() - usada pelos servidores para responder para o cliente
************/
/******
nomehost=nome do host servidor
nomeserv=nome do servico no /etc/services (ftp, tacacs, finger, ...)
tipo=tcp, udp, icmp, ...
******/
CNCSOCK *InicSocket(nomehost, nomeserv, tipo)
char *nomehost, *nomeserv, *tipo;
{
struct servent *servicep; /* ponteiro de informacoes do servico */
struct hostent *hostp; /* ponteiro de informacoes do hospedeiro */
CNCSOCK *aux;
aux = (CNCSOCK *)malloc(sizeof(CNCSOCK));
if ( aux == (CNCSOCK *)NULL )
return (CNCSOCK *)NULL;
memset(aux, 0x00, sizeof(CNCSOCK));
/* Obtem o IP do servidor */
hostp = gethostbyname(nomehost);
if ( hostp == (struct hostent *)NULL ) {
free(aux);
return (CNCSOCK *)NULL;
}
/* Atribui o tipo de endereco do hospedeiro */
aux->serveraddr_in.sin_family = AF_INET;
if ( strcmp(tipo, "udp") == 0 )
aux->sock_type = SOCK_DGRAM;
else
aux->sock_type = SOCK_STREAM;
/* Atribui o endereco do hospedeiro */
aux->serveraddr_in.sin_addr.s_addr = ((struct in_addr *) (hostp->h_addr))->s_addr;
/* Obtem os dados relativos ao servico prestado pelo servidor */
servicep = getservbyname(nomeserv, tipo);
if ( servicep == (struct servent *)NULL ) {
free(aux);
return (CNCSOCK *)NULL;
}
/* Atribui a porta onde o servico esta sendo prestado */
aux->serveraddr_in.sin_port = (ushort) servicep->s_port;
if ( AbreSocket(aux) < 0 ) {
free(aux);
return (CNCSOCK *)NULL;
}
return aux;
} /* end InicSocket() */
/*************************************************************************/
int AbreSocket(cs)
CNCSOCK *cs;
{
int addrlen;
struct sockaddr_in myaddr_in;
/* Cria um socket para TCP */
cs->sock = socket ( AF_INET, cs->sock_type, 0 );
if ( cs->sock < 0 )
return -1;
/* Estabelece conexao com o servidor para enviar transacoes */
if ( connect ( cs->sock, &cs->serveraddr_in, sizeof( struct sockaddr_in ) ) == -1 ) {
close(cs->sock);
return -1;
}
addrlen = sizeof( struct sockaddr_in );
getsockname( cs->sock, &myaddr_in, &addrlen );
return cs->sock;
} /* end AbreSocket() */
/*************************************************************************/
int MsgToServer (cs, buffer, tam)
CNCSOCK *cs;
char *buffer;
int tam;
{
int tot_env;
if ( cs == (CNCSOCK *)NULL || buffer == (char *)NULL || tam <= 0 )
return -1;
tot_env = send ( cs->sock, buffer, tam, 0 );
if ( tot_env < 0 )
return -1;
return tot_env;
} /* end MsgToServer() */
/**************************************************************************/
int MsgFromServer (cs, buffer, tam, timeout)
CNCSOCK *cs;
char *buffer;
int tam;
int timeout;
{
int lidos, tam_max=tam;
lidos = LeDoServer ( cs, buffer, tam_max, timeout );
if ( lidos < 0 )
return -1;
return lidos;
} /* end MsgFromServer() */
/**************************************************************************/
int LeDoServer( cs, buffer, tam, timeout )
CNCSOCK *cs;
char *buffer;
int tam, timeout;
{
int recebidos = 0;
int restantes = tam;
void ( * func_old ) ();
func_old = signal (SIGALRM, AlarmSock);
alarm(timeout);
recebidos = recv( cs->sock, buffer, restantes, 0 );
alarm(0);
signal(SIGALRM, func_old);
return recebidos;
} /* end LeDoServer() */
/***************************************************************************/
int FimSocket(cs)
CNCSOCK *cs;
{
if ( cs == (CNCSOCK *)NULL )
return 0;
close(cs->sock);
free(cs);
cs = (CNCSOCK *)NULL;
return 0;
} /* end FimSocket() */
/****************************************************************************/
void AlarmSock()
{
return;
} /* end AlarmSock() */
/***************************************************************************/
int MsgFromClient (buffer, tam, timeout)
char *buffer;
int tam;
int timeout;
{
int lidos, tam_max=tam;
lidos = LeDoCliente ( buffer, tam_max, timeout );
return lidos;
} /* fim MsgFromClient */
/***************************************************************************/
int LeDoCliente( buffer, tam, timeout )
char *buffer;
int tam, timeout;
{
int recebidos = 0;
int restantes = tam;
int total = 0;
void ( * func_old ) ();
func_old = signal (SIGALRM, AlarmSock);
while ( restantes > 0 ) {
alarm(timeout);
recebidos = read( E_PADRAO, buffer, restantes );
alarm(0);
if ( recebidos < 0 )
break; /* sair do while() */
if ( recebidos == 0 )
break; /* sair do while() */
total += recebidos;
restantes -= recebidos;
buffer += recebidos;
}
signal(SIGALRM, func_old);
return total;
} /* end LeDoCliente() */
/**************************************************************************/
int MsgToClient (buffer, tam)
char *buffer;
int tam;
{
int tot_env;
tot_env = write ( S_PADRAO, buffer, tam );
if ( tot_env < 0 )
return -1;
return tot_env;
} /* fim MsgToClient() */