int main(int argc, char** argv){

	if (argc!=3) {
		printf("Usage: %s <Server IP> <PortNumer>\n",argv[0]);
		exit(1);
	}
	struct sockaddr_in serverAddr;
	socklen_t serverAddrLen;
	serverAddr.sin_family = PF_INET;
	serverAddr.sin_port = htons(atoi(argv[2]));
	inet_pton(AF_INET,argv[1],&serverAddr.sin_addr);
	serverAddrLen = sizeof(serverAddr);
	
	newSocket* server;
	server = connectUDP(&serverAddr,serverAddrLen);
	
	char buffer[1000] = "P r a s e e m";
	buffer[13] = '\0';
	char recv[1000];
	sendToUDP(server,buffer);
	recvFromUDP(server,recv);
	printf("Received %s\n",recv);

	return 0;
}
/*
 * This function should pass messages to client, collect ack
 * and calculate the latency as per given formula.
*/
int
UDPLatency(const char *host, const char *service, int msg_len) {
	char *buf;
	
	int sock, nchars;	
	char msg_count = 0;
	struct timeval client_start_time, client_end_time;
	long int cst_usecs, cet_usecs;
	
	/* Create msg buffer of specified length here */
	buf = (char *) malloc(sizeof(char) * msg_len);
	memset(buf, 'M', msg_len); 

	/* Allocate a socket */
	sock = connectUDP(host, service);

	/* Note time before sending the messages */
	gettimeofday(&client_start_time, NULL);
	cst_usecs = (client_start_time.tv_sec * MICROSECS + client_start_time.tv_usec);

	while(msg_count < MSGCOUNT) {
		msg_count++;
		
		buf[0] = msg_count;
		nchars = strlen(buf);
		(void *)write(sock, buf, msg_len);

		fprintf(stderr, "The size of buffer is %d \n", strlen(buf));

		/* Read the ack back from server */
		if(read(sock, buf, sizeof(buf)) < 0) {
			errexit("Socket read failed %s \n", strerror(errno));
		}
	}

	/* Note time after all messages are received */
	gettimeofday(&client_end_time, NULL);
	cet_usecs = (client_end_time.tv_sec * MICROSECS + client_end_time.tv_usec);

	/* Calculate throughput number */
	long int total_time_msecs = (cet_usecs - cst_usecs)/1000;
	long int throughput = (MSGCOUNT * msg_len * 1000)/total_time_msecs;	
	
	fprintf(stdout, "\nTotal time spent is: %ld ms, Throughtput of client with msg size : %d is %ld \n", total_time_msecs, msg_len, throughput);	
	
	free(buf);
	close(sock);

}
Beispiel #3
0
/*------------------------------------------------------------------------
 * UDPecho - send input to ECHO service on specified host and print reply
 *------------------------------------------------------------------------
 */
int
UDPecho(const char *host, const char *service)
{
        char    buf[LINELEN+1];         /* buffer for one line of text  */
        int     s, nchars;              /* socket descriptor, read count*/

        s = connectUDP(host, service);

        while (fgets(buf, sizeof(buf), stdin)) {
                buf[LINELEN] = '\0';    /* insure null-terminated */
                nchars = strlen(buf);
                (void) write(s, buf, nchars);

                if (read(s, buf, nchars) < 0)
                        errexit("socket read failed: %s\n",
                                        strerror(errno));
                fputs(buf, stdout);
        }
}
/*------------------------------------------------------------------------
 * main - UDP client for TIME service that prints the resulting time
 *------------------------------------------------------------------------
 */
int
main(int argc, char *argv[])
{
	char	*host = "localhost";	/* host to use if none supplied	*/
	char	*service = "time";	/* default service name		*/
	time_t	now;			/* 32-bit integer to hold time	*/ 
	int	s, n;			/* socket descriptor, read count*/

	switch (argc) {
	case 1:
		host = "localhost";
		break;
	case 3:
		service = argv[2];
		/* FALL THROUGH */
	case 2:
		host = argv[1];
		break;
	default:
		fprintf(stderr, "usage: UDPtime [host [port]]\n");
		exit(1);
	}

	s = connectUDP(host, service);

	(void) write(s, MSG, strlen(MSG));

	/* Read the time */

	n = read(s, (char *)&now, sizeof(now));
	if (n < 0)
		errexit("read failed: %s\n", strerror(errno));
	now = ntohl((unsigned long)now);	/* put in host order	*/
	now -= UNIXEPOCH;		/* convert UCT to UNIX epoch	*/
	printf("%s", ctime(&now));
	exit(0);
}