File: socketServerExample.cpp

package info (click to toggle)
libaria 2.8.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,628 kB
  • ctags: 16,574
  • sloc: cpp: 135,490; makefile: 925; python: 597; java: 570; ansic: 182
file content (146 lines) | stat: -rw-r--r-- 5,253 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Adept MobileRobots Robotics Interface for Applications (ARIA)
Copyright (C) 2004, 2005 ActivMedia Robotics LLC
Copyright (C) 2006, 2007, 2008, 2009, 2010 MobileRobots Inc.
Copyright (C) 2011, 2012, 2013 Adept Technology

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

If you wish to redistribute ARIA under different terms, contact 
Adept MobileRobots for information about a commercial version of ARIA at 
robots@mobilerobots.com or 
Adept MobileRobots, 10 Columbia Drive, Amherst, NH 03031; +1-603-881-7960
*/
#include "Aria.h"


/** @example socketServerExample.cpp ArSocket example server program
 *
  This is an example to show how to use ArSocket. 
  a server. This is the server program. The client is socketClientExample.cpp
  
  This program opens a server on port 7777. It waits for the
  client to connect and says hello to the client. It then waits for the
  client to say hello and then exits.

  First run this server program. Then run the client program,
  socketClientExample, perhaps in a different terminal window.
  You should see the server accept the connection, send its
  greeting to the client, recieve a response from the client,
  and then the connection closes.

  This server only accepts one client connection, and then
  it exits.  In practice, most servers would loop, accepting
  many clients, and simultaneously handling curently open
  client connections.  You can use threads to do that (see
  ArASyncTask), but ArSocket is not inherently thread-safe,
  you would also need to use an ArMutex object to protect 
  ArSocket calls. 

  @see ArSocket
  @see socketClientExample.cpp

  Also see the ArNetServer to help manage a server, or the  
  ArNetworking library for a more complete networking framework.
*/


int main()
{
  // The string to send to the client. 
  char *strToSend="Hello Client";
  // The buffer in which to recieve the hello from the client
  char buff[100];
  // The size of the string the client sent
  size_t strSize;

  // Initialize Aria.  This is especially essential on Windows,
  // because it will initialize Windows's sockets sytem.
  Aria::init();

  // The socket objects: one for accepting new client connections,
  // and another for communicating with a client after it connects.
  ArSocket serverSock, clientSock;


  // Open the server socket
  if (serverSock.open(7777, ArSocket::TCP))
    ArLog::log(ArLog::Normal, "socketServerExample: Opened the server port.");
  else
  {
    ArLog::log(ArLog::Normal, "socketServerExample: Failed to open the server port: %s.",
	   serverSock.getErrorStr().c_str());
    Aria::exit(-1);
    return(-1);
  }

  for(int clientNo = 0; Aria::getRunning(); ++clientNo)
  {

    // Wait for a client to connect to us.
    ArLog::log(ArLog::Normal, "socketServerExample: Waiting for a client to connect. Press CTRL-C to exit.");

    if (serverSock.accept(&clientSock))
      ArLog::log(ArLog::Normal, "socketServerExample: Client %d has connected.", clientNo);
    else
      ArLog::log(ArLog::Terse, "socketServerExample: Error in accepting a connection from the client: %s.",
         serverSock.getErrorStr().c_str());

    // Send the string 'Hello Client' to the client. write() should
    // return the same number of bytes that we told it to write. Otherwise,
    // its an error condition.
    if (clientSock.write(strToSend, strlen(strToSend)) == strlen(strToSend))
      ArLog::log(ArLog::Normal, "socketServerExample: Said hello to the client.");
    else
    {
      ArLog::log(ArLog::Normal, "socketServerExample: Error in sending hello string to the client.");
      Aria::exit(-1);
      return(-1);
    }

    // Read data from the client. read() will block until data is
    // received. 
    strSize=clientSock.read(buff, sizeof(buff));

    // If the amount read is 0 or less, its an error condition.
    if (strSize > 0)
    {
      // Terminate the string with a NULL character.
      buff[strSize]='\0';
      ArLog::log(ArLog::Normal, "socketServerExample: Client said: %s.", buff);
    }
    else
    {
      ArLog::log(ArLog::Normal, "socketServerExample: Error in waiting/reading the hello from the client.");
      Aria::exit(-1);
      return(-1);
    }

    // Now lets close the connection to the client
    clientSock.close();
    ArLog::log(ArLog::Normal, "socketServerExample: Socket to client closed.");
    
  }

  // And lets close the server port
  serverSock.close();
    ArLog::log(ArLog::Normal, "socketServerExample: Server socket closed.");


  // Uninitialize Aria and exit the program
  Aria::exit(0);

  return(0);
}