Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************/
- /* Include Section */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #include <arpa/inet.h>
- #include <sys/time.h>
- /************************************************************************/
- /* Variables declaration Section */
- /************************************************************************/
- #define MAX_SAMPL 256
- static time_t starttime;
- /************************************************************************/
- /* Code Section */
- /************************************************************************/
- static int moxaconnect(char *ipaddr, unsigned short port)
- {
- int sockfd;
- int error;
- int flag = 1;
- struct sockaddr_in addr_dest;
- addr_dest.sin_family = AF_INET;
- addr_dest.sin_addr.s_addr = inet_addr(ipaddr);
- addr_dest.sin_port = htons(port);
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd<0) {
- return(-2);
- }
- error = connect(sockfd, (struct sockaddr *)&addr_dest, sizeof(addr_dest));
- if (error) {
- close(sockfd);
- return(error);
- }
- error = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
- if (error) {
- close(sockfd);
- return error;
- }
- return sockfd;
- }
- static void get_usec_now(unsigned int *usec)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- tv.tv_sec -= starttime;
- *usec=(tv.tv_sec*1000000) + tv.tv_usec;
- }
- int main(int argc, char **argv)
- {
- int sock, error, i;
- unsigned char val = 'a';
- unsigned char bytein;
- unsigned int cur_diff;
- unsigned int sent_usec;
- unsigned int recv_usec;
- unsigned int min_diff = 0xffffffff;
- unsigned int max_diff = 0;
- unsigned int sum_diff = 0;
- struct timeval tv;
- if (argc<3) {
- printf("Usage: %s <moxa ip address> <port>\n", argv[0]);
- return -1;
- }
- sock=moxaconnect(argv[1], atoi(argv[2]));
- if (sock<0) {
- printf("Error %d opening socket\n", sock);
- return (sock);
- }
- gettimeofday(&tv, NULL);
- starttime = tv.tv_sec;
- printf("Start sending and receving %d bytes - All times in microseconds\n", MAX_SAMPL);
- for(i=0; i<MAX_SAMPL; i++) {
- error = send(sock, &val, 1, 0);
- get_usec_now(&sent_usec);
- if (error != 1)
- printf("Error %d writing on socket %d\n", error, sock);
- error = read(sock, &bytein, 1);
- get_usec_now(&recv_usec);
- if (error != 1)
- printf("Error %d reading on socket %d\n", error, sock);
- cur_diff = recv_usec - sent_usec;
- max_diff = (cur_diff > max_diff) ? cur_diff : max_diff;
- min_diff = (cur_diff < min_diff) ? cur_diff : min_diff;
- sum_diff += cur_diff;
- printf("%3d> SEND: %d\tRECV: %d\tDIFF: %d\tMIN: %d\tMAX: %d\tAVG: %d\n",
- i+1, sent_usec, recv_usec, cur_diff, min_diff, max_diff, ((sum_diff)/(i+1)));
- }
- error = shutdown(sock, SHUT_RDWR);
- return error;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement