Guest User

server_socket

a guest
Aug 14th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1.  void* run() {
  2.         // Remove 1 item at a time and process it. Blocks if no items are
  3.         // available to process.
  4.  
  5.         //Client declaration
  6.         TCPConnector* connector_client = new TCPConnector();
  7.         printf("ip_client = %s\tport_client = %s\tport_client_int = %d\n", ip_client.c_str(), port_client.c_str(),atoi(port_client.c_str()));
  8.         TCPStream* stream_client = connector_client->connect(ip_client.c_str(), atoi(port_client.c_str()));
  9.  
  10.                 //Client declaration ends
  11.         for (int i = 0;; i++) {
  12.             printf("thread %lu, loop %d - waiting for item...\n",
  13.                    (long unsigned int)self(), i);
  14.             WorkItem* item = m_queue.remove();
  15.             printf("thread %lu, loop %d - got one item\n",
  16.                    (long unsigned int)self(), i);
  17.             TCPStream* stream = item->getStream();
  18.  
  19.  
  20.  
  21.             // Echo messages back the client until the connection is
  22.             // closed
  23.             //char input[256];
  24.             char input[1024]; //changed from 256 to 1024
  25.             char line[1024];
  26.             memset(line,'\0',sizeof(line));
  27.             memset(input,'\0',sizeof(input));
  28.             int len;
  29.             while ((len = stream->receive(input, sizeof(input)-1)) > 0 ){
  30.                 input[len] = NULL;
  31.                 //Code Addition by Srini starts here
  32.  
  33.  
  34.                 //Client declaration
  35.                 //TCPConnector* connector_client = new TCPConnector();
  36.                 //printf("ip_client = %s\tport_client = %s\tport_client_int = %d\n", ip_client.c_str(), port_client.c_str(),atoi(port_client.c_str()));
  37.                 //TCPStream* stream_client = connector_client->connect(ip_client.c_str(), atoi(port_client.c_str()));
  38.  
  39.                 //Client declaration ends
  40.                 if (stream_client)
  41.                 {
  42.                         stream_client->send(input, sizeof(input));
  43.                         printf("sent - %s\n", input);
  44.                         len = stream_client->receive(line, sizeof(line));
  45.                         line[len] = NULL;
  46.                         printf("received - %s\n", line);
  47.                         delete stream_client;
  48.                 }
  49.  
  50.                 //Code Additon by Srini ends here
  51.  
  52.                 stream->send(line, len);
  53.                 printf("thread %lu, echoed '%s' back to the client\n",
  54.                        (long unsigned int)self(), line);
  55.             }
  56.             delete item;
  57.         }
  58.  
  59.         // Should never get here
  60.         return NULL;
  61. }
Add Comment
Please, Sign In to add comment