Skip to content

Instantly share code, notes, and snippets.

@anveo
Created February 14, 2009 20:31
Show Gist options
  • Save anveo/64464 to your computer and use it in GitHub Desktop.
Save anveo/64464 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#define GETTIME(x) gettimeofday(x,NULL)
#define BUFFSIZE 32
void Die(char *mess) { perror(mess); exit(1); }
/* Globals */
char* msg = "test";
char* port = "6666";
struct timeval starttime;
struct timeval endtime;
double totaltime;
static void sig_alrm(int signo)
{
// interupt the recvfrom
return;
}
int connectTCP(char* hostname)
{
/*{{{*/
/* Initialize variables */
int sock;
char buffer[BUFFSIZE];
unsigned int echolen;
int received = 0;
int status;
struct addrinfo hints;
struct addrinfo* servinfo;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
status = getaddrinfo(hostname, port, &hints, &servinfo);
sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if(sock < 0)
{
Die("Failed to connect with server");
}
/* Establish connection */
if(connect(sock,
servinfo->ai_addr,
servinfo->ai_addrlen) < 0) {
Die("Failed to connect with server");
}
/* Send the word to the server */
echolen = strlen(msg);
//if(send(sock, msg, echolen, 0) != echolen) {
if(send(sock, msg, echolen, 0) < 0) {
Die("Mismatch in number of sent bytes");
}
/* Receive the word back from the server */
fprintf(stdout, "Received: ");
while(received < echolen) {
int bytes = 0;
if((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1) {
Die("Failed to receive bytes from server");
}
received += bytes;
buffer[bytes] = '\0'; /* Assure null terminated string */
fprintf(stdout, buffer);
}
fprintf(stdout, "\n");
close(sock);
freeaddrinfo(servinfo);
return 1;/*}}}*/
}
int connectUDP(char* hostname)
{
/*{{{*/
/* Initialize variables */
int sock;
char buffer[BUFFSIZE];
unsigned int echolen;
int received = 0;
int status;
struct addrinfo hints;
struct addrinfo* servinfo;
struct sockaddr_in addr;
signal(SIGALRM,sig_alrm);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
status = getaddrinfo(hostname, port, &hints, &servinfo);
sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if(sock < 0)
{
Die("Failed to connect with server");
}
/* Send the word to the server */
echolen = strlen(msg);
//if(send(sock, msg, echolen, 0) != echolen) {
if(sendto(sock, msg, strlen(msg)+1, 0, servinfo->ai_addr, servinfo->ai_addrlen) < 0) {
Die("Mismatch in number of sent bytes");
}
/* Receive the word back from the server */
fprintf(stdout, "Received: ");
while(received < echolen) {
int bytes = 0;
int addrlen = sizeof(addr);
if((bytes = recvfrom(sock, buffer, BUFFSIZE-1, 0, (struct sockaddr*)&addr, addrlen)) < 1) {
//Die("Failed to receive bytes from server");
}
received += bytes;
buffer[bytes] = '\0'; /* Assure null terminated string */
fprintf(stdout, buffer);
}
fprintf(stdout, "\n");
close(sock);
freeaddrinfo(servinfo);
return 1;/*}}}*/
}
int main(int argc, char** argv)
{
/* Make sure command-line arguments have been passed */
if(argc != 4)
{
printf("Usage: %s host1 host2 host3\n", argv[0]);
exit(0);
}
/* Initialize the timer */
printf("Timer started .........\n");
GETTIME (&starttime);
/* Loop through the hosts */
int i;
for(i = 1; i < argc; i++)
{
connectUDP(argv[i]);
}
/* End the timer */
GETTIME(&endtime);
printf("Timer ends .........\n");
/* Output results */
totaltime = (double)((endtime.tv_sec-starttime.tv_sec)+(0.000001*(endtime.tv_usec - starttime.tv_usec)));
printf("TotalTime as measured by timer=%g sec\n", (double) totaltime);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment