GENERIC_SERVER  0.0.0.9
A light-weight, cross-platform, pluggable, extensible and secure framework for deploying C++ plug-ins.
 All Classes Files Functions Variables Typedefs Pages
sample_client.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2013 Broadcom Corporation
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License version 2.1 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public
14  License along with this library; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
28 #include "util.h"
29 
30 void error(const char *msg)
31 {
32  perror(msg);
33  exit(0);
34 }
35 /***********************************************************************************/
36 
37 int main(int argc, char *argv[])
38 {
39  char buff[MAX_SZ],read_buff[MAX_SZ];
40  SOCKET sock;
41  unsigned int portno,size,sess_type;
42  int n,i,no_req;
43  string plugin_name,message;
44  SSL *ssl_client;
45  SSL_CTX *ctx;
46  ssl_client = NULL;
47  ctx = NULL;
48 
49 #ifdef WINDOWS
50  sock_init();
51 #endif
52  memset(buff,0,MAX_SZ);
53  if (argc < 7)
54  error("Usage: sample_client <ip_address> <port> <plugin_name> <message> <no_requests> <1=SSL|0=RAW>");
55  portno = atoi(argv[2]);
56  plugin_name = string(argv[3]);
57  message = string(argv[4]);
58  no_req = atoi(argv[5]);
59  sess_type = atoi(argv[6]);
60  if(sess_type == 1)
61  {
62  if(!ssl_init(&ctx))
63  error("ERROR SSL init");
64  if(!ssl_connect((unsigned char *) argv[1], portno,sock,&ssl_client,&ctx))
65  error("ERROR SSL connecting");
66  }
67  else
68  {
69  if(!sock_connect((unsigned char *)argv[1],portno,sock))
70  error("ERROR connecting");
71  }
72  setblocking(sock);
73  memset(buff,0,MAX_SZ);
74  size = create_message(plugin_name,message,buff);
75  for(i=0; i < no_req; i++)
76  {
77  if(sess_type == 1)
78  n = SSL_write(ssl_client, buff,size);
79  else
80  n = send(sock,buff,size,0);
81  if (n < 0)
82  error("write ERROR ..");
83  if(sess_type == 1)
84  n = ssl_read_socket((unsigned char *)read_buff,sock,&ssl_client);
85  else
86  n = read_socket((unsigned char *)read_buff,sock);
87  if (n < 0)
88  error("read ERROR..");
89  cout << "Ctr: " << i << " " << string(read_buff) << endl;
90  if(!memcmp(read_buff,"00 TERMINATE",12))
91  break;
92  }
93  if(sess_type == 1)
94  while((SSL_shutdown(ssl_client)) == 0);
95 #ifdef WINDOWS
96  closesocket(sock);
97 #else
98  close(sock);
99 #endif
100  return 0;
101 }
102 /***********************************************************************************/
103