Advertisement
Guest User

tcp_client.c

a guest
Oct 30th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. /*-
  2.  * Copyright (c) 2013 Michael Tuexen
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24.  * SUCH DAMAGE.
  25.  */
  26.  
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <sys/select.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include "socket.h"
  37.  
  38. #define BUFFER_SIZE  (2<<16)
  39. #define MESSAGE_SIZE (9216)
  40.  
  41. int main(int argc, char **argv)
  42. {
  43.     int fd, in, port;
  44.     fd_set rset;
  45.     struct sockaddr_in server_addr;
  46.     ssize_t len;
  47.     char buf[BUFFER_SIZE];
  48.  
  49.     if (argc != 3)
  50.     {
  51.         printf("usage: echo_client \"IP-Adress Port\"\n");
  52.         return 0;
  53.     }
  54.    
  55.     port = atoi(argv[2]);
  56.  
  57.     /* Adresse ausgeben */
  58. //  printf("Address: %s:%d\n", argv[1], port);
  59.  
  60.     in = 0;
  61.     fd = Socket(AF_INET, SOCK_STREAM, 0);
  62.  
  63.     memset(&server_addr, 0, sizeof(server_addr));
  64.     server_addr.sin_family = AF_INET;
  65. #ifdef HAVE_SIN_LEN
  66.     server_addr.sin_len = sizeof(struct sockaddr_in);
  67. #endif
  68.     server_addr.sin_port = htons(port);
  69.     if ((server_addr.sin_addr.s_addr = (in_addr_t)inet_addr(argv[1])) == INADDR_NONE) {
  70.         fprintf(stderr, "Invalid address\n");
  71.     }
  72.  
  73.     Connect(fd, (const struct sockaddr *) &server_addr, sizeof(server_addr));
  74.  
  75.     memset((void *) buf, 'A', sizeof(buf));
  76.    
  77.     /* FD-Set initialisieren */
  78.     FD_ZERO(&rset);
  79.     /* FileDescriptors zum set hinzufügen */
  80.     FD_SET(in, &rset);
  81.     FD_SET(fd, &rset);
  82.  
  83.     for(;;)
  84.     {
  85.         if (Select(fd + 1, &rset, NULL, NULL, NULL) == 0)
  86.         {
  87.             /* Timeout... call Select again */
  88.             printf("Select call timed out\n");
  89.             continue;
  90.         }
  91.  
  92.         /* Pending user input */
  93.         if (FD_ISSET(in, &rset))
  94.         {
  95.             /* Buffer "nullen" */
  96.             memset((void *) buf, 0, sizeof(buf));
  97.  
  98.             len = Read(in, (void *) buf, sizeof(buf));
  99.             if (len < 0)
  100.             {
  101.                 perror("read");
  102.             }
  103.            
  104.             /* Stdin geschlossen */
  105.             if (len == 0)
  106.             {
  107.                 /* Verbindungsabbau anstossen */
  108.                 Shutdown(fd, SHUT_WR);
  109.                 printf("Verbindungsabbau...\n");
  110.             }
  111.  
  112.             /* Send user input to server */
  113.             Write(fd, (const void *) buf, (size_t) len);
  114.  
  115.             buf[len] = '\0';
  116.             printf("Send to server: %s\n", buf);
  117.         }
  118.  
  119.         /* Pending data on port */
  120.         if (FD_ISSET(fd, &rset))
  121.         {
  122.             /* Buffer "nullen" */
  123.             memset((void *) buf, 0, sizeof(buf));
  124.  
  125.             /* Socket lesen */
  126.             len = Read(fd, (void *) buf, sizeof(buf));
  127.  
  128.             if (len == 0)
  129.             {
  130.                 /* Server closed the connection */
  131.                 printf("Server closed the connection.\n");
  132.                 break;
  133.             }
  134.  
  135.             buf[len] = '\0';
  136.             printf("%s", buf);
  137.         }
  138.     }
  139.  
  140.     /* Client soll sich beenden sobald der Server die Verbindung schließt */
  141.     Close(fd);
  142.    
  143.  
  144.     return(0);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement