int main (int argc, char *argv[])
{
    int         sock;                   /* Socket descriptor */
    char *      echoString;             /* String to send to echo server */
    char        echoBuffer[RCVBUFSIZE]; /* Buffer for received string */
    int         echoStringLen;          /* Length of string to echo */
    int         bytesRcvd;              /* Bytes read in single recv() */
    int         bytesSend;              /* Bytes read in single send() */
    int         i;                      /* counter for data-arguments */

    parse_args (argc, argv);

    sock = CreateTCPClientSocket (argv_ip, argv_port);
        
    for (i = 0; i < argv_nrofdata; i++)
    {
        echoString = argv_data [i];
        echoStringLen = strlen (echoString);          /* Determine input length */

        delaying ();
        
        // TODO: add code to send this string to the server; use send()
        bytesSend = send (sock, echoString, echoStringLen, 0);
        if (bytesSend < 0)
        {
            DieWithError ("send() failed");
        }
        
        // TODO: add code to display the transmitted string in verbose mode; use info_s()
        info_s ("Sending to server:  ", echoString);
        
        // TODO: add code to receive & display the converted string from the server
        //       use recv() & printf()
        bytesRcvd = recv (sock, echoBuffer, RCVBUFSIZE-1, 0);
        if (bytesRcvd < 0)
        {
            DieWithError ("recv() failed");
        }
        add_nt (echoBuffer, bytesRcvd);
        printf ("                            Received from server:'%s'\n", echoBuffer);
    }

    delaying ();

    close (sock);
    info ("close & exit");
    exit (0);
}
int main (int argc, char *argv[])
{
    int         sock;                   /* Socket descriptor */
    char        echoBuffer[RCVBUFSIZE]; /* Buffer for received string */
    int         bytesRcvd;              /* Bytes read in single recv() */
    int         bytesSend;              /* Bytes read in single send() */
    char        line[80];               /* String entered by user to send to server */

    parse_args (argc, argv);

    sock = CreateTCPClientSocket (argv_ip, argv_port);
        
    while (strcmp (line, "Quit") != 0)
    {
        /* Send message to server */
        fgets (line, sizeof (line), stdin);
        remove_nl (line);
        info_s ("Entered:  ", line);

        bytesSend = send (sock, line, strlen (line), 0);
        if (bytesSend < 0)
        {
            DieWithError ("send() failed");
        }

        /* Receive message from server */
        if(strcmp (line, "Quit") != 0)
        {
            bytesRcvd = recv (sock, echoBuffer, RCVBUFSIZE-1, 0);
            if (bytesRcvd < 0)
            {
                DieWithError ("recv() failed");
            }
            if (bytesRcvd > 0)
            {
                add_nt (echoBuffer, bytesRcvd);
                printf ("Received: %s\n", echoBuffer);
            }
        }
    }

    delaying ();

    close (sock);
    info ("close & exit");
    exit (0);
}
Exemplo n.º 3
0
int main (int argc, char *argv[])
{
    int         sock;                   /* Socket descriptor */
    char *      echoString;             /* String to send to echo server */
    char        echoBuffer[RCVBUFSIZE]; /* Buffer for received string */
    int         echoStringLen;          /* Length of string to echo */
    int         bytesRcvd;              /* Bytes read in single recv() */
    int         i;                      /* counter for data-arguments */

    parse_args (argc, argv);

    sock = CreateTCPClientSocket (argv_ip, argv_port);
        
    for (i = 0; i < argv_nrofdata; i++)
    {
        echoString = argv_data [i];
        echoStringLen = strlen (echoString);          /* Determine input length */
        delaying();
        
        if (send(sock, echoString, echoStringLen, 0) < 0) {
            printf("error in send: exiting");
            exit(-1);
        }
        info_s("data", echoString);
        
        bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0);
        if (bytesRcvd < 0) {
            printf("error in recv: exiting");
            exit(-1);
        }
        
        int j;
        for (j = 0; j < bytesRcvd; j++) {
            printf("%c", echoBuffer[j]);
        }
        printf("\n");
    }

    delaying ();

    close (sock);
    info ("close & exit");
    exit (0);
}