-2

I have made small server and client programs to compare time taken for udp and tcp. So I have made a while loop for sending 100 messages. What the problem seems to be is that presence of loop in tdp client causes program to stop execution after certain line. The debug lines are also not being executed. I have never seen this kind of behaviour. I expect the loop to exeute 100 times and output the time taken for those loops. But the otput is just Connection to server:port 127.0.0.1:55057

/* usage: ./tcpclient host port */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <arpa/inet.h>

#define BUFSIZE 1024

void error(char *msg) {
   perror(msg);
   exit(0);
}

int main(int argc, char **argv) {
    int sockfd, portno, n;
    struct sockaddr_in serveraddr;
    struct hostent *server;
    char *hostname;
    char buf[BUFSIZE];
    strcpy(buf, "hello");
    clock_t start;  int count=0;
    /* check command line arguments */
    if (argc != 3) {
        fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
        exit(0);
    }
    hostname = argv[1];
    portno = atoi(argv[2]);

    /* socket: create the socket */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");

    /* gethostbyname: get the server's DNS entry */
    server = gethostbyname(hostname);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host as %s\n", hostname);
        exit(0);
    }
    /* build the server's Internet address */
    bzero((char *) &serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
            (char *)&serveraddr.sin_addr.s_addr, server->h_length);
    serveraddr.sin_port = htons(portno);

    /* connect: create a connection with the server */
    if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0)
        error("ERROR connecting");

    /* get message line from the user */

    printf("Connection to server:port %s:%d\n",inet_ntoa(serveraddr.sin_addr),htons(portno));
    printf("I never here here in presence of loop");

    start = clock();
    while(1){
        /* send the message line to the server */
        n = write(sockfd, buf, strlen(buf));
        if (n < 0)
            error("ERROR writing to socket");
        /* print the server's reply */
        n = read(sockfd, buf, BUFSIZE);
        if (n < 0)
            error("ERROR reading from socket");
        printf("Echo from server: %s", buf);
     }
    printf("Time taken: %ld ",clock()-start);
    close(sockfd);
    return 0;
}
dmrche
  • 1
  • 4
  • 2
    What do you mean by "execute after certain line"? – giusti Jan 17 '17 at 17:31
  • 1
    It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight Jan 17 '17 at 17:34
  • where's the UDP client? – yano Jan 17 '17 at 17:35
  • @ yano Udp client is working fine with the same for loop. So I didn't put the code. – dmrche Jan 17 '17 at 17:38
  • the output from the calls to `printf()` are not displaying, as expected, is because the `format` parameter string is not terminated with a '\n' so the output is sitting in the stdout system buffer. – user3629249 Jan 17 '17 at 17:41
  • this line: `if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0)` should be: `if (connect(sockfd, (const struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)` – user3629249 Jan 17 '17 at 17:52
  • returning a 0 from `main()` is generally considered to mean `success`. However, that is what the posted code is returning when an error occurs. Suggest replacing: `exit(0);` with `exit( EXIT_FAILURE );` – user3629249 Jan 17 '17 at 18:06
  • the function: `htons()` returns an `uint16_t`, so this line: `printf("Connection to server:port %s:%d\n",inet_ntoa(serveraddr.sin_addr),htons(portno));` should be: `printf("Connection to server:port %s:%uh\n",inet_ntoa(serveraddr.sin_addr),htons(portno)); And the variable declaration: `int protno;` should be: `uint16_t portno;` and this line: `portno = atoi(argv[2]);` should be: `portno = (unsigned short int)atoi(argv[2]);` – user3629249 Jan 17 '17 at 18:14
  • thie code block beginning with: `while(1)` will run forever, perhaps eventually hanging on the `read()` statement. The question states 100 messages, so the line should be: `for( size_t i=0; i<100; i++ )` – user3629249 Jan 17 '17 at 18:24
  • By inserting the statement: `#include ` you can eliminate the function: `error() in your code. the syntax for `error()` is: `void error(int status, int errnum, const char *format, ...);` – user3629249 Jan 17 '17 at 18:26

1 Answers1

0

The line

printf("I never here here in presence of loop");

is not showing up because stdout is likely line-buffered and therefore output won't be visible until a newline is sent to stdout, or stdout is flushed.

If you change it to

printf("I never here here in presence of loop\n");

it will likely show up in your output.

Community
  • 1
  • 1
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56