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 also:
ArSocket

socketClientExample.cpp

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

00001 #include "Aria.h"
00002 
00003 
00035 int main()
00036 {
00037   // The string to send to the client. 
00038   char *strToSend="Hello Client";
00039   // The buffer in which to recieve the hello from the client
00040   char buff[100];
00041   // The size of the string the client sent
00042   size_t strSize;
00043 
00044   // The socket objects: one for accepting new client connections,
00045   // and another for communicating with a client after it connects.
00046   ArSocket serverSock, clientSock;
00047 
00048   // Initialize Aria.  This is especially essential on Windows,
00049   // because it will initialize Windows's sockets sytem.
00050   Aria::init();
00051 
00052   // Open the server socket
00053   if (serverSock.open(7777, ArSocket::TCP))
00054     ArLog::log(ArLog::Normal, "socketServerExample: Opened the server port.");
00055   else
00056   {
00057     ArLog::log(ArLog::Normal, "socketServerExample: Failed to open the server port: %s.",
00058        serverSock.getErrorStr().c_str());
00059     return(-1);
00060   }
00061 
00062   for(int clientNo = 0; Aria::getRunning(); ++clientNo)
00063   {
00064 
00065     // Wait for a client to connect to us.
00066     ArLog::log(ArLog::Normal, "socketServerExample: Waiting for a client to connect. Press CTRL-C to exit.");
00067 
00068     if (serverSock.accept(&clientSock))
00069       ArLog::log(ArLog::Normal, "socketServerExample: Client %d has connected.", clientNo);
00070     else
00071       ArLog::log(ArLog::Terse, "socketServerExample: Error in accepting a connection from the client: %s.",
00072          serverSock.getErrorStr().c_str());
00073 
00074     // Send the string 'Hello Client' to the client. write() should
00075     // return the same number of bytes that we told it to write. Otherwise,
00076     // its an error condition.
00077     if (clientSock.write(strToSend, strlen(strToSend)) == strlen(strToSend))
00078       ArLog::log(ArLog::Normal, "socketServerExample: Said hello to the client.");
00079     else
00080     {
00081       ArLog::log(ArLog::Normal, "socketServerExample: Error in sending hello string to the client.");
00082       return(-1);
00083     }
00084 
00085     // Read data from the client. read() will block until data is
00086     // received. 
00087     strSize=clientSock.read(buff, sizeof(buff));
00088 
00089     // If the amount read is 0 or less, its an error condition.
00090     if (strSize > 0)
00091     {
00092       // Terminate the string with a NULL character.
00093       buff[strSize]='\0';
00094       ArLog::log(ArLog::Normal, "socketServerExample: Client said: %s.", buff);
00095     }
00096     else
00097     {
00098       ArLog::log(ArLog::Normal, "socketServerExample: Error in waiting/reading the hello from the client.");
00099       return(-1);
00100     }
00101 
00102     // Now lets close the connection to the client
00103     clientSock.close();
00104     ArLog::log(ArLog::Normal, "socketServerExample: Socket to client closed.");
00105     
00106   }
00107 
00108   // And lets close the server port
00109   serverSock.close();
00110     ArLog::log(ArLog::Normal, "socketServerExample: Server socket closed.");
00111 
00112 
00113   // Uninitialize Aria
00114   Aria::uninit();
00115 
00116   // All done
00117   return(0);
00118 }

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7