Public paste
dgfdg
By: asd | Date: May 11 2007 10:20 | Format: C++ | Expires: never | Size: 3.39 KB | Hits: 1411

  1. #include <netdb.h>
  2. #include <netinet/in.h>
  3. #include <unistd.h>
  4. #include <iostream>
  5.  
  6. #define MAX_LINE 100
  7. #define LINE_ARRAY_SIZE (MAX_LINE+1)
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.   int socketDescriptor;
  14.   unsigned short int serverPort;
  15.   struct sockaddr_in serverAddress;
  16.   struct hostent *hostInfo;
  17.   char buf[LINE_ARRAY_SIZE], c;
  18.  
  19.   cout << "Enter server host name or IP address: ";
  20.   cin.get(buf, MAX_LINE, 'n');
  21.  
  22.   // gethostbyname() takes a host name or ip address in "numbers and
  23.   // dots" notation, and returns a pointer to a hostent structure,
  24.   // which we'll need later.  It's not important for us what this
  25.   // structure is actually composed of.
  26.   hostInfo = gethostbyname(buf);
  27.   if (hostInfo == NULL) {
  28.     cout << "problem interpreting host: " << buf << "n";
  29.     exit(1);
  30.   }
  31.  
  32.   cout << "Enter server port number: ";
  33.   cin >> serverPort;
  34.   cin.get(c); // dispose of the newline
  35.  
  36.   // Create a socket.  "AF_INET" means it will use the IPv4 protocol.
  37.   // "SOCK_STREAM" means it will be a reliable connection (i.e., TCP;
  38.   // for UDP use SOCK_DGRAM), and I'm not sure what the 0 for the last
  39.   // parameter means, but it seems to work.
  40.   socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
  41.   if (socketDescriptor < 0) {
  42.     cerr << "cannot create socketn";
  43.     exit(1);
  44.   }
  45.  
  46.   // Connect to server.  First we have to set some fields in the
  47.   // serverAddress structure.  The system will assign me an arbitrary
  48.   // local port that is not in use.
  49.   serverAddress.sin_family = hostInfo->h_addrtype;
  50.   memcpy((char *) &serverAddress.sin_addr.s_addr,
  51.          hostInfo->h_addr_list[0], hostInfo->h_length);
  52.   serverAddress.sin_port = htons(serverPort);
  53.                                
  54.   if (connect(socketDescriptor,
  55.               (struct sockaddr *) &serverAddress,
  56.               sizeof(serverAddress)) < 0) {
  57.     cerr << "cannot connectn";
  58.     exit(1);
  59.   }
  60.  
  61.   cout << "nEnter some lines, and the server will modify them andn";
  62.   cout << "send them back.  When you are done, enter a line withn";
  63.   cout << "just a dot, and nothing else.n";
  64.   cout << "If a line is more than " << MAX_LINE << " characters, thenn";
  65.   cout << "only the first " << MAX_LINE << " characters will be used.nn";
  66.  
  67.   // Prompt the user for input, then read in the input, up to MAX_LINE
  68.   // charactars, and then dispose of the rest of the line, including
  69.   // the newline character.
  70.   cout << "Input: ";
  71.   cin.get(buf, MAX_LINE, 'n');
  72.   while (cin.get(c) && c != 'n')
  73.     ;
  74.  
  75.   // Stop when the user inputs a line with just a dot.
  76.   while (strcmp(buf, ".")) {
  77.     // Send the line to the server.
  78.     if (send(socketDescriptor, buf, strlen(buf) + 1, 0) < 0) {
  79.       cerr << "cannot send data ";
  80.       close(socketDescriptor);
  81.       exit(1);
  82.     }
  83.  
  84.     // Zero out the buffer.
  85.     memset(buf, 0x0, LINE_ARRAY_SIZE);
  86.  
  87.     // Read the modified line back from the server.
  88.     if (recv(socketDescriptor, buf, MAX_LINE, 0) < 0) {
  89.       cerr << "didn't get response from server?";
  90.       close(socketDescriptor);
  91.       exit(1);
  92.     }
  93.  
  94.     cout << "Modified: " << buf << "n";
  95.  
  96.     // Prompt the user for input, then read in the input, up to MAX_LINE
  97.     // charactars, and then dispose of the rest of the line, including
  98.     // the newline character.  As above.
  99.     cout << "Input: ";
  100.     cin.get(buf, MAX_LINE, 'n');
  101.     while (cin.get(c) && c != 'n')
  102.       ;
  103.   }
  104.  
  105.   close(socketDescriptor);
  106.   return 0;
  107. }
  108.